want to see who deleted the message on_message_delete

Question:

when somenone delets a message, it returns the author name, not who deleted the message. what should i change ?

@client.event
async def on_message_delete(message):
    if "discord.gg" not in message.content and "https://discord.gg/" not in message.content and "discord.gg/" not in message.content:
        log_channel = client.get_channel(N/A)
        embed = discord.Embed(
            title="Message Deleted",
            description=f"Message sent by {message.author.name} has been deleted:n{message.content}",
            color=discord.Color.red()
        )
        embed.set_footer(text="N/A", icon_url="N/A")
        deletor = None
        if message.guild:
            deletor = message.guild.get_member(message.author.id)
        else:
            deletor = client.get_user(message.author.id)
        if deletor:
            embed.add_field(name="Deleted By", value=deletor.name, inline=False)
        await log_channel.send(embed=embed)

i tried to use author.deleted and deleted.user.id even this

tried even to use {message.author}

@client.event
async def on_message_delete(message):
    if "discord.gg" not in message.content and "https://discord.gg/" not in message.content and "discord.gg/" not in message.content:
        log_channel = client.get_channel(N/A)
        embed = discord.Embed(
            title="Message Deleted",
            description=f"Message sent by **{message.author.name}** has been deleted by: **{message.author}**nnDeleted message:n```{message.content}```",
            color=discord.Color.red()
        )
        embed.set_footer(text="N/A", icon_url="N/A")
        #embed.add_field(name=f"Deleted By {message.author}", inline=False)
        await log_channel.send(embed=embed)
Asked By: GFT

||

Answers:

I don’t think you’ll be able to do what you want with the on_message_delete event. The docs only have it having the discord.Message object as a parameter and even the discord gateway events doc only show that the event has the message ID, channel ID, and the guild ID.

The only way I can think of doing it is using the Audit Logs. Perhaps iterating through delete/bulk delete entries and finding the message that was deleted in there and trying to get the author information out of it. However, this will only work if someone else deleted someone’s message – there won’t be an audit log entry if an individual deleted their own message. But if you go the audit log iterating route and can’t find an entry in the last minute or so then you can assume the individual was the one who deleted it.

Iterating over the audit log data gives you a series of:

  • AuditLogAction. This has a user property which is the user that did the action (ie, deleted the message in your case)
  • The above action has also a action property which will tell you what kind of action it was – ie, in your case, a message_delete action.
  • The action object also has a target parameter which should be the message that was deleted – you might be able to retrieve the message ID from that to check if it’s the same as the one that got deleted in on_message_delete.

Hopefully that gives you something to go off if you really want to get the user that deleted a message in on_message_delete.

EDIT: Some example code to get you started:

@client.event
async def on_message_delete(message: discord.Message):
    now = datetime.datetime.now()
    guild = message.guild
    # check logs to see who deleted it
    after = now - datetime.timedelta(minutes=1)

    target = None
    async for entry in guild.audit_logs(after=after, oldest_first=False, action=discord.AuditLogAction.message_delete):
        # there's no way to know for certain if the entry is referring to the same message as us
        # it could go either way
        if entry.extra.channel.id != message.channel.id:
            # definitely not our message - skip
            continue
        if entry.target.id != message.author.id:
            # definitely not our message - skip
            continue
        target = entry.user
        break

    if not target:
        # couldn't find a "matching" audit log entry - deleted by message author
        target = message.author
    print(f"Message deleted by {target.name} {target.id}")

A few caveats:

  • on_message_delete only works for messages in the bot’s cache – you’ll have to look on_raw_message_delete for others
  • similarly, there are methods/events for bulk deleting too
  • the audit log entry doesn’t have the message ID of the message deleted – so we’re doing the checks we can and then assuming that the entry is for the message that just got deleted (it is pretty likely)
Answered By: ESloman

done 🙂 thanks for helping
final code

@client.event
async def on_message_delete(message):
    if "discord.gg" not in message.content and "https://discord.gg/" not in message.content and "discord.gg/" not in message.content:
        log_channel = client.get_channel(N/A)
        deletor = None
        async for entry in message.guild.audit_logs(limit=1, action=discord.AuditLogAction.message_delete):
            deletor = entry.user
        embed = discord.Embed(
            title="Message Deleted",
            description=f"Message sent by **{message.author.mention}** in {message.channel.mention} has been deleted:nn```{message.content}```",
            color=discord.Color.red()
        )
        embed.set_footer(text="N/A", icon_url="N/A")
        if deletor:
            embed.add_field(name="Deleted By", value=f"{deletor.mention}", inline=False)
        await log_channel.send(embed=embed)
Answered By: GFT
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.