Log person who deleted message in Discord.py bot

I’m logging deleted Discord messages using the on_message_delete event like so:

    async def on_message_delete(self, message):
        if not message.author.bot:
            channel = db.field("SELECT LogID FROM guilds WHERE GuildID = ?", message.guild.id)

            if channel != 0: #If this guild wants to log deleted messages

                embed = discord.Embed(title="Message Deleted",
                embed.add_field(name="Member: ", value=message.author.mention, inline=False)
                embed.add_field(name="Deleter: ", value="What do I put here", inline=False)
                embed.set_footer(text=f"User ID: {message.author.id}")
                fields = [("Message:", message.content, True),
                          ("Channel:", message.channel.mention, False)]

                for name, value, inline in fields:
                    embed.add_field(name=name, value=value, inline=inline)

                await self.bot.get_channel(channel).send(embed=embed)

Is there a way to get the id of a person who deleted a message so that even if a mod deletes someone else’s message, you could still get their id?

(I have looked at other sources as well like this Reddit page, and the API for messages but neither gave any insight)


I was finally able to log them given @yotam rec’s comment about using Discord Audit Logs.

I did it by:

  1. Reusing my on_message_delete listener
    async def on_message_delete(self, message):
        if not message.author.bot:
  1. Creating a Boolean variable to ensure that I won’t post the same embed twice
modDeleted = False
  1. Checking if a message delete audit log event was filed at the same time as this function
async for entry in message.guild.audit_logs(limit=1, action=discord.AuditLogAction.message_delete):
    if entry.created_at.now(timezone("US/Eastern")) == getTime(): #Custom time function
        modDeleted = True
        embed = discord.Embed(title="Message Deleted By Mod")
        embed.add_field(name="Member: ", value = message.author.mention, inline=True)
        embed.add_field(name = "Mod: ", value = entry.user.mention, inline=True)
        embed.add_field(name = "Message: ", value = message.content, inline=False)
        embed.add_field(name = "Channel: ", value = message.channel.mention, inline=False)

        await self.bot.get_channel(channel).send(embed=embed)
  1. Checking if it couldn’t find it in the audit log, and if it didn’t, created another embed knowing that a user deleted their own message
if modDeleted == False:

    embed = discord.Embed(title="Message Deleted")
    embed.add_field(name="Member: ", value=message.author.mention, inline=False)
    embed.add_field(name="Message: ", value=message.content, inline=True)
    embed.add_field(name="Channel: ", value=message.channel.mention, inline=False)

    await self.bot.get_channel(channel).send(embed=embed)

Hope this helps anyone else having trouble with this as well!