我才刚刚开始学习如何创建discord机器人,我正在努力弄清楚如何记录是谁删除了一条消息.
我try 了message.author
,但当然,这会记录谁发送了消息,而且我不知道很多语法,所以我没有try 其他任何事情.
我才刚刚开始学习如何创建discord机器人,我正在努力弄清楚如何记录是谁删除了一条消息.
我try 了message.author
,但当然,这会记录谁发送了消息,而且我不知道很多语法,所以我没有try 其他任何事情.
您可以使用在删除消息时触发的messageDelete
event.如果一个用户删除了另一个用户的消息,您可以判断审核日志(log).
首先,确保您有所需的意图:Guilds
、GuildMembers
和GuildMessages
.你还需要partials
:Channel
,Message
和GuildMember
来处理在你的机器人上线之前发送的消息.
一旦消息被删除,您可以使用fetchAuditLogs
方法来获取发送被删除消息的行会的审核日志(log).
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
],
partials: [
Partials.Channel,
Partials.GuildMember,
Partials.Message,
],
});
client.on('messageDelete', async (message) => {
const logs = await message.guild.fetchAuditLogs({
type: AuditLogEvent.MessageDelete,
limit: 1,
});
// logs.entries is a collection, so grab the first one
const firstEntry = logs.entries.first();
const { executorId, target, targetId } = firstEntry;
// Ensure the executor is cached
const user = await client.users.fetch(executorId);
if (target) {
// The message object is in the cache and you can provide a detailed log here
console.log(`A message by ${target.tag} was deleted by ${user.tag}.`);
} else {
// The message object was not cached, but you can still retrieve some information
console.log(`A message with id ${targetId} was deleted by ${user.tag}.`);
}
});
在discord.js v14.8+中有一个新事件,GuildAuditLogEntryCreate
.一旦收到相应的审核日志(log)事件(GuildAuditLogEntryCreate
),您就可以找出是谁删除了邮件.它需要启用GuildModeration
意图.
const { AuditLogEvent, Events } = require('discord.js');
client.on(Events.GuildAuditLogEntryCreate, async (auditLog) => {
// Define your variables
const { action, executorId, target, targetId } = auditLog;
// Check only for deleted messages
if (action !== AuditLogEvent.MessageDelete) return;
// Ensure the executor is cached
const user = await client.users.fetch(executorId);
if (target) {
// The message object is in the cache and you can provide a detailed log here
console.log(`A message by ${target.tag} was deleted by ${user.tag}.`);
} else {
// The message object was not cached, but you can still retrieve some information
console.log(`A message with id ${targetId} was deleted by ${user.tag}.`);
}
});