具有管理员权限或是机器人程序所有者|Nextcord



如果用户具有管理员权限或是机器人程序所有者,我想执行该命令。

我有

@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_anyCCD_ 2。

相关内容

最新更新