pythonpermissionsdiscordownernextcord

Has admin permission or is bot owner | Nextcord


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?


Solution

  • 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)).