Random_Pythoneer59
Random_Pythoneer59

Reputation: 728

How to pass the user invoking command as default parameter in discord.py bot.command?

I am trying to make a rank based system for my discord.py bot.

I want to implement a rank command which returns the rank of the specified user. By default I want the specified user to be the one invoking the command, i.e. ctx.message.author.

async def rank(self, ctx, user: discord.Member=ctx.message.author):

I know this is invalid syntax, but how could I achieve such a thing?

My discord.py version is 1.7.3.

Thanks!

Upvotes: 1

Views: 390

Answers (2)

SimplySerious
SimplySerious

Reputation: 11

You can make use of Lambda Expressions in Python:

async def rank(self, ctx, user: discord.Member=lambda ctx:ctx.message.author):

Documentation

Upvotes: 0

tk421
tk421

Reputation: 281

I would make user default to None (using typing.Optional) and replace it with ctx.message.author inside the command.

e.g

async def rank(self, ctx, user: typing.Optional[discord.Member]=None):
    if not user:
        user = ctx.message.author

Upvotes: 2

Related Questions