拥有管理员权限或者是机器人所有者 |下一线
如果用户具有管理员权限或者是机器人所有者,我想执行该命令。
如果用户有
@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)
我可以删除装饰器并在函数内部手动检查,但是这真的很混乱,因为我想在我的机器人的所有命令中实现这一点。
我该怎么做?
I want to execute the command if the user has admin permission OR is the bot owner.
I have
@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)
...............
The above code works fine if the user has admin but fails if the user is bot owner and doesnt has admin, I tried below:
@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)
...............
The above code gave me a syntax error:
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)
I could remove the decorators and check manually inside the function but that would be really messy as I want to implement this in ALL commands for my bot.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过更多研究文档后,我得到了答案。
您可以使用 <代码>check_any就像
@commands.check_any(commands.is_owner(),commands.has_permission(administrator=True))
。After some more looking into docs, I got the answer.
You can use
check_any
like@commands.check_any(commands.is_owner(), commands.has_permission(administrator=True))
.