英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论