Heyo, so I'm trying to make a delete logger that just essentially sends a message in a pre-defined channel saying 'x user deleted a message by x person' however I have a problem.
So how I'm currently getting the name of who deleted the message is by checking the most recent audit log. However, since deleting your own messages doesn't show up in the audit log, it breaks if you delete your own message. And cause of that, I'm checking if the audit log was created < 1 second ago (and if not, assign the person who deleted the message as the message author). But then there's another problem. That being, if you delete multiple messages and they are all sent by the same person (example: person 1 deletes 3 messages by person 2), it condenses that audit log into one entry (shows "x user deleted x messages from x user", instead of "x user deleted a message from x user" repeated x times). But I don't think it updates the time the audit log was created.
So now I'm not entirely sure with how to fix this one. Nothing really comes to mind. Here is my current code for the on_message_delete event.
@client.event
async def on_message_delete(message):
pst = pytz.timezone('US/Pacific')
psttime = datetime.datetime.now(pst)
current_pst_date = psttime.strftime('%Y/%m/%d, %I:%M %p PST.')
delete_channel = message.channel.mention
nsfw_tag = ''
spoiler_tag = ''
in_nsfw = False
channel = client.get_channel(863957409293664266)
message_author = message.author
if message.content.startswith("^"):
return
delete_author = None
delta = datetime.datetime.utcnow() - datetime.timedelta(seconds=1)
print(delta)
async for entry in message.guild.audit_logs(action=discord.AuditLogAction.message_delete, limit=1):
print(entry.created_at)
if entry.created_at > delta:
delete_author = "{0.user}".format(entry)
else:
delete_author = message.author
if delete_author is None:
await ctx.send("Test: Some random error ocurred.")
return
if 'nsfw=True' in str(message):
nsfw_tag = '**:warning: NSFW :warning:**'
in_nsfw = True
spoiler_tag = '||'
e = discord.Embed(title='',
description=f"{nsfw_tag}\n{delete_author.mention} Deleted a Message by {message_author.name} in {spoiler_tag}{delete_channel}{spoiler_tag}\n||\n||")
if in_nsfw is True:
e.color = Color.red()
else:
e.color = Color.gold()
e.set_author(name=f"{message_author}", icon_url=f"{message_author.avatar_url}")
e.set_footer(text=f"• {current_pst_date}")
await channel.send(embed=e)
...because Discord API doesn't return the user who deleted a message.
Like @Chuaat said, you can try to do it in other ways, but this could lead to some errors.
Discord.py developing has ended, so nobody will be able to do what you want.
It is possible to get the deleter of a message, but it's impossible to do it using only discord.Message
attributes.
You can use the guild audit log for this scope, like this:
async for entry in message.guild.audit_logs(limit=1):
deleter = entry.user
await message.channel.send(embed = discord.Embed(title="Message deleted", description=f"{deleter.mention} deleted a message"))