If a member posts the same message three times within ten seconds, in any channels, ban them, delete their messages of the last hour and post the text in a channel I choose; members with the @Staff role are exempt
Automatically detects and bans members who post the same message in three or more channels within 10 seconds. Staff members are exempt from this check.
How it works:
Configuration:
Made by MEE6. Remix it to get a copy of your own and change anything you like.
Member: @Alex (Alex) Reason: the same message 3 times in 10 seconds
check out this cool server! discord.gg/spam123
#general, #Lounge
// Raid spam: a member who posts the same message three times within ten
// seconds โ across channels, the way spam bots sweep a server โ is banned
// and their recent messages are removed.
mee6.settings({
staff_role: { type: 'role', label: 'Staff role (exempt from the ban)', sample: 'Staff' },
log_channel: { type: 'channel', label: 'Where to log bans', required: true, sample: 'mod-logs' },
});
interface Seen {
content: string;
channelId: string;
messageId: string;
at: number;
}
const WINDOW_MS = 10_000;
const COPIES = 3;
// Every run starts from nothing, so the recent messages live in the member's
// store, not in a variable: one read and one write per message.
mee6.on('message', async (ctx) => {
if (ctx.settings.staff_role && ctx.author.roles.includes(ctx.settings.staff_role)) return;
const content = ctx.message.content.trim().toLowerCase();
if (!content) return;
const now = Date.now();
const m = mee6.store.member(ctx.author.id);
const recent = ((await m.get<Seen[]>('recent')) || []).filter((s) => now - s.at < 3_600_000);
recent.push({ content, channelId: ctx.channelId, messageId: ctx.message.id, at: now });
// Keep an hour of history, twenty messages at most: enough to clean up after a ban.
const kept = recent.slice(-20);
await m.set('recent', kept, { ttl: 3600 });
const copies = kept.filter((s) => s.content === content && now - s.at < WINDOW_MS);
if (copies.length < COPIES) return;
const channels = new Set(copies.map((s) => s.channelId));
// Spam: remove what they posted in the last hour, then ban.
for (const s of kept) {
try {
await mee6.deleteMessage(s.channelId, s.messageId);
} catch (e) {
// already gone, or no permission in that channel
}
}
try {
await mee6.ban(ctx.author.id, {
reason: `Spam: the same message ${copies.length} times in ${WINDOW_MS / 1000} seconds`,
deleteMessageDays: 1,
});
} catch (e) {
mee6.log(`Could not ban ${ctx.author.id}: ${e}`);
return;
}
await m.delete('recent');
if (ctx.settings.log_channel) {
await ctx.send(ctx.settings.log_channel, {
embed: {
title: '๐ซ Banned for spam',
description: `**Member:** ${ctx.author.mention} (${ctx.author.username})\n**Reason:** the same message ${
copies.length
} times in ${WINDOW_MS / 1000} seconds`,
fields: [
{ name: 'Message', value: content.substring(0, 1024) },
{
name: 'Channels',
value: Array.from(channels)
.map((id) => `<#${id}>`)
.join(', '),
},
],
color: 0xe74c3c,
footer: { text: `User ID: ${ctx.author.id}` },
},
});
}
});