在我的不和谐机器人中,我创建了一个带有确认按钮的核通道命令。问题是,当我单击“否”时,“是”按钮仍然可以按下。我想要的是当我按“否”时,“是”和“否”按钮都会消失。 这是我的代码:
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.")
当然,我已经导入了所需的分子和其他所有内容,问题只是出在代码上。任何帮助将不胜感激!
要实现您想要的行为,即在确认提示中单击“否”后“是”和“否”按钮都会消失,您可以稍微修改您的代码。目前,您仅在单击“否”按钮时禁用它,但您还需要禁用“是”按钮。
在 nukeConfirm 类的 no_callback 方法中,循环迭代视图中的所有子项(按钮)并禁用除单击的按钮之外的它们。这可确保单击“否”时禁用“是”和“否”按钮。
进行这些更改后,单击“否”现在应该会按照您的预期禁用确认提示中的两个按钮。
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.")