【问题标题】:How to pass the user invoking command as default parameter in discord.py bot.command?如何在 discord.py bot.command 中将用户调用命令作为默认参数传递?
【发布时间】:2021-12-18 01:15:05
【问题描述】:

我正在尝试为我的 discord.py 机器人创建一个基于排名的系统。

我想实现一个返回指定用户排名的排名命令。默认情况下,我希望指定的用户是调用命令的用户,即ctx.message.author

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

我知道这是无效的语法,但我怎么能做到呢?

我的 discord.py 版本是1.7.3

谢谢!

【问题讨论】:

    标签: discord.py command python-3.10


    【解决方案1】:

    我会将 user 默认设置为 None(使用 typing.Optional)并在命令中将其替换为 ctx.message.author。

    例如

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-08
      • 1970-01-01
      • 2020-09-16
      • 2021-09-11
      • 1970-01-01
      • 2020-08-04
      • 1970-01-01
      • 2011-12-27
      相关资源
      最近更新 更多