Мой код:
Код: Выделить всё
import discord
from discord.ext import commands
class 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))
Вторая проблема в строке 23, там написано, что "nuke_channel" не определен, но разве он не должен быть определен, потому что он у меня уже есть в Channels' class.
Теперь, когда я набираю .nuke #xxx и нажимаю «Да», он уничтожает канал, но не пингует меня на новом канале. Вместо этого там написано:
Код: Выделить всё
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
Подробнее здесь: https://stackoverflow.com/questions/783 ... ally-works