所以我尝试让我的discord bot成为nuke命令,但只有nuke部分有效,取消部分不起作用。
我的代码:
import discord
from discord.ext import commands
from discord.ui import Button, View
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:
yes = Button(label="Yes", style=discord.ButtonStyle.green)
no = Button(label="No", style=discord.ButtonStyle.red)
async def yes_callback(interaction):
if interaction.user == ctx.author:
new_channel = await nuke_channel.clone(reason="Has been Nuked!")
await 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: {nuke_channel.name}")
async def no_callback(interaction):
if interaction.user == ctx.author:
await nuke_channel.send("Aborted")
no.label = "No more pressing!"
no.disabled = True
await interaction.response.edit_message(view=self)
yes.callback = yes_callback
no.callback = no_callback
view = View()
view.add_item(yes)
view.add_item(no)
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.")
当我点击“是”时,一切都很顺利。但是当我单击“否”时,机器人会发送一条消息“已中止”并显示
interaction failed
但按钮仍然可以单击。我的问题是,如何在按“否”后使按钮不可点击,以及如何停止使其显示interaction failed
。因为我已经添加了no.disabled = True
所以我不知道出了什么问题
这是一种混乱,这不是推荐的发表观点的方式。我会指出你代码中的错误。
特别是这一行:
await interaction.response.edit_message(view=self)
由于这不是视图类而是齿轮,因此
self
指的是齿轮而不是视图。交互失败,因为您应该将 ui.View
实例传递给 view=
kwarg,但实际上传递的是 Cog
。
现在,推荐的制作此视图的方法如下:
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)
然后,在您的命令中,您可以像这样初始化视图:
view = Confirm(ctx, nuke_channel)
更简洁的代码。