Search code examples
pythonparametersdiscord.pynextcord

nextcord py parameter on_raw_message_delete


I use on_raw_message_delete and i want to print the name of the person who deleted the message. I can print the deleted message and user who writed the message but I don't know how to print the name of the person who deleted the deleted message.

@commands.Cog.listener()
async def on_raw_message_delete(self, payload):
    channel = self.bot.get_channel(channel_id)
    embed = nextcord.Embed(title="message deleted",
    description=f"Message deleted in <#{payload.cached_message.channel.id}>\n Message deleted by {payload.name}")

Solution

  • It is not part of the message delete payload, so you'll need to check the audit logs.

    You also may want to check the date of the entry since a user deleting their own message is not logged to audit logs.

    @commands.Cog.listener()
    async def on_message_delete(self, message: nextcord.Message):
        async for entry in message.guild.audit_logs(action=nextcord.AuditLogAction.message_delete, limit=1):
            log = entry
        # if deleted more than a second ago, use the author instead
        deleter = log.user if (datetime.now() - log.created_at).seconds > 1 else message.author
    

    Note that this may be unreliable if you have messages deleted frequently in your guild.

    This is untested, but you can modify as needed.