2

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)

2 Answers 2

3

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)
8
  • 1
    This is so wrong. You have wrong syntaxes. While using event we don't use parentheses. Also to send message you don't use that command. It is await channel.send(embed=embed) Commented May 13, 2021 at 12:28
  • This was a long time ago so probably the versions have changed, what I am 100% sure about Is that you can 100% use parentheses on events. and i am pretty sure that this was working back then, probably still working anyways Commented May 14, 2021 at 15:32
  • Nope. They don't. We get an error instead. Commented May 14, 2021 at 16:09
  • As I mentioned that was from some good time so it probably was working on the old version, hence why the guy marked it as correct and it got 3 upvotes, I will look to fix it for current versions anyways, ty for mentioning that for me Commented May 15, 2021 at 21:54
  • @BhavyadeepYadav Did you really just comment after a year that this solution marked as "helpful and correct" is wrong? A lot happens in a year, new discord.py versions are released and then things can just change. Anyway: Nothing needs to be adjusted here, because at that time everything worked and was correct.
    – Dominik
    Commented May 17, 2021 at 13:48
1

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
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.