How can I get my Discord bot to properly handle Audit Log events?

I’m having trouble with my Discord bot’s Audit Log functionality. I’ve set it up using the discordjs.guide, but it’s not working as expected. I’m trying to log deleted messages, but nothing happens.

Here’s a simplified version of my code:

const { Client, GatewayIntentBits, AuditLogEvent, Events } = require('discord.js');

const bot = new Client({ 
    intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages]
});

bot.on(Events.GuildAuditLogEntryCreate, async (logEntry) => {
    const { action, channelInfo, doerId, victimId } = logEntry;

    if (action !== AuditLogEvent.MessageDelete) return;

    const doer = await bot.users.fetch(doerId);
    const victim = await bot.users.fetch(victimId);

    console.log(`${victim.username}'s message was removed by ${doer.username} in ${channelInfo}.`);
});

bot.login('your-token-here');

I’ve double-checked that the bot has the ‘View Audit Log’ permission. I’ve also tried adding debug logs, but they don’t show up either. What am I missing? How can I get this Audit Log event to work correctly?

I’ve been working with Discord bots for a while, and I’ve run into this exact problem before. One thing that’s not immediately obvious is that the Audit Log events aren’t always instant. There can be a slight delay between when an action occurs and when the audit log entry is created.

To handle this, you might want to implement a short delay before checking the audit log. Something like this could work:

bot.on(Events.MessageDelete, async (message) => {
    await new Promise(resolve => setTimeout(resolve, 1000));
    const fetchedLogs = await message.guild.fetchAuditLogs({
        limit: 1,
        type: AuditLogEvent.MessageDelete,
    });
    const deletionLog = fetchedLogs.entries.first();

    if (deletionLog) {
        const { executor, target } = deletionLog;
        console.log(`A message by ${target.tag} was deleted by ${executor.tag}.`);
    } else {
        console.log(`A message was deleted, but no relevant audit logs were found.`);
    }
});

This approach has worked well for me in practice. Give it a try and see if it resolves your issue.

hey emma, ive had similar issues. make sure ur bot has the right intents enabled, especially GuildAuditLogs. also, check if ur using the latest discord.js version. sometimes older versions have bugs with audit logs. hope this helps!

I encountered a similar issue when working with Discord.js and Audit Log events. The problem likely lies in your intents configuration. You need to explicitly include GatewayIntentBits.GuildAuditLogs in your Client constructor. Additionally, ensure you’re using discord.js v14 or later, as earlier versions handle Audit Log events differently. Here’s a revised version of your Client setup:

const bot = new Client({ 
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.GuildAuditLogs
    ]
});

This should allow your bot to properly receive and handle Audit Log events. If issues persist, consider implementing error handling to catch and log any potential errors occurring during the event processing.