Rumah > Soal Jawab > teks badan
P粉7093078652023-08-30 15:32:16
Anda boleh menggunakan messageDelete
event 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: Guilds
,GuildMembers
和GuildMessages
。您还需要partials
:Channel
,Message
和GuildMember
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
)时立即找出谁删除了消息。它需要启用GuildModeration
Niat.
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}.`); } });