【发布时间】:2020-07-23 20:24:27
【问题描述】:
有什么办法,我可以得到以下事件发生时正在使用的命令:
@bot.event
async def on_command(command):
print(command)
我需要它用于统计目的,我已经搜索了图书馆但失败了。
【问题讨论】:
标签: discord.py
有什么办法,我可以得到以下事件发生时正在使用的命令:
@bot.event
async def on_command(command):
print(command)
我需要它用于统计目的,我已经搜索了图书馆但失败了。
【问题讨论】:
标签: discord.py
是的 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
【讨论】:
正如文档所述,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 的小回答。它将允许您创建一个日志系统。
【讨论】: