Wie kann man Benutzer verfolgen, die Nachrichten in discord.js gelöscht haben?
P粉760675452
P粉760675452 2023-08-29 17:00:40
0
1
642
<p>Ich habe gerade angefangen zu lernen, wie man einen Discord-Bot erstellt, und versuche herauszufinden, wie man protokolliert, wer eine Nachricht gelöscht hat. </p> <p>Ich habe <code>message.author</code> ausprobiert, aber das protokolliert natürlich, wer die Nachricht gesendet hat, und ich kenne mich mit der Syntax nicht so gut aus, also habe ich nichts anderes ausprobiert. </p>
P粉760675452
P粉760675452

Antworte allen(1)
P粉709307865

您可以使用messageDelete事件,该事件在消息被删除时触发。您可以检查审核日志,以查看用户是否删除了其他用户的消息。

首先,确保您具有所需的意图:GuildsGuildMembersGuildMessages。您还需要partialsChannelMessageGuildMember,以处理在您的机器人上线之前发送的消息。

一旦消息被删除,您可以使用fetchAuditLogs方法来获取被删除消息所在的服务器的审核日志。

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。您可以在收到相应的审核日志事件(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}.`);
  }
});
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage