Get user who deleted a message

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] });
},
};
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] });
},
};
7 Replies
d.js toolkit
d.js toolkit3mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - 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
Mark
Mark3mo ago
Audit logs are not generated if the author or a bot deleted it
naatthhaan
naatthhaanOP3mo ago
ah I see
Mark
Mark3mo ago
Would recommend using the guildAuditLogEntryCreate event for this instead of fetching
naatthhaan
naatthhaanOP3mo ago
is it just better for api calls?
Mark
Mark3mo ago
Yeah, you wouldn't be be making a request every time a message is deleted
naatthhaan
naatthhaanOP3mo ago
cool. thank you for the help
Want results from more Discord servers?
Add your server