Log deleted messages with their content and author in one channel, edits with before and after in another, joins and leaves in a third, role changes in a fourth, each channel chosen in settings, say who deleted a message, kicked or banned a member or changed a role, and skip messages from bots
Automatically logs moderation events across your server into separate channels:
Configure the four log channels in settings. Bot messages are automatically skipped. To say who did it, the bot needs the View Audit Log permission.
Made by MEE6. Remix it to get a copy of your own and change anything you like.
@Alex (Alex)
#general
I think I made a typo
I think I fixed the typo
// Moderation logging system: tracks deleted messages, edits, joins/leaves and role changes, and who did them
mee6.settings({
deleted_channel: { type: 'channel', label: 'Deleted messages log', required: true, sample: 'mod-logs-deleted' },
edited_channel: { type: 'channel', label: 'Edited messages log', required: true, sample: 'mod-logs-edited' },
members_channel: { type: 'channel', label: 'Member joins/leaves log', required: true, sample: 'mod-logs-members' },
roles_channel: { type: 'channel', label: 'Role changes log', required: true, sample: 'mod-logs-roles' },
});
// Who did it: the newest matching line of the server's audit log. null when
// there is none, 'unreadable' when the bot may not read it, so a log never
// says nobody did it just because it could not look.
async function findEntry(options: {
actions: AuditLogAction[];
targetId: string;
channelId?: string;
}): Promise<AuditLogEntry | null | 'unreadable'> {
try {
const [entry] = await mee6.listAuditLog({ ...options, withinSeconds: 10 });
return entry ?? null;
} catch (error) {
mee6.log(`Could not read the audit log: ${error}`);
return 'unreadable';
}
}
const NEEDS_PERMISSION = 'Unknown (the bot needs View Audit Log)';
function byWhom(entry: AuditLogEntry): string {
return entry.executorId ? `<@${entry.executorId}>` : 'Unknown';
}
// Log deleted messages
mee6.on('message_delete', async (ctx) => {
// Skip bot messages
if (ctx.author.bot) return;
const channelId = ctx.settings.deleted_channel;
if (!channelId) {
mee6.log('Deleted messages log channel not configured');
return;
}
const entry = await findEntry({
actions: ['message_delete'],
targetId: ctx.author.id,
channelId: ctx.channelId,
});
// Discord writes nothing when members delete their own message.
const deletedBy = entry === 'unreadable' ? NEEDS_PERMISSION : entry ? byWhom(entry) : 'Its author, or unknown';
await ctx.send(channelId, {
embed: {
title: '🗑️ Message Deleted',
description: ctx.message.content || '*No text content*',
color: '#e74c3c',
fields: [
{ name: 'Author', value: `${ctx.author.mention} (${ctx.author.username})`, inline: true },
{ name: 'Channel', value: `<#${ctx.channelId}>`, inline: true },
{ name: 'Deleted by', value: deletedBy, inline: true },
],
footer: { text: `User ID: ${ctx.author.id} • Message ID: ${ctx.messageId}` },
timestamp: new Date().toISOString(),
},
});
});
// Log edited messages
mee6.on('message_edit', async (ctx) => {
// Skip bot messages
if (ctx.author.bot) return;
const channelId = ctx.settings.edited_channel;
if (!channelId) {
mee6.log('Edited messages log channel not configured');
return;
}
await ctx.send(channelId, {
embed: {
title: '✏️ Message Edited',
color: '#f39c12',
fields: [
{ name: 'Author', value: `${ctx.author.mention} (${ctx.author.username})`, inline: true },
{ name: 'Channel', value: `<#${ctx.channelId}>`, inline: true },
{ name: 'Before', value: ctx.previousContent || '*No text content*' },
{ name: 'After', value: ctx.message.content || '*No text content*' },
],
footer: { text: `User ID: ${ctx.author.id} • Message ID: ${ctx.messageId}` },
timestamp: new Date().toISOString(),
},
});
});
// Log member joins
mee6.on('member_join', async (ctx) => {
const channelId = ctx.settings.members_channel;
if (!channelId) {
mee6.log('Member logs channel not configured');
return;
}
const accountAge = Math.floor((Date.now() - Date.parse(ctx.author.createdAt)) / 86400000);
await ctx.send(channelId, {
embed: {
title: '📥 Member Joined',
description: `${ctx.author.mention} (${ctx.author.username})`,
color: '#2ecc71',
thumbnail_url: ctx.author.avatarUrl,
fields: [
{
name: 'Account Created',
value: `<t:${Math.floor(Date.parse(ctx.author.createdAt) / 1000)}:R>`,
inline: true,
},
{ name: 'Account Age', value: `${accountAge} days`, inline: true },
],
footer: { text: `User ID: ${ctx.author.id}` },
timestamp: new Date().toISOString(),
},
});
});
// Log member leaves
mee6.on('member_leave', async (ctx) => {
const channelId = ctx.settings.members_channel;
if (!channelId) {
mee6.log('Member logs channel not configured');
return;
}
const entry = await findEntry({ actions: ['member_kick', 'member_ban_add'], targetId: ctx.author.id });
const removal = entry && entry !== 'unreadable' ? entry : null;
const title = !removal
? '📤 Member Left'
: removal.action === 'member_kick'
? '👢 Member Kicked'
: '🔨 Member Banned';
await ctx.send(channelId, {
embed: {
title,
description: `${ctx.author.mention} (${ctx.author.username})`,
color: '#e67e22',
thumbnail_url: ctx.author.avatarUrl,
fields: [
...(removal
? [
{ name: 'By', value: byWhom(removal), inline: true },
{ name: 'Reason', value: removal.reason ?? 'No reason given', inline: true },
]
: []),
...(entry === 'unreadable' ? [{ name: 'Kicked or banned?', value: NEEDS_PERMISSION }] : []),
],
footer: { text: `User ID: ${ctx.author.id}` },
timestamp: new Date().toISOString(),
},
});
});
// Log role changes
mee6.on('member_update', async (ctx) => {
const channelId = ctx.settings.roles_channel;
if (!channelId) {
mee6.log('Role changes log channel not configured');
return;
}
// Skip if no role changes
if (ctx.addedRoles.length === 0 && ctx.removedRoles.length === 0) return;
const fields = [];
if (ctx.addedRoles.length > 0) {
fields.push({
name: '✅ Roles Added',
value: ctx.addedRoles.map((id) => `<@&${id}>`).join(', '),
});
}
if (ctx.removedRoles.length > 0) {
fields.push({
name: '❌ Roles Removed',
value: ctx.removedRoles.map((id) => `<@&${id}>`).join(', '),
});
}
const entry = await findEntry({ actions: ['member_role_update'], targetId: ctx.author.id });
fields.push({
name: 'Changed by',
value: entry === 'unreadable' ? NEEDS_PERMISSION : entry ? byWhom(entry) : 'Unknown',
});
await ctx.send(channelId, {
embed: {
title: '🎭 Role Changes',
description: `${ctx.author.mention} (${ctx.author.username})`,
color: '#9b59b6',
fields,
footer: { text: `User ID: ${ctx.author.id}` },
timestamp: new Date().toISOString(),
},
});
});