Finding Who Deleted The Message
My discord.js bot is programmed to log deleted messages. I have the code setup, but I was wondering if there was a way to see who deleted it? Thanks. Heres the code: bot.on('messag
Solution 1:
There is no smooth and accurate way to do this as @NintendoZaedus correctly pointed out. However I made a way to somewhat do this with minimal possible mistakes made.
Do note these are impossible at the time of writing this and will return 'Unknown':
- See which bot deleted a message as Discord does not log message deletions for bots
- See if the author deleted their own message (Discord does not log this either)
- 100% determine if the message deleted matches the fetched audit log due to Discord not including the message ID in audit logs
// If discord.js isn't defined, add this, if it is, don't.
const Discord = require("discord.js");
bot.on("messageDelete", async (messageDelete) => {
// Add latency as audit logs aren't instantly updated, adding a higher latency will result in slower logs, but higher accuracy.
await Discord.Util.delayFor(900);
// Fetch a couple audit logs than just one as new entries could've been added right after this event was emitted.
const fetchedLogs = await messageDelete.guild.fetchAuditLogs({
limit: 6,
type: 'MESSAGE_DELETE'
}).catch(console.error);
const auditEntry = fetchedLogs.entries.find(a =>
// Small filter function to make use of the little discord provides to narrow down the correct audit entry.
a.target.id === messageDelete.author.id &&
a.extra.channel.id === messageDelete.channel.id &&
// Ignore entries that are older than 20 seconds to reduce false positives.
Date.now() - a.createdTimestamp < 20000
);
// If entry exists, grab the user that deleted the message and display username + tag, if none, display 'Unknown'.
const executor = auditEntry.executor ? auditEntry.executor.tag : 'Unknown';
// Finally, prepare the embed and send the log. (using similar code posted by thread author but improved)
// <Discord>.MessageEmbed for v12, <Discord>.RichEmbed for older.
const DeleteEmbed = new Discord.MessageEmbed()
.setTitle("DELETED MESSAGE")
.setColor("#fc3c3c")
.addField("Author", messageDelete.author.tag, true)
// New field for user which deleted the message.
.addField("Deleted By", executor, true)
.addField("Channel", messageDelete.channel, true)
// Messages can be empty too, but I won't be going over how to include embeds/attachments, just displaying 'None' instead to avoid undefined/null.
.addField("Message", messageDelete.content || "None")
.setFooter(`Message ID: ${messageDelete.id} | Author ID: ${messageDelete.author.id}`);
const DeleteChannel = messageDelete.guild.channels.find(x => x.name === "delete-log");
DeleteChannel.send(DeleteEmbed);
});
Solution 2:
There is no way of doing it except through the audit logs which might be very buggy and hard to work with. I hope this is a little help to you.
Solution 3:
I think the only way you can achieve that is by looking at the audit logs.
client.on("messageDelete", async msg => {
let logs = await msg.guild.fetchAuditLogs({type: 72});
let entry = logs.entries.first();
let embed = new Discord.RichEmbed()
.setTitle("**DELETED MESSAGE**")
.setColor("#fc3c3c")
.addField("Author", msg.author.tag, true)
.addField("Channel", msg.channel, true)
.addField("Message", msg.content)
.addField("Executor", entry.executor)
.addField("Reason", entry.reason || "Unspecified")
.setFooter(`Message ID: ${msg.id} | Author ID: ${msg.author.id}`);
let channel = msg.guild.channels.find(x => x.name === 'delete-log');
channel.send({embed});
});
Solution 4:
This can be achieved by using message.author.tag
. With this, you can even show their avatar.
bot.on("messageDelete", async(message) => {
let delEmbed = new discord.RichEmbed()
.setAuthor(message.author.tag, message.author.avatarURL)
.setThumbnail(message.author.avatarURL)
.setColor("RANDOM")
.setDescription("A message from a user was deleted!")
.addField("Message", message.content)
.setTimestamp();
let loggingChannel = message.guild.channels.find(ch => ch.name === "logs")
if(!loggingChannel) return;
loggingChannel.send(delEmbed)
})
Solution 5:
I edited the message to make it more beatufil:
const Discord = require("discord.js);
bot.on("messageDelete", async msg => {
let logs = await msg.guild.fetchAuditLogs({type: 72});
let entry = logs.entries.first();
let embed = new Discord.RichEmbed()
.setTitle("**DELETED MESSAGE**")
.setColor("#fc3c3c")
.setAuthor(msg.author.tag, msg.author.avatarURL)
.setDescription(`**Author:** ${msg.author.tag}\n**Channel:** ${msg.channel}\n**Message:** ${msg.content}\n**Executor**: ${entry.executor}`)
.setFooter(`Message ID: ${msg.id}\nAuthor ID: ${msg.author.id}`)
.setThumbnail(msg.author.avatarURL)
.setTimestamp();
let delchannel = msg.guild.channels.find(x => x.name === 'your-log-channel-name');
delchannel.send(embed);
});
Post a Comment for "Finding Who Deleted The Message"