尝试在使用命令时让机器人记录一些内容 discord.py

时间:2021-05-21 13:59:40

标签: python discord discord.py

我正在尝试让我的机器人在使用时记录一个事件,也就是 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}**!")

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

0 个答案:

没有答案