3

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)
Broso56
  • 55
  • 5
  • 2
    A `discord.Message` object doesn't have any attribute about who deleted it. Deleted messages are considered like `discord.DeletedReferencedMessage` objects, but the only have `channel_id`, `guild_id`, and `id` attributes. So, since I don't know your answer, I upvoted your answer, and I'll set a bounty on it if nobody will answer before 2 days. – FLAK-ZOSO Sep 04 '21 at 10:00
  • 1
    The discord API doesn't return the user who deleted a message, so the best way is the way you're already using by getting the latest audit log action, checking if it's in the same channel and if not, assume that the deletor was the author of the message. This could lead to some errors, eg if P1 deletes a message of P2 in channel A, and then P3 deletes its own message in channel A, the bot would think that P1 deleted P3's message. – LoahL Sep 04 '21 at 11:49

2 Answers2

1

Impossible


...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


Discord.py developing has ended, so nobody will be able to do what you want.

Edit


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"))
FLAK-ZOSO
  • 3,873
  • 4
  • 8
  • 28
-1

So you`re trying to get the message author from a deleted message and the user who deletet it. I think this should work:

@client.event
async def on_message_delete(message, member):
    async for entry in message.guild.audit_logs(limit=1,action=discord.AuditLogAction.message_delete):
        deleter = entry.user
    print(f"{deleter.name} deleted message by {message.author.name}")

output: enter image description here

PCM
  • 2,881
  • 2
  • 8
  • 30
  • 2
    Please maintain your language. – PCM Sep 05 '21 at 12:13
  • 1
    Please don't post text as an image. This makes in inaccessible to search engines, some users with disabilities, while making it impossible for us to copy&paste it, just to name a few of the many drawbacks. Thanks! – Neuron Sep 05 '21 at 15:11
  • I don't think this would work if soneone deleted their own message. It doesn't show up on the audit log sadly. – Broso56 Sep 06 '21 at 14:59
  • 1
    @Der Pruefer This shouldn't work. It should work for the most part, I'm sure, but have you tried deleting your own message and seeing if it pops up? Cause if I'm looking at this right it's only grabbing the name from the audit log, and therefor if you delete your own message, since it doesn't show up on the audit log, it would falsely flag another user as the deleter. If this really works to it's full extent then I am very surprised, I'll have to try this later. Thank you for helping. – Broso56 Sep 07 '21 at 20:12
  • @Broso56 yeah sure i have tried that and it work just perfect. Ohterwise i wouldn`t say it would work. – Der Pruefer Sep 12 '21 at 17:54