有什么办法吗,我可以在以下事件发生时得到正在使用的命令:
@bot.event
async def on_command(command):
print(command)
我需要它用于统计目的,我已经搜索了图书馆,但没有成功。
Yes on_command(context(接受context参数。上下文有一个.command
属性,它为您提供了命令名称。
在代码中,它看起来像这样:
@bot.event
async def on_command(context):
print(context.command)
您可以在此处阅读有关上下文参数所包含内容的更多信息: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)
下面是我最近写的关于错误管理的一个小答案。它将允许您创建一个日志系统。