Audit Log Parser
Parse and analyze Discord audit log entries. Track server changes, moderator actions, and generate comprehensive reports.
Action value: 1 | Target type: Guild
Action Details
// Discord.js v14+ - Fetch Audit Logs
const { AuditLogEvent } = require('discord.js');
// Fetch recent audit logs
const auditLogs = await guild.fetchAuditLogs({
limit: 50, // Max 100
type: AuditLogEvent.GUILD_UPDATE
});
// Access entries
for (const entry of auditLogs.entries.values()) {
console.log(`Action: ${entry.action}`);
console.log(`Executor: ${entry.executor.tag}`);
console.log(`Target: ${entry.target}`);
console.log(`Reason: ${entry.reason || 'No reason provided'}`);
console.log(`Timestamp: ${entry.createdAt}`);
// Access changes (for UPDATE actions)
if (entry.changes) {
for (const change of entry.changes) {
console.log(`Changed ${change.key}: ${change.old} → ${change.new}`);
}
}
}
// Filter by user
const userLogs = await guild.fetchAuditLogs({
user: '123456789012345678', // User ID
type: AuditLogEvent.GUILD_UPDATE
});
// Find specific entry
const firstEntry = auditLogs.entries.first();
if (firstEntry) {
console.log(`Most recent action by: ${firstEntry.executor.tag}`);
}// Discord.js - Monitor Audit Logs
const { AuditLogEvent } = require('discord.js');
// Monitor for server settings were updated
setInterval(async () => {
const auditLogs = await guild.fetchAuditLogs({
limit: 1,
type: AuditLogEvent.GUILD_UPDATE
});
const entry = auditLogs.entries.first();
if (entry && entry.createdAt > lastChecked) {
// New entry found
console.log(`Server settings were updated`);
console.log(`By: ${entry.executor.tag}`);
console.log(`Target: ${entry.target}`);
// Send to log channel
const logChannel = guild.channels.cache.get('LOG_CHANNEL_ID');
if (logChannel) {
const embed = {
color: 0xFF6F00,
title: 'Server settings were updated',
fields: [
{ name: 'Executor', value: `<@${entry.executor.id}>`, inline: true },
{ name: 'Target', value: entry.target?.toString() || 'Unknown', inline: true },
{ name: 'Reason', value: entry.reason || 'No reason provided' }
],
timestamp: entry.createdAt
};
await logChannel.send({ embeds: [embed] });
}
lastChecked = entry.createdAt;
}
}, 5000); // Check every 5 seconds
// Better approach: Listen to events and check audit logs
client.on('guildMemberRemove', async (member) => {
// Wait a bit for audit log to be created
await new Promise(resolve => setTimeout(resolve, 1000));
const auditLogs = await member.guild.fetchAuditLogs({
limit: 1,
type: AuditLogEvent.MEMBER_KICK
});
const kickLog = auditLogs.entries.first();
if (kickLog) {
console.log(`${member.user.tag} was kicked by ${kickLog.executor.tag}`);
console.log(`Reason: ${kickLog.reason || 'No reason'}`);
}
});What are Audit Logs?
Audit logs track all administrative actions in a Discord server. They show who did what, when, and why. Requires "View Audit Log" permission. Logs are kept for 45 days.
What are Discord Audit Logs?
Discord audit logs are a record of all administrative actions performed in a server. They track who did what, when they did it, and optionally why. This is essential for server moderation, security, and accountability.
Audit logs are stored for 45 days and can be accessed by users with the "View Audit Log" permission. They contain detailed information about changes, including before/after values for updates.
Audit Log Categories
Member Actions
- • Member kicks and bans
- • Member updates (nick, roles)
- • Timeouts and voice changes
- • Member prune operations
Channel Actions
- • Channel creation and deletion
- • Channel updates (name, topic)
- • Permission overwrites
- • Thread creation and updates
Role Actions
- • Role creation and deletion
- • Role updates (permissions, color)
- • Role position changes
- • Role assignments to members
Message Actions
- • Message deletion (single)
- • Bulk message deletion
- • Message pin/unpin
Server Settings
- • Server name and icon
- • Region and verification level
- • Explicit content filter
- • AFK channel and timeout
Other Actions
- • Webhook creation and updates
- • Emoji and sticker changes
- • Integration updates
- • Invite creation and deletion
Required Permissions
View Audit Log
Your bot needs the "View Audit Log" permission to access audit log entries. This permission allows reading all audit log data but not modifying it.
Permission Value: 128 (1 << 7) Grant this permission in Server Settings → Roles → Bot Role → Permissions
Common Use Cases
Moderation Logs
Track moderator actions (bans, kicks, timeouts) and send them to a dedicated log channel. This provides accountability and helps with moderation team coordination.
Server Changes
Monitor changes to server settings, channels, and roles. Detect unauthorized modifications and track configuration history.
Security Monitoring
Alert administrators when suspicious actions occur, such as mass channel deletions, role permission changes, or unusual webhook activity.
Analytics
Generate reports on server activity: most active moderators, common actions, peak activity times, and moderation trends.
Best Practices
Combine with Events
Don't poll audit logs constantly. Instead, listen to Discord events (like memberRemove) and then check audit logs to get context. This is more efficient and reduces API calls.
Add Delays
Audit log entries may take 1-2 seconds to appear after an action. Add a small delay before fetching logs after an event to ensure the entry exists.
Filter Appropriately
Use action type filters to narrow down results. Fetching all logs and filtering client-side wastes bandwidth and is slower.
Cache Recent Logs
Cache recently processed audit log entries to avoid duplicate processing. Store the last checked timestamp or entry ID.
Include Reasons
When displaying audit logs, always show the reason field if present. This provides crucial context for why an action was taken.
Restrict Log Access
Log channels should only be visible to administrators and moderators. Audit logs can contain sensitive information that shouldn't be public.