正在获取命令信息



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

@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)

下面是我最近写的关于错误管理的一个小答案。它将允许您创建一个日志系统。

最新更新