【问题标题】:Discord Bot Command logDiscord Bot 命令日志
【发布时间】:2021-03-07 05:14:32
【问题描述】:

我有一个 Discord 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____)。我错过了什么?我可以尝试什么?

【问题讨论】:

    标签: python python-3.x discord discord.py discord.py-rewrite


    【解决方案1】:

    运行命令(任何命令)时,它会显示“缺少必需的 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}')
    

    代替命令的是发送一个看起来像十六进制代码的东西(0x____)

    这是Context 实例的字符串表示形式。由于您只发送 command 而您什么也没做,并且看到它实际上是 Context 而不是我上面解释的 Command,这就是结果。

    【讨论】:

      【解决方案2】:

      我想要类似的东西,并通过反复试验得到了这个,其中 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))
      

      【讨论】:

        猜你喜欢
        • 2018-03-04
        • 1970-01-01
        • 1970-01-01
        • 2021-10-30
        • 2020-10-25
        • 2018-10-21
        • 2021-03-01
        • 2021-04-11
        • 2021-01-21
        相关资源
        最近更新 更多