From 4c545fac0754c3b1871afc841c470d5ce04aa887 Mon Sep 17 00:00:00 2001 From: AnxietyisReal <96593068+AnxietyisReal@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:48:24 +1100 Subject: [PATCH] i hope i did it right. (Sync from TAEMBO/IRTGaming-bot) --- src/client.ts | 4 ++-- src/events/messageCreate.ts | 32 ++++++++++++++++---------------- src/typings/interfaces.d.ts | 17 ++++++++++------- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/client.ts b/src/client.ts index 156aac0..b8aa657 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,7 +1,7 @@ import Discord, { Client, WebhookClient, GatewayIntentBits, Partials } from 'discord.js'; import fs from 'node:fs'; import timeNames from './timeNames'; -import { Punishment, formatTimeOpt, Tokens, Config } from './typings/interfaces'; +import { Punishment, formatTimeOpt, Tokens, Config, repeatedMessages } from './typings/interfaces'; import { bannedWords, bonkCount, userLevels, punishments } from './schoolClassroom'; import MPDB from './models/MPServer'; import axios from 'axios'; @@ -38,7 +38,7 @@ export default class TClient extends Client { punishments: punishments; bonkCount: bonkCount; bannedWords: bannedWords; - repeatedMessages: any; + repeatedMessages: repeatedMessages; statsGraph: number; constructor(){ diff --git a/src/events/messageCreate.ts b/src/events/messageCreate.ts index 3d13256..70514cb 100644 --- a/src/events/messageCreate.ts +++ b/src/events/messageCreate.ts @@ -20,21 +20,21 @@ export default { message.channel.send('That word is banned here.').then((x)=>setTimeout(()=>x.delete(), 5000)); if (client.repeatedMessages[message.author.id]){ // add this message to the list - client.repeatedMessages[message.author.id].set(message.createdTimestamp, {cont: 0, ch: message.channelId}); + client.repeatedMessages[message.author.id].data.set(message.createdTimestamp, {cont: 0, ch: message.channelId}); // reset timeout - clearTimeout(client.repeatedMessages[message.author.id].to); - client.repeatedMessages[message.author.id].to = setTimeout(onTimeout, 30000); + clearTimeout(client.repeatedMessages[message.author.id].timeout); + client.repeatedMessages[message.author.id].timeout = setTimeout(onTimeout, 30000); // this is the time in which 4 messages have to be sent, in milliseconds (ms) const threshold = 30000; // message mustve been sent after (now - threshold), so purge those that were sent earlier - client.repeatedMessages[message.author.id] = client.repeatedMessages[message.author.id].filter((x, i)=>i >= Date.now() - threshold) + client.repeatedMessages[message.author.id].data = client.repeatedMessages[message.author.id].data.filter((x, i)=>i >= Date.now() - threshold) // a spammed message is one that has been sent atleast 4 times in the last threshold milliseconds - const spammedMessage = client.repeatedMessages[message.author.id]?.find((x)=>{ - return client.repeatedMessages[message.author.id].size >= 4; + const spammedMessage = client.repeatedMessages[message.author.id]?.data.find((x)=>{ + return client.repeatedMessages[message.author.id].data.size >= 4; }); // if a spammed message exists; @@ -46,9 +46,9 @@ export default { } } else { client.repeatedMessages[message.author.id] = new client.collection(); - client.repeatedMessages[message.author.id].set(message.createdTimestamp, {cont: 0, ch: message.channelId}); + client.repeatedMessages[message.author.id].data.set(message.createdTimestamp, {cont: 0, ch: message.channelId}); // autodelete after 30 secs - client.repeatedMessages[message.author.id].to = setTimeout(onTimeout, 30000); + client.repeatedMessages[message.author.id].timeout = setTimeout(onTimeout, 30000); } } if (message.content.toLowerCase().includes('discord.gg/') && !message.member.roles.cache.has(client.config.mainServer.roles.dcmod) && message.guildId == client.config.mainServer.id && !Whitelist.includes(message.channelId)) { @@ -56,14 +56,14 @@ export default { message.delete().catch(err=>console.log('advertisement automod; msg got possibly deleted by another bot.')) message.channel.send('Advertising other Discord servers is not allowed.').then(x=>setTimeout(()=>x.delete(), 10000)) if (client.repeatedMessages[message.author.id]){ - client.repeatedMessages[message.author.id].set(message.createdTimestamp,{cont:1,ch:message.channelId}); + client.repeatedMessages[message.author.id].data.set(message.createdTimestamp,{cont:1,ch:message.channelId}); - clearTimeout(client.repeatedMessages[message.author.id].to); - client.repeatedMessages[message.author.id].to = setTimeout(onTimeout, 60000); + clearTimeout(client.repeatedMessages[message.author.id].timeout); + client.repeatedMessages[message.author.id].timeout = setTimeout(onTimeout, 60000); const threshold = 60000; - client.repeatedMessages[message.author.id] = client.repeatedMessages[message.author.id].filter((x:any, i:number)=> i >= Date.now() - threshold) - const spammedMessage = client.repeatedMessages[message.author.id]?.find((x:any)=>{ - return client.repeatedMessages[message.author.id].filter((y:any)=>x.cont === y.cont).size >= 4; + client.repeatedMessages[message.author.id].data = client.repeatedMessages[message.author.id].data.filter((x, i)=> i >= Date.now() - threshold) + const spammedMessage = client.repeatedMessages[message.author.id]?.data.find((x)=>{ + return client.repeatedMessages[message.author.id].data.filter((y)=>x.cont === y.cont).size >= 4; }); if (spammedMessage){ @@ -72,8 +72,8 @@ export default { } }else{ client.repeatedMessages[message.author.id] = new client.collection(); - client.repeatedMessages[message.author.id].set(message.createdTimestamp, {cont: 1, ch: message.channelId}); - client.repeatedMessages[message.author.id].to = setTimeout(onTimeout, 60000); + client.repeatedMessages[message.author.id].data.set(message.createdTimestamp, {cont: 1, ch: message.channelId}); + client.repeatedMessages[message.author.id].timeout = setTimeout(onTimeout, 60000); } } diff --git a/src/typings/interfaces.d.ts b/src/typings/interfaces.d.ts index bb2837a..c4ece0e 100644 --- a/src/typings/interfaces.d.ts +++ b/src/typings/interfaces.d.ts @@ -1,4 +1,4 @@ -import Discord, { ColorResolvable } from 'discord.js'; +import Discord from 'discord.js'; export interface UserLevels { messages: number, @@ -13,6 +13,9 @@ export interface punOpt { reason?: string, interaction?: Discord.ChatInputCommandInteraction<"cached"> } +export interface repeatedMessages { + [key:string]: {data: Discord.Collection, timeout: NodeJS.Timeout} +} export interface Punishment { id: number; type: string; @@ -127,12 +130,12 @@ export interface Tokens { webhook_url_test: string } export interface Config { - embedColor: ColorResolvable, - embedColorGreen: ColorResolvable, - embedColorYellow: ColorResolvable, - embedColorRed: ColorResolvable, - embedColorBCA: ColorResolvable, - embedColorXmas: ColorResolvable, + embedColor: Discord.ColorResolvable, + embedColorGreen: Discord.ColorResolvable, + embedColorYellow: Discord.ColorResolvable, + embedColorRed: Discord.ColorResolvable, + embedColorBCA: Discord.ColorResolvable, + embedColorXmas: Discord.ColorResolvable, LRSstart: number, whitelistedServers: Array, botSwitches: botSwitches,