In my discord bot, I’v made a nuke channel command with a confirmation button. The problem is that when I click no, the yes button is still pressable. What I want is when I press no, both the yes and no button will disappear.
Here is my code:
class nukeConfirm(discord.ui.View):
def __init__(self, ctx, nuke_channel):
self.ctx = ctx
self.nuke_channel = nuke_channel
super().__init__()
@discord.ui.button(label="Yes", style=discord.ButtonStyle.green)
async def yes_callback(self, interaction, button):
if interaction.user == self.ctx.author:
new_channel = await self.nuke_channel.clone(reason="Has been Nuked!")
await self.nuke_channel.delete()
await new_channel.send(f"Nuked! {self.ctx.author.mention}", delete_after=0.1)
await new_channel.send(f"Nuked by `{self.ctx.author.name}`")
with open("logs.json", "a") as f:
f.writelines(f"\n{self.ctx.author} used the nuke command. Channel nuked: {self.nuke_channel.name}")
@discord.ui.button(label="No", style=discord.ButtonStyle.red)
async def no_callback(self, interaction, button):
if interaction.user == self.ctx.author:
await self.ctx.send("Aborted")
button.label = "No"
button.disabled = True
await interaction.response.edit_message(view=self)
class Channels(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
@commands.has_permissions(manage_channels=True)
async def nuke(self, ctx, channel: discord.TextChannel = None):
if channel is None:
await ctx.send(f"{ctx.author.mention}! You did not mention a channel!", delete_after=5)
return
nuke_channel = discord.utils.get(ctx.guild.channels, name=channel.name)
if nuke_channel is not None:
view = nukeConfirm(ctx, nuke_channel)
await nuke_channel.send(f"{ctx.author.mention}, are you sure you want to nuke this channel?", view=view)
else:
await ctx.send(f"No channel named **{channel.name}** was found!")
@nuke.error
async def nuke_error(self, ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send("You don't have permission to use this command.")
Of course, I have imported the need molecules and everything else, the problem is just with the code. Any help will be appreciated!
>Solution :
To achieve the behavior you want, where both the "Yes" and "No" buttons disappear after clicking "No" in the confirmation prompt, you can modify your code slightly. Currently, you are only disabling the "No" button when it is clicked, but you also need to disable the "Yes" button.
In the no_callback method of the nukeConfirm class, the loop iterates over all the children (buttons) in the view and disables them except for the button that was clicked. This ensures that both "Yes" and "No" buttons are disabled when "No" is clicked.
With these changes, clicking "No" should now disable both buttons in the confirmation prompt as you intended.
class nukeConfirm(discord.ui.View):
def __init__(self, ctx, nuke_channel):
self.ctx = ctx
self.nuke_channel = nuke_channel
super().__init()
@discord.ui.button(label="Yes", style=discord.ButtonStyle.green)
async def yes_callback(self, interaction, button):
if interaction.user == self.ctx.author:
new_channel = await self.nuke_channel.clone(reason="Has been Nuked!")
await self.nuke_channel.delete()
await new_channel.send(f"Nuked! {self.ctx.author.mention}", delete_after=0.1)
await new_channel.send(f"Nuked by {self.ctx.author.name}")
with open("logs.json", "a") as f:
f.writelines(f"\n{self.ctx.author} used the nuke command. Channel nuked: {self.nuke_channel.name}")
@discord.ui.button(label="No", style=discord.ButtonStyle.red)
async def no_callback(self, interaction, button):
if interaction.user == self.ctx.author:
await self.ctx.send("Aborted")
for child in self.children:
if isinstance(child, discord.ui.Button) and child != button:
child.disabled = True
await interaction.response.edit_message(view=self)
class Channels(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
@commands.has_permissions(manage_channels=True)
async def nuke(self, ctx, channel: discord.TextChannel = None):
if channel is None:
await ctx.send(f"{ctx.author.mention}! You did not mention a channel!", delete_after=5)
return
nuke_channel = discord.utils.get(ctx.guild.channels, name=channel.name)
if nuke_channel is not None:
view = nukeConfirm(ctx, nuke_channel)
await nuke_channel.send(f"{ctx.author.mention}, are you sure you want to nuke this channel?", view=view)
else:
await ctx.send(f"No channel named {channel.name} was found!")
@nuke.error
async def nuke_error(self, ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send("You don't have permission to use this command.")