【问题标题】:Trying to make bot log something when a command is used discord.py尝试在使用命令时让机器人记录一些东西 discord.py
【发布时间】:2021-08-10 18:25:57
【问题描述】:

我试图让我的机器人在使用时记录一个事件,也就是一个 mod 命令。由于我的机器人位于多个服务器中,我有一个命令,人们可以在其中设置他们希望机器人记录事件的日志通道。到目前为止,我有

@commands.command()
    @commands.has_permissions(manage_messages=True)
    async def setlogchannel(self, ctx, channel: discord.TextChannel):
      with open('logchannel.json', 'r', encoding='utf-8') as fp:
        log_channel = json.load(fp)

      try:
        log_channel[str(ctx.guild.id)] = channel.id
      except KeyError:
        new = {str(ctx.guild.id): channel.id}
        log_channel.update(new)

      await ctx.send(f"Log Channel set to: `{channel}`!")

      with open('logchannel.json', 'w', encoding='utf-8') as fpp:
        json.dump(log_channel, fpp, indent=2)

这是一个人们可以设置日志通道的命令,并且在将其放入 .json 文件时可以正常工作。但是比如说,我希望机器人在有人使用>clear 命令时进行记录。目前为止,

@commands.command()
    @commands.has_permissions(manage_messages=True)
    async def clear(self, ctx, amount=5):

      def log_channel(bot, channel):
        with open('logchannel.json', 'r') as fp:
          log_channel = json.load(fp)
          return log_channel[str(ctx.guild.id)]

      await ctx.channel.purge(limit=amount+1)
      embed=discord.Embed(title="Messages Cleared", description=f'{amount} messages were deleted!', color=0x00FFFF)
      author = ctx.message.author
      pfp = author.avatar_url
      embed.set_author(name=f"{ctx.author}", icon_url=pfp)
      await ctx.send(embed=embed)
      await asyncio.sleep(3)
      await ctx.channel.purge(limit=1)
      log_channel = discord.utils.get(ctx.guild.text_channels, id=log_channel[str(ctx.guild.id)])
      await log_channel.send(f"**{ctx.author.name}** cleared **{amount}** messages in the channel, **{ctx.channel.name}**!")

我进行了一些更改以查看是否有任何更改使其正常工作,但机器人不会将消息发送到指定的日志通道。如何获取要发送的日志消息?谢谢。

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    您请求日志通道的方式似乎是错误的。

    您实际上必须再次打开您的JSON 文件并查找log_channel[str(ctx.guild.id)

    这是一个关于它如何工作的示例:

        @commands.command()
        async def clear(self, ctx, amount=5):
            # Do what you want to do
            embed = discord.Embed(title="Test",
            description=f"{ctx.message.author} cleared {amount} messages".) # Your embed/msg
            with open('logchannel.json', 'r', encoding='utf-8') as fp:
                log_channel = json.load(fp)
    
            try:
                if log_channel:
                    log_c = ctx.guild.get_channel(log_channel[str(ctx.guild.id)])
                    await log_c.send(embed=embed)
                else:
                    await ctx.send(embed=embed)
            except (AttributeError, KeyError):
                await ctx.send(embed=embed)
    
    • 如果找不到频道,我还内置了一些 except 语句。

    【讨论】:

    • 您也可以同时处理两个异常,而不是有两个单独的except 块,只是except (AttributeError, KeyError):。另一件事是将整个 try/except 块减少一个级别,让文件打开这么长时间并不是一个好主意。很好的答案!
    • @ŁukaszKwieciński 我改变了我的代码并做了这两件事:D
    • @ŁukaszKwieciński 当然应该是这样,你是对的,可能是我这边的复制和粘贴错误,我会编辑它!
    猜你喜欢
    • 2017-10-14
    • 1970-01-01
    • 2021-06-02
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 1970-01-01
    相关资源
    最近更新 更多