Discord 机器人命令日志

Discord Bot Command log

我有一个 Discord Bot,我试图通过跟踪命令使用来跟踪 bot 的使用情况。我的代码如下。我 运行 遇到了我似乎无法获取命令名称的问题。 这是代码:

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

当 运行 一个命令(任何命令)时它说“缺少必需的 arg 'command'” 我也尝试过其他代码。我试过的其他代码:

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

这只会发送除命令之外的所有内容。代替命令的是发送一个看起来像十六进制代码的东西(0x____)。我错过了什么?我可以尝试什么?

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

您收到此错误是因为 API documentation for on_command 声明它只有 1 个参数,即 ctx,因此 Discord 将 永远不会 传递任何其他参数进入其中,因此您的 command 参数将始终丢失。

Context 的文档说您可以使用 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____)

这是 Context 实例的字符串表示。因为你只发送 command 而你什么都不做,并且看到它实际上是 Context 而不是我上面解释的 Command ,这就是结果。

我想要类似的东西,并通过反复试验得到了这个,其中 msg_dump_channel 是您要将输出打印到的频道的 ID。

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