简体   繁体   English

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

[英]Discord.py Logging deleted and edited messages

I'm making a server bot for my server and I want to log all message deletions and edits.我正在为我的服务器制作一个服务器机器人,我想记录所有消息删除和编辑。 It will log into a log channel for staff to see.它将登录到一个日志通道供工作人员查看。 In the log channel, I want to have the message show what was deleted or what was before the message edit and what was after the edit of the message.在日志频道中,我想让消息显示删除的内容或消息编辑之前的内容以及消息编辑之后的内容。 How would I have to bot display the deleted or edited message?我将如何让机器人显示已删除或已编辑的消息?

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

You can use on_message_delete and on_message_edit as you are using and then you should give the function message not ctx.您可以在使用时使用on_message_deleteon_message_edit ,然后您应该给出 function 消息而不是 ctx。

Example on_message_delete :示例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)

Example on_message_edit :示例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)

The way shown above will not work at the time of making this post.上面显示的方式在发表这篇文章时不起作用。 That's why I have edited it so that It will.这就是为什么我已经编辑它,所以它会。

The line:该行:

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

It does not work as message does not have the attribute member .它不起作用,因为message没有属性member It also does not work because you can not set the colour to Blue or a string without integer conversion.它也不起作用,因为您无法将颜色设置为Blue或没有 integer 转换的字符串。 The best way to do this is by just defining a hex like this enter color=0xFF0000 this makes it red.最好的方法是定义一个像这样的十六进制输入color=0xFF0000这使它变成红色。

The full line changed:整行更改:

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

Here are the full two commands edited to work.以下是完整的两个编辑命令。

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

I would at the top of your code define the channel you would like to use something like this.我会在你的代码顶部定义你想使用这样的频道。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM