user19932233
user19932233

Reputation:

How to disable other buttons in a view

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 needed libraries and everything else, the problem is just with the code. Any help will be appreciated!

Upvotes: -1

Views: 64

Answers (1)

Brawldude2
Brawldude2

Reputation: 138

You can use view.children to get the button from label. Here's the solution:

yes_btn = discord.utils.get(self.children, label="Yes") #We need to access the other button this way
yes_btn.disabled = True

Upvotes: -1

Related Questions