如何限制仅命令作者使用Discord帮助选择菜单并添加时限
解决方案:限制仅命令调用者操作选择菜单并添加时限
针对你的需求,我们可以通过两个关键修改实现目标:
1. 限制仅/help命令的调用者可操作菜单
在DropdownView中保存命令调用者的用户ID,通过重写interaction_check方法,全局验证操作菜单的用户是否为原调用者。
2. 添加菜单使用时限
初始化View时设置timeout参数,并重写on_timeout方法,在超时后自动禁用菜单并更新提示。
修改后的完整代码
class Dropdown(discord.ui.Select): def __init__(self): options = [ discord.SelectOption(label='General', value="1", description='Provides you with general commands and information about them.', emoji='1️⃣'), discord.SelectOption(label='Moderation', value="2", description='Provides you with moderation commands and information about them.', emoji='2️⃣'), ] super().__init__(placeholder='Choose what type of help you need below.', min_values=1, max_values=1, options=options) async def callback(self, interaction: discord.Interaction): generalEmbed=discord.Embed(title="General Commands", description="Everyone can use these commands, abusing them will result in punishment.", color=0xFF5349) generalEmbed.add_field(name="``/help``", value="Provides you with this embed.", inline=False) generalEmbed.add_field(name="``/suggest``", value="Sends a suggestion to <#1059976520376012811>.", inline=False) generalEmbed.set_footer(text="Made by justin;#6868") if self.values[0]=="1": await interaction.response.edit_message(embed=generalEmbed) moderationEmbed=discord.Embed(title="Moderation Commands", description="Staff with the correct permissions can use these commands. The following commands will be logged.", color=0xFF5349) moderationEmbed.add_field(name="``/purge``", value="Purges the specified amount of messages.", inline=False) moderationEmbed.add_field(name="``/kick``", value="Kicks the specified user with the reason.", inline=False) moderationEmbed.add_field(name="``/ban``", value="Bans the specified user with the reason.", inline=False) moderationEmbed.add_field(name="``/unban``", value="Unbans the specified user with the reason.", inline=False) moderationEmbed.add_field(name="``/mute``", value="Mutes the specified user with the reason and amount of time.", inline=False) moderationEmbed.add_field(name="``/unmute``", value="Unmutes the specified user with the reason.", inline=False) moderationEmbed.set_footer(text="Made by justin;#6868") if self.values[0]=="2": await interaction.response.edit_message(embed=moderationEmbed) class DropdownView(discord.ui.View): def __init__(self, author_id: int, timeout: float = 300.0): # timeout设置为300秒(5分钟),可根据需求调整 super().__init__(timeout=timeout) self.author_id = author_id self.add_item(Dropdown()) async def interaction_check(self, interaction: discord.Interaction) -> bool: # 验证操作用户是否为命令调用者 if interaction.user.id != self.author_id: await interaction.response.send_message("你无权操作这个菜单!", ephemeral=True) return False return True async def on_timeout(self) -> None: # 超时后禁用所有组件并更新消息提示 for item in self.children: item.disabled = True # 获取原消息并更新 if self.message: embed = self.message.embeds[0] embed.description = "帮助菜单已超时,无法继续操作。如需帮助请重新调用`/help`命令。" await self.message.edit(embed=embed, view=self) class Bot(commands.Bot): def __init__(self): intents = discord.Intents.default() intents.message_content = True super().__init__(command_prefix=commands.when_mentioned_or('k!'), intents=intents) async def on_ready(self): print('hi') bot = Bot() @bot.tree.command(name="help", description="Provides you with a help embed.") async def _help(interaction): embed=discord.Embed(title="Help Menu", description="To find the help you're looking for, look below in this embed for whatever you need help with!", color=0xFF5349) embed.set_footer(text="Made by justin;#6868") # 传入调用者ID给View view = DropdownView(author_id=interaction.user.id) # 发送消息后保存消息引用到View,方便超时后编辑 message = await interaction.response.send_message(embed=embed, view=view, ephemeral=True) view.message = await message.original_response()
关键修改说明
- 权限验证:在
DropdownView的interaction_check方法中,对比操作用户ID与命令调用者ID,非调用者操作时返回错误提示并拒绝执行。 - 时限设置:初始化
DropdownView时传入timeout参数(单位秒),超时后触发on_timeout方法,禁用所有组件并更新消息提示。 - 消息引用保存:发送消息后获取原消息对象并赋值给
view.message,确保超时后能正确编辑消息。
内容的提问来源于stack exchange,提问作者Justin
相关产品推荐
相关产品推荐

