#Get user who deleted a message

7 messages · Page 1 of 1 (latest)

quick crown
#

On the messageDelete event, is there any way to get the user who deleted the message? I'm trying with audit logs but its not giving anything relevant:

const { EmbedBuilder, AuditLogEvent } = require("discord.js");

module.exports = {
    once: false,
    async execute(message, client) {
        if (message.partial) {
            try {
                message = await message.fetch();
            } catch {
                return;
            }
        }
        if (message.author.bot) return;

        const embed = new EmbedBuilder()
            .setTitle("Message Deleted")
            .setDescription(`**${message.author.toString()}**'s message was deleted in <#${message.channel.id}>`)
            .setColor(client.config.colors.danger)
            .addFields({
                name: "Content",
                value: message.content || "No content",
                inline: false,
            })
            .setFooter({
                text: client.config.footer.text,
                iconURL: client.config.footer.img,
            })
            .setTimestamp();

        let channel = await client.channels.fetch(client.config.channels.memberChannel);

        const fetchedLogs = await message.guild.fetchAuditLogs({
            limit: 1,
            type: AuditLogEvent.MessageDelete,
        });
        const deletionLog = fetchedLogs.entries.first();

        if (deletionLog) {
            const { executor, target, createdAt } = deletionLog;

            const timeDifference = Date.now() - createdAt.getTime();
            const isSameMessage = target.id === message.author.id;

            if (isSameMessage && timeDifference < 5000) {
                embed.addFields({
                    name: "Deleted by",
                    value: executor.tag,
                    inline: true,
                });
            }
        }

        channel.send({ embeds: [embed] });
    },
};
final streamBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • Not a discord.js issue? Check out #1081585952654360687.
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
  • Marked as resolved by OP
woven tree
#

Audit logs are not generated if the author or a bot deleted it

quick crown
#

ah I see

woven tree
#

Would recommend using the guildAuditLogEntryCreate event for this instead of fetching

quick crown
#

is it just better for api calls?

woven tree
#

Yeah, you wouldn't be be making a request every time a message is deleted