【问题标题】:Discord.py Logging deleted and edited messagesDiscord.py 记录删除和编辑的消息
【发布时间】:2020-11-20 18:28:42
【问题描述】:

我正在为我的服务器制作一个服务器机器人,我想记录所有消息删除和编辑。它将登录到一个日志通道供工作人员查看。在日志通道中,我想让消息显示删除的内容或消息编辑之前的内容以及消息编辑之后的内容。我将如何让机器人显示已删除或已编辑的消息?

@client.event()
async def on_message_delete(ctx):
    embed=discord.Embed(title="{} deleted a message".format(member.name), description="", color="Blue")
    embed.add_field(name="What the message was goes here" ,value="", inline=True)
    channel=client.get_channel(channel_id)

    await channel.send(channel, embed=embed)

【问题讨论】:

    标签: python python-3.x discord bots


    【解决方案1】:

    上面显示的方式在发布这篇文章时不起作用。这就是我编辑它的原因。

    行:

    embed=discord.Embed(title="{} edited a 
        message".format(message_before.member.name), 
        description="", color="Blue")
    

    它不起作用,因为message 没有属性member。它也不起作用,因为您不能将颜色设置为 Blue 或没有整数转换的字符串。最好的方法是定义一个像这样的十六进制输入 color=0xFF0000 这使它变成红色。

    整行已更改:

    embed = discord.Embed(title="{} deleted a message".format(message.author.name),
                          description="", color=0xFF0000)
    

    以下是完整的两个编辑命令。

    @client.event
    async def on_message_delete(message):
        embed = discord.Embed(title="{} deleted a message".format(message.author.name),
                              description="", color=0xFF0000)
        embed.add_field(name=message.content, value="This is the message that he has deleted",
                        inline=True)
        channel = client.get_channel(channelid)
        await channel.send(channel, embed=embed)
    
    
    @client.event
    async def on_message_edit(message_before, message_after):
        embed = discord.Embed(title="{} edited a message".format(message_before.author.name),
                              description="", color=0xFF0000)
        embed.add_field(name=message_before.content, value="This is the message before any edit",
                        inline=True)
        embed.add_field(name=message_after.content, value="This is the message after the edit",
                        inline=True)
        channel = client.get_channel(channelid)
        await channel.send(channel, embed=embed)
    

    我会在您的代码顶部定义您想要使用的频道。

    logging_channel = channelID
    # then at your two commands do this:
    global logging_channel
    

    【讨论】:

      【解决方案2】:

      您可以在使用时使用on_message_delete 和on_message_edit,然后您应该给出函数消息而不是ctx。

      例如on_message_delete:

      @client.event()
      async def on_message_delete(message):
          embed=discord.Embed(title="{} deleted a message".format(message.member.name), 
          description="", color="Blue")
          embed.add_field(name= message.content ,value="This is the message that he has 
          deleted", 
          inline=True)
          channel=client.get_channel(channel_id)
      await channel.send(channel, embed=embed)
      

      例如on_message_edit:

      @client.event()
      async def on_message_edit(message_before, message_after):
          embed=discord.Embed(title="{} edited a 
          message".format(message_before.member.name), 
          description="", color="Blue")
          embed.add_field(name= message_before.content ,value="This is the message before 
          any edit", 
          inline=True)
          embed.add_field(name= message_after.content ,value="This is the message after the 
          edit", 
          inline=True)
          channel=client.get_channel(channel_id)
      await channel.send(channel, embed=embed)
      

      【讨论】:

      • 这是大错特错。你有错误的语法。在使用 event 时,我们不使用括号。另外要发送消息,您不使用该命令。是await channel.send(embed=embed)
      • 这是很久以前的事了,所以版本可能已经改变,我 100% 确定的是你可以 100% 在事件中使用括号。我很确定这在当时有效,可能仍然有效
      • 不。他们没有。我们得到了一个错误。
      • 正如我所提到的,那是从某个美好的时光开始的,所以它可能正在使用旧版本,因此为什么这个人将其标记为正确并获得了 3 个赞成票,我会为当前版本修复它无论如何,ty为我提到这一点
      • @BhavyadeepYadav 你真的只是在一年后评论这个标记为“有用且正确”的解决方案是错误的吗?一年中发生了很多事情,新的discord.py 版本发布,然后事情可能会发生变化。无论如何:这里不需要调整,因为当时一切正常且正确。
      猜你喜欢
      • 2021-08-17
      • 2021-04-04
      • 1970-01-01
      • 1970-01-01
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      • 2018-06-29
      • 2021-09-08
      相关资源
      最近更新 更多