拥有管理员权限或者是机器人所有者 |下线

Has admin permission or is bot owner | Nextcord

如果用户具有管理员权限或者是 bot 所有者,我想执行命令。

我有

@commands.command(brief = "Deletes all roles in guild", description = "Deletes all roles in guild")
@commands.has_permissions(administrator=True)
async def nuke_roles(self, ctx):
    view = Confirm()
    await ctx.send("NOTE: The bot's role should be on top to cause maximum damage!")
    await ctx.send("Are you sure that you want to delete all roles in this guild?", view=view)
...............

如果用户有管理员,上面的代码可以正常工作,但如果用户是机器人所有者并且没有管理员,上面的代码就会失败,我在下面尝试过:

    @commands.has_permissions(administrator=True) or @commands.is_owner()
    async def nuke_roles(self, ctx):
        view = Confirm()
        await ctx.send("NOTE: The bot's role should be on top to cause maximum damage!")
        await ctx.send("Are you sure that you want to delete all roles in this guild?", view=view)
...............

上面的代码给我一个语法错误:

Traceback (most recent call last):
  File "/home/crowbar/.local/lib/python3.10/site-packages/nextcord/ext/commands/bot.py", line 679, in _load_from_module_spec
    spec.loader.exec_module(lib)  # type: ignore
  File "<frozen importlib._bootstrap_external>", line 879, in exec_module
  File "<frozen importlib._bootstrap_external>", line 1017, in get_code
  File "<frozen importlib._bootstrap_external>", line 947, in source_to_code
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/crowbar/projects/pengoon/src/cogs/danger.py", line 35
    @commands.has_permissions(administrator=True) or @commands.is_owner()
                                                     ^
SyntaxError: invalid syntax

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/crowbar/projects/pengoon/src/bot.py", line 53, in <module>
    bot.load_extension(f"cogs.{name}")
  File "/home/crowbar/.local/lib/python3.10/site-packages/nextcord/ext/commands/bot.py", line 793, in load_extension
    self._load_from_module_spec(spec, name, extras=extras)
  File "/home/crowbar/.local/lib/python3.10/site-packages/nextcord/ext/commands/bot.py", line 682, in _load_from_module_spec
    raise errors.ExtensionFailed(key, e) from e
nextcord.ext.commands.errors.ExtensionFailed: Extension 'cogs.danger' raised an error: SyntaxError: invalid syntax (danger.py, line 35)

我可以删除装饰器并在函数内部手动检查,但这真的很麻烦,因为我想在我的机器人的所有命令中实现它。

我该怎么做?

进一步查看文档后,我得到了答案。

您可以使用 check_any 之类的 @commands.check_any(commands.is_owner(), commands.has_permission(administrator=True)).