discord.py v2中如何为App Command设置管理员或特定角色权限?
嘿,我完全懂你的困扰——discord.py里的@commands.has_permissions确实是给传统前缀命令设计的,对App Command(也就是斜杠命令)根本不起作用,毕竟这两套命令系统的权限校验逻辑是分开的。不过discord.py v2已经为App Command提供了专门的权限控制方案,下面给你两种实用的解决办法,直接适配你的代码:
方案一:自定义权限检查(灵活支持管理员/特定角色)
如果你的需求是同时允许管理员或特定角色使用命令,这个方案最适合,逻辑完全自定义。
步骤1:写一个异步检查函数
这个函数会接收interaction参数,判断用户是否符合权限要求:
async def is_admin_or_allowed_role(interaction: discord.Interaction) -> bool: # 先检查用户是否是服务器管理员 if interaction.user.guild_permissions.administrator: return True # 再检查用户是否拥有指定角色(把下面的ID换成你需要的角色ID) allowed_role_id = 123456789012345678 allowed_role = interaction.guild.get_role(allowed_role_id) return allowed_role in interaction.user.roles
步骤2:替换原装饰器
把你代码里的@commands.has_permissions(administrator=True)换成@app_commands.check(is_admin_or_allowed_role)即可:
@app_commands.command(name="embed_message", description="Create a embed message") @app_commands.describe( title="The title of your message", message="Your message", color="Embed color", image="Add an image to your message (with image's url : http(s)://...)", url="Add an url to your message (http(s)://....)" ) @app_commands.choices(color=[Choice(name="🔴 Red", value=0xff0000), Choice(name="🔵 Blue", value=0x0000ff), Choice(name="🟢 Green", value=0x00ff00), Choice(name="🟣 Purple", value=0x883af1), Choice(name="🟡 Yellow", value=0xffe34d), Choice(name="🟠 Orange", value=0xff8000), Choice(name="🟤 Brown", value=0x845321), Choice(name="⚫️ Black", value=0xffffff), Choice(name="⚪️ White", value=0x000000), ]) # 替换成自定义权限检查装饰器 @app_commands.check(is_admin_or_allowed_role) async def embed_message(self, interaction: discord.Interaction, title:str="", message:str="", image:str="", url:str="", color:int=0xff0000): await interaction.response.defer() if title!="" or message!="": embed = discord.Embed(title = title, description = message, color = color) if image!="": embed.set_thumbnail(url=image) if url!="": embed.url=url await interaction.followup.send(embed=embed) else: await interaction.followup.send("You must enter at least one title or message", ephemeral=True)
可选:添加权限不足提示
为了让用户体验更好,可以给命令加一个错误处理函数,当权限校验失败时发送提示:
@embed_message.error async def embed_message_error(self, interaction: discord.Interaction, error: app_commands.AppCommandError): if isinstance(error, app_commands.CheckFailure): await interaction.response.send_message("你没有权限使用这个命令!", ephemeral=True)
方案二:使用默认权限装饰器(仅针对管理员/内置权限)
如果你的需求只是限制管理员使用命令,这个方案更简单,不需要自定义函数:
直接用@app_commands.default_permissions()装饰器替换原有的前缀命令权限装饰器即可:
@app_commands.command(name="embed_message", description="Create a embed message") @app_commands.describe( title="The title of your message", message="Your message", color="Embed color", image="Add an image to your message (with image's url : http(s)://...)", url="Add an url to your message (http(s)://....)" ) @app_commands.choices(color=[Choice(name="🔴 Red", value=0xff0000), Choice(name="🔵 Blue", value=0x0000ff), Choice(name="🟢 Green", value=0x00ff00), Choice(name="🟣 Purple", value=0x883af1), Choice(name="🟡 Yellow", value=0xffe34d), Choice(name="🟠 Orange", value=0xff8000), Choice(name="🟤 Brown", value=0x845321), Choice(name="⚫️ Black", value=0xffffff), Choice(name="⚪️ White", value=0x000000), ]) # 直接设置默认需要管理员权限 @app_commands.default_permissions(administrator=True) async def embed_message(self, interaction: discord.Interaction, title:str="", message:str="", image:str="", url:str="", color:int=0xff0000): # 原有代码不变
这个装饰器会告诉Discord:只有拥有管理员权限的用户才能看到并使用这个命令,不需要额外的逻辑处理。
内容的提问来源于stack exchange,提问作者Maxistyle
相关产品推荐
相关产品推荐

