From eae95775e54f53d889b7d8954222c4ae64a6485d Mon Sep 17 00:00:00 2001 From: toast-ts <96593068+toast-ts@users.noreply.github.com> Date: Mon, 4 Sep 2023 15:04:34 +1000 Subject: [PATCH] Move automoderation to dedicated file --- src/events/messageCreate.ts | 42 +++++++------------------------------ src/funcs/Automod.ts | 36 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 35 deletions(-) create mode 100644 src/funcs/Automod.ts diff --git a/src/events/messageCreate.ts b/src/events/messageCreate.ts index 61148aa..a7ae921 100644 --- a/src/events/messageCreate.ts +++ b/src/events/messageCreate.ts @@ -2,49 +2,21 @@ import Discord from 'discord.js'; import TClient from '../client.js'; import Response from '../funcs/ResponseSystem.js'; import LogPrefix from '../helpers/LogPrefix.js'; +import Automoderator from '../funcs/Automod.js'; + export default { async run(client:TClient, message:Discord.Message){ if (message.author.bot) return; if (!message.inGuild()) return (client.channels.resolve(client.config.mainServer.channels.logs) as Discord.TextChannel).send({content: `<:fish_unamused:1083675172407623711> <@${client.config.whitelist[0]}>\n**${message.author.username}** tried to send me a DM, their message is:\`\`\`${message.content}\`\`\``, allowedMentions: {parse: ['users']}}); - - const msgarr = message.content.toLowerCase().replaceAll(/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?\n?0-9]/g, '').split(' '); let automodded: boolean; - //const Whitelist = [] // Array of channel ids for automod to be disabled in (Disables bannedWords and advertisement, mind you.) - - async function repeatedMessages(thresholdTime:number, thresholdAmount:number, type:string, muteTime:string, muteReason:string){ - if (client.repeatedMessages[message.author.id]){ - // Add message to the list - client.repeatedMessages[message.author.id].data.set(message.createdTimestamp, {type, channel: message.channelId}); - - // Reset the timeout - clearTimeout(client.repeatedMessages[message.author.id].timeout); - client.repeatedMessages[message.author.id].timeout = setTimeout(()=>delete client.repeatedMessages[message.author.id], thresholdTime); - - // Message sent after (now - threshold), so purge those that were sent earlier - client.repeatedMessages[message.author.id].data = client.repeatedMessages[message.author.id].data.filter((x,i)=>i>=Date.now() - thresholdTime); - - // A spammed message is one that has been sent within the threshold parameters - const spammedMessage = client.repeatedMessages[message.author.id].data.find(x=>{ - return client.repeatedMessages[message.author.id].data.filter(y=>x.type===y.type).size >= thresholdAmount; - }); - if (spammedMessage){ - delete client.repeatedMessages[message.author.id]; - await client.punishments.addPunishment('mute', {time: muteTime}, (client.user as Discord.User).id, `[AUTOMOD] ${muteReason}`, message.author, message.member as Discord.GuildMember); - } - } else { - client.repeatedMessages[message.author.id] = {data: new client.collection(), timeout: setTimeout(()=>delete client.repeatedMessages[message.author.id], thresholdTime)}; - client.repeatedMessages[message.author.id].data.set(message.createdTimestamp, {type, channel: message.channelId}); - } - } - if (client.config.botSwitches.automod && !message.member.roles.cache.has(client.config.mainServer.roles.admin) && message.guildId == client.config.mainServer.id){ const automodFailReason = 'msg got possibly deleted by another bot.'; - if (await client.bannedWords._content.findById(msgarr)/* && !Whitelist.includes(message.channelId) */){ + if (await client.bannedWords._content.findById(Automoderator.scanMsg(message))/* && !Whitelist.includes(message.channelId) */){ automodded = true; message.delete().catch(()=>console.log(LogPrefix('AUTOMOD-BANNEDWORDS'), automodFailReason)); message.channel.send('That word isn\'t allowed here.').then(x=>setTimeout(()=>x.delete(), 10000)); - await repeatedMessages(30000, 3, 'bw', '30m', 'Constant swearing'); + await Automoderator.repeatedMessages(client, message, 30000, 3, 'bw', '30m', 'Constant swearing'); } else if (message.content.toLowerCase().includes('discord.gg/') && !client.isStaff(message.member as Discord.GuildMember)){ const url = message.content.split(' ').find(x=>x.includes('discord.gg/')); const validInvite = await client.fetchInvite(url).catch(()=>undefined); @@ -52,7 +24,7 @@ export default { automodded = true; message.delete().catch(()=>console.log(LogPrefix('AUTOMOD-ADVERT'), automodFailReason)); message.channel.send('Please don\'t advertise other Discord servers.').then(x=>setTimeout(()=>x.delete(), 15000)); - await repeatedMessages(60000, 2, 'adv', '1h', 'Discord advertisement'); + await Automoderator.repeatedMessages(client, message, 60000, 2, 'adv', '1h', 'Discord advertisement'); } } } @@ -85,8 +57,8 @@ export default { if (incomingArrays.cantRead.some(e=>message.content.toLowerCase().includes(e))) message.reply('https://tenor.com/view/aristocats-george-pen-cap-meticulous-gif-5330931'); if (message.content.toLowerCase().includes('is daggerbot working')) message.reply('https://tenor.com/view/i-still-feel-alive-living-existing-active-singing-gif-14630579'); if (incomingArrays.deadChat.some(e=>message.content.toLowerCase().includes(e))) message.reply('https://cdn.discordapp.com/attachments/925589318276382720/1011333656167579849/F57G5ZS.png'); - if (msgarr.includes('nawdic') && incomingArrays.theyBrokeIt.some(e=>msgarr.includes(e)) && client.isStaff(message.member) && message.channelId !== '516344221452599306') message.reply({embeds: [new client.embed().setTitle('*Nawdic has done an oopsie*').setImage('https://c.tenor.com/JSj9ie_MD9kAAAAC/kopfsch%C3%BCtteln-an-kopf-fassen-oh-no.gif').setColor(client.config.embedColor)]}); - if (msgarr.includes('monster') && incomingArrays.theyBrokeIt.some(e=>msgarr.includes(e)) && client.isStaff(message.member) && message.channelId !== '516344221452599306') message.reply({embeds: [new client.embed().setTitle('*Monster has broken something*').setImage('https://media.tenor.com/ZIzIjb_wvEoAAAAC/face-palm.gif').setColor(client.config.embedColor)]}); + if (Automoderator.scanMsg(message).includes('nawdic') && incomingArrays.theyBrokeIt.some(e=>Automoderator.scanMsg(message).includes(e)) && client.isStaff(message.member) && message.channelId !== '516344221452599306') message.reply({embeds: [new client.embed().setTitle('*Nawdic has done an oopsie*').setImage('https://c.tenor.com/JSj9ie_MD9kAAAAC/kopfsch%C3%BCtteln-an-kopf-fassen-oh-no.gif').setColor(client.config.embedColor)]}); + if (Automoderator.scanMsg(message).includes('monster') && incomingArrays.theyBrokeIt.some(e=>Automoderator.scanMsg(message).includes(e)) && client.isStaff(message.member) && message.channelId !== '516344221452599306') message.reply({embeds: [new client.embed().setTitle('*Monster has broken something*').setImage('https://media.tenor.com/ZIzIjb_wvEoAAAAC/face-palm.gif').setColor(client.config.embedColor)]}); // Failsafe thingy (Toastproof maybe) if (message.content.startsWith('!!_wepanikfrfr') && client.config.whitelist.includes(message.author.id)) (client.guilds.cache.get(message.guildId) as Discord.Guild).commands.set(client.registry).then(()=>message.reply('How did you manage to lose the commands??? Anyways, it\'s re-registered now.')).catch((e:Error)=>message.reply(`Failed to deploy slash commands:\n\`\`\`${e.message}\`\`\``)); } diff --git a/src/funcs/Automod.ts b/src/funcs/Automod.ts new file mode 100644 index 0000000..77854a6 --- /dev/null +++ b/src/funcs/Automod.ts @@ -0,0 +1,36 @@ +import Discord from 'discord.js'; +import TClient from '../client.js'; + +export default class Automoderator { + static Whitelist(message:Discord.Message, ...arr:string[]){// Array of channel ids for automod to be disabled in (Disables bannedWords and advertisement, mind you.) + return arr.includes(message.channelId); + } + static scanMsg(message:Discord.Message){ + return message.content.toLowerCase().replaceAll(/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?\n?0-9]/g, '').split(' ') + } + static async repeatedMessages(client:TClient, message:Discord.Message, thresholdTime:number, thresholdAmount:number, type:string, muteTime:string, muteReason:string){ + if (client.repeatedMessages[message.author.id]){ + // Add message to the list + client.repeatedMessages[message.author.id].data.set(message.createdTimestamp, {type, channel: message.channelId}); + + // Reset the timeout + clearTimeout(client.repeatedMessages[message.author.id].timeout); + client.repeatedMessages[message.author.id].timeout = setTimeout(()=>delete client.repeatedMessages[message.author.id], thresholdTime); + + // Message sent after (now - threshold), so purge those that were sent earlier + client.repeatedMessages[message.author.id].data = client.repeatedMessages[message.author.id].data.filter((x,i)=>i>=Date.now() - thresholdTime); + + // A spammed message is one that has been sent within the threshold parameters + const spammedMessage = client.repeatedMessages[message.author.id].data.find(x=>{ + return client.repeatedMessages[message.author.id].data.filter(y=>x.type===y.type).size >= thresholdAmount; + }); + if (spammedMessage){ + delete client.repeatedMessages[message.author.id]; + await client.punishments.addPunishment('mute', {time: muteTime}, (client.user as Discord.User).id, `[AUTOMOD] ${muteReason}`, message.author, message.member as Discord.GuildMember); + } + } else { + client.repeatedMessages[message.author.id] = {data: new client.collection(), timeout: setTimeout(()=>delete client.repeatedMessages[message.author.id], thresholdTime)}; + client.repeatedMessages[message.author.id].data.set(message.createdTimestamp, {type, channel: message.channelId}); + } + } +}