So I've tried to make my discord bot a nuke command, but only the nuke part works, the cancel part doesn't work.
My code:
import discordfrom discord.ext import commandsclass Confirm(discord.ui.View): def __init__(self, ctx, nuke_channel): self.ctx = ctx # For your condition self.nuke_channel = nuke_channel # So you can access it in your buttons 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! {ctx.author.mention}", delete_after=0.1) await new_channel.send(f"Nuked by `{ctx.author.name}`") with open("logs.json", "a") as f: f.writelines(f"\n{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 nuke_channel.send("Aborted") button.label = "No more pressing!" # the 'button' is the button being pressed 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 = Confirm(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.")async def setup(bot): await bot.add_cog(Channels(bot))
The problem is in line 15, 16, and 18, it give me an error saying "ctx" is not defined
how can I make it defined or should I change it to interaction.author.mention
?Second problem is in line 23, it says "nuke_channel" is not defined
but shan't it be defined cuz I already have it in the Channels' class.Now, when I type .nuke #xxx and click on yes, it nukes the channel but doesn't ping me in the new channel. Instead it says:
Traceback (most recent call last): File "/home/runner/Python-Discord-Bot/.pythonlibs/lib/python3.10/site-packages/discord/ui/view.py", line 427, in _scheduled_task await item.callback(interaction) File "/home/runner/Python-Discord-Bot/cogs/admin/nuketest.py", line 15, in yes_callback await new_channel.send(f"Nuked! {ctx.author.mention}", delete_after=0.1)NameError: name 'ctx' is not defined
and when I press no, it loads for a while and says This interaction failed. Please help thx