Discord.py记录已删除和已编辑的消息

时间:2020-07-30 23:32:44

标签: python python-3.x discord bots

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

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

2 个答案:

答案 0 :(得分:3)

您可以在使用on_message_deleteon_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)

答案 1 :(得分:0)

在发布此帖子时,上面显示的方式将不起作用。这就是为什么我编辑它以便它会。

行:

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

我希望这有帮助。 谢谢, 豆 |领导开发 @ Alle Group。