ItzCook1e
ItzCook1e

Reputation: 13

Discord Bot Command log

I have a Discord Bot where I am trying to track the usage of the bot by tracking command usage. The code I have is below. I am running into the problem where I cant seem to get the command name. Here is the code:

    @commands.Cog.listener(name='on_command')
    async def print(self, ctx, command):
        server = ctx.guild.name
        user = command.author
        command = ctx.command
        print(f'{server} > {user} > {command}')

When running a command (any command) it says "missing required arg 'command'" I have also tried other code. The other code I have tried:

    @commands.Cog.listener(name='on_command')
    async def print(self, command, server=None, user=None):
        server = command.guild.name
        user = command.author
        print(f'{server} > {user} > {command}')

This just sends everything except for the command. In place of the command is sends a hex code looking thing (0x____). What am I missing? What can I try?

Upvotes: 0

Views: 3719

Answers (2)

norinhara
norinhara

Reputation: 568

I wanted something similar and got to this through trial and error, where msg_dump_channel is the id of the channel you want to print your output to.

from discord.ext import commands
bot = commands.Bot(command_prefix = '>')
@bot.event
async def on_command(ctx):
    channel = bot.get_channel(msg_dump_channel)
    server = ctx.guild.name
    user = ctx.author
    command = ctx.command
await channel.send('{} used {} in {}'.format(user, command, server))

Upvotes: 0

stijndcl
stijndcl

Reputation: 5647

When running a command (any command) it says "missing required arg 'command'"

You're getting this error because the API documentation for on_command states that it only has 1 parameter, being ctx, so Discord will never pass anything else into it, so your command parameter will always be missing.

The docs for Context say you can get the command using ctx.command.

@commands.Cog.listener(name='on_command')
async def print(self, ctx):
    server = ctx.guild.name
    user = ctx.author
    command = ctx.command
    print(f'{server} > {user} > {command}')

In place of the command is sends a hex code looking thing (0x____)

That's the string representation of a Context instance. As you're only sending command and you're doing nothing with it, and seeing as it's actually Context instead of a Command as I explained above, this will be what comes out.

Upvotes: 0

Related Questions