B
B
Bibarsest2021-10-23 22:38:43
Python
Bibarsest, 2021-10-23 22:38:43

Python. Is it possible to execute one of several decorators in discord.py?

I want the command for the bot to work if the author of the command either has administrator rights or is the owner of the bot. Simple code that works without "or @commands.is_owner()" on the second line:

@client.command()
@commands.has_permissions(administrator = True) or @commands.is_owner()
async def очисти(ctx, amount = 2, message = None):
  await ctx.channel.purge(limit = amount)

Mistake:
@commands.has_permissions(administrator = True) or @commands.is_owner()
                                                   ^
SyntaxError: invalid syntax

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-10-23
@Bibarsest

Well, firstly, if you had any idea what a decorator is and how it works, then the idea with or would not have occurred to you ...
And if you had dug into the documentation, you would have found commands.check()

def check_if_it_is_me(ctx):
    return ctx.message.author.id == 85309593344815104

@bot.command()
@commands.check(check_if_it_is_me)
async def only_for_me(ctx):
    await ctx.send('I know you!')

Obviously, inside the predicate (in the example it is called check_if_it_is_me) you can place a variety of check logic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question