怎样在Discord.py中添加后续按钮?

huangapple go评论150阅读模式
英文:

How can i add buttons afterly in Discord.py?

问题

await message.edit(view=view)
英文:

I am making a Discord bot for an order. And I need to make a role giver button list and add buttons to the first message after with a command I trying this for 2 days but I can't add buttons only edit the first message

My code is:

class RolButton(Button):
    def __init__(self, rol_name):
        global a
        a = role_name
        super().__init__(label=rol_name, style=ButtonStyle.green)

    async def callback(self, interaction):
        author = interaction.user
        
        role = discord.utils.get(author.guild.roles, name=a)
        if role in author.roles:
            await author.remove_roles(role)
            await interaction.response.send_message(f"you dropped {role} role", ephemeral=True)
        else:
            await author.add_roles(role)
            await interaction.response.send_message(f"You take {role} role", ephemeral=True)

@Bot.command()
async def role(ctx, *args):
    args = str(args).replace("(", "").replace(")", "").replace("', '", " ").replace("'", "")
    await ctx.channel.purge(limit=1)
    button = RolButton(args)
    view = MyView()
    guild = ctx.guild
    await guild.create_role(name=args)
    view.add_item(button)
    embed = Embed(
        title="Take roles",
        description="Click buttons you want"
    )
    await ctx.send(embed=embed, view=view)

@Bot.command()
async def rolekle(ctx, *args):
    args = str(args).replace("(", "").replace(")", "").replace("', '", " ").replace("'", "")
    await ctx.channel.purge(limit=1)
    button = RolButton(args)
    view = MyView()
    guild = ctx.guild
    await guild.create_role(name=args)
    view.add_item(button)
    async for message in ctx.channel.history(limit=1):
        await message.edit(view=view)

How can I add buttons after with command?

答案1

得分: 0

如果您想要向消息添加按钮并希望保留已存在的按钮您需要重用现有视图或创建一个带有相同组件的新视图
英文:

If you want to add a button to a message and want to keep the ones that already exist, you need to reuse the existing view or create a new one with the same components:

class RolButton(Button):
    def __init__(self, rol_name):
        super().__init__(label=rol_name, style=ButtonStyle.green)

    async def callback(self, interaction):
        author = interaction.user
        
        role = discord.utils.get(author.guild.roles, name=self.label)
        if role in author.roles:
            await author.remove_roles(role)
            await interaction.response.send_message(f"you dropped {role} role", ephemeral=True)
        else:
            await author.add_roles(role)
            await interaction.response.send_message(f"You take {role} role", ephemeral=True)


@bot.command()
async def rolekle(ctx, *args):
    # args is a list of arguments, do not convert it to string, this is very ugly
    guild = ctx.guild
    new_role = await guild.create_role(name=args[0])
    # delete command message
    await ctx.message.delete()
    view = MyView()
    async for message in ctx.channel.history(limit=1):
        for row in message.components:
            for button in row.children: 
                view.add_item(RolButton(button.label))
        view.add_item(RolButton(new_role.name))
        await message.edit(view=view)

huangapple
  • 本文由 发表于 2023年4月13日 17:10:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003692.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定