【问题标题】:Getting the command information获取命令信息
【发布时间】:2020-07-23 20:24:27
【问题描述】:

有什么办法,我可以得到以下事件发生时正在使用的命令:

@bot.event
async def on_command(command):
    print(command)

我需要它用于统计目的,我已经搜索了图书馆但失败了。

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    是的 on_command(context) 接受上下文参数。上下文有一个.command 属性,它为您提供命令名称。

    在代码中它看起来像这样:

    @bot.event
    async def on_command(context):
        print(context.command)
    

    您可以在此处阅读有关 context 参数包含的所有内容的更多信息:https://discordpy.readthedocs.io/en/latest/ext/commands/api.html?#discord.ext.commands.Context

    【讨论】:

    • 哇,谢谢,亲爱的,我迷路了。 command.command 有效;)
    【解决方案2】:

    正如文档所述,on_command 事件有一个 ctx 参数。每个Context 对象都有一个command 属性,即commands.Command 对象:

    @bot.event
    async def on_command(ctx):
        print(ctx.command)
    

    但是,如果您只想计算成功调用的命令,您可以使用on_command_completion 事件:

    @bot.event
    async def on_command_completion(ctx):
        print(ctx.command)
    

    结合on_command_error,您将能够知道用户难以调用的命令:

    @bot.event
    async def on_command_error(ctx, error):
        print(ctx.command.name)
        print(error)
    

    这是我最近写的关于error management 的小回答。它将允许您创建一个日志系统。

    【讨论】:

      猜你喜欢
      • 2011-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-22
      • 2013-03-10
      相关资源
      最近更新 更多