How can i add buttons afterly in Discord.py?
Question:
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?
Answers:
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)
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?
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)