How to set a default using context with context defined in same line?

前端 未结 1 1565
灰色年华
灰色年华 2021-01-27 08:21

I am creating a discord bot, and I am trying to make it where if a member says a command with no user mentioned, it defaults to themself. It doesn\'t work inline, and I need the

相关标签:
1条回答
  • 2021-01-27 09:14

    You can mark the parameter Optional, then check if it is None in the body of the coroutine:

    from discord import Member
    from typing import Optional
    
    @bot.command(name="joined", description="Shows precisely when a member of the server joined", aliases=["entry", "jointime", "join_time"], pass_context=True)
    async def joined(ctx, member: Optional[Member]):
        """Says when a member joined."""
        member = member or ctx.author
        await ctx.send('{0.name} joined in {0.joined_at}'.format(member))
    
    0 讨论(0)
提交回复
热议问题