Finding who deleted the message

不问归期 提交于 2020-12-15 05:01:44

问题


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("messageDelete", (messageDelete) => {

  let DeleteEmbed = new Discord.RichEmbed()
  .setTitle("**DELETED MESSAGE**")
  .setColor("#fc3c3c")
  .addField("Author", messageDelete.author.tag, true)
  .addField("Channel", messageDelete.channel, true)
  .addField("Message", messageDelete.content)
  .setFooter(`Message ID: ${messageDelete.id} | Author ID: ${messageDelete.author.id}`);

  let DeleteChannel = messageDelete.guild.channels.find(x => x.name === "delete-log");
  DeleteChannel.send(DeleteEmbed);
});

Thanks!


回答1:


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.




回答2:


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(() => ({
    entries: []
  }));

  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 ? 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 exception.
    .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);
});

If I made a mistake or have any improvements, feel free to edit this!




回答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});
});



回答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)
})



回答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);
});


来源:https://stackoverflow.com/questions/53328061/finding-who-deleted-the-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!