Discord.py Logging deleted and edited messages

Question:

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)
Asked By: Unknown_Shell

||

Answers:

You can use on_message_delete and on_message_edit as you are using and then you should give the function message not ctx.

Example 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(embed=embed)

Example 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(embed=embed)
Answered By: Ahmed Khaled

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. It also does not work because you can not set the colour to Blue or a string without integer conversion. The best way to do this is by just defining a hex like this enter color=0xFF0000 this makes it red.

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
Answered By: bean
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.