我的discord.js机器人已被编程为记录已删除的消息。我已经设置了代码,但是我想知道是否有办法查看谁删除了它?谢谢。
此处提供代码:
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);
});
谢谢!
答案 0 :(得分:2)
@NintendoZaedus正确指出,没有流畅,准确的方法来执行此操作。但是,我提出了一种方法来尽可能减少犯下的错误。
请注意,在编写本文时这些是不可能的,并且将返回“未知”:
// 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);
});
如果我犯了一个错误或有任何改进,请随时进行编辑!
答案 1 :(得分:0)
我认为,实现此目标的唯一方法是查看审核日志。
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});
});
答案 2 :(得分:0)
除了通过审核日志(这可能是非常多的错误并且很难使用)之外,没有其他方法可以做到。希望对您有所帮助。
答案 3 :(得分:-1)
这可以通过使用message.author.tag
来实现。这样,您甚至可以显示他们的头像。
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)
})
答案 4 :(得分:-2)
我对邮件进行了编辑,使其更加美观:
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);
});