Bagaimana untuk menjejaki pengguna yang memadamkan mesej dalam discord.js?
P粉760675452
P粉760675452 2023-08-29 17:00:40
0
1
641
<p>Saya baru mula belajar cara mencipta bot perselisihan dan saya cuba memikirkan cara untuk log siapa yang memadam mesej. </p> <p>Saya mencuba <kod>message.author</code>, tetapi sudah tentu, log yang menghantar mesej itu dan saya tidak tahu banyak tentang sintaks jadi tidak mencuba apa-apa lagi. </p>
P粉760675452
P粉760675452

membalas semua(1)
P粉709307865

Anda boleh menggunakan messageDeleteevent yang dicetuskan apabila mesej dipadamkan. Anda boleh menyemak log audit untuk melihat sama ada pengguna memadamkan mesej pengguna lain.

Pertama sekali, pastikan anda mempunyai niat yang diperlukan: GuildsGuildMembersGuildMessages。您还需要partialsChannelMessageGuildMember untuk mengendalikan mesej yang dihantar sebelum bot anda disiarkan secara langsung.

Setelah mesej dipadamkan, anda boleh menggunakan kaedah fetchAuditLogs untuk mendapatkan log audit pelayan di mana mesej yang dipadam itu berada.

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

Dalam discord.js v14.8+, terdapat acara baharu GuildAuditLogEntryCreate。您可以在收到相应的审核日志事件(GuildAuditLogEntryCreate)时立即找出谁删除了消息。它需要启用GuildModerationNiat.

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}.`);
  }
});
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan