Search code examples
discord.pycommandpython-3.10

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!


Solution

  • 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