Karan
Karan

Reputation: 498

Getting the command information

Is there any way, I can get what command is being used at the time of the following event:

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

I need it for statistics purpose, I have searched the library but failed.

Upvotes: 0

Views: 115

Answers (2)

MrSpaar
MrSpaar

Reputation: 3994

As the documentation says, on_command events have a ctx argument. Every Context object have a command attribute, which is a commands.Command object:

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

However, if you want to count only commands that were successfuly invoked, you can use the on_command_completion event:

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

Combined with on_command_error, you'll be able to know what command users find hard to invoke:

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

Here's a small answer that I wrote recently about error management. It would allow you to create a log system.

Upvotes: 2

ravexu
ravexu

Reputation: 665

Yes on_command(context) takes the context argument. And the context has a .command attribute which gives you the command name.

In code it would look like this:

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

You can read more about everything the context argument contains here: https://discordpy.readthedocs.io/en/latest/ext/commands/api.html?#discord.ext.commands.Context

Upvotes: 2

Related Questions