fileoverview description module import type { Message , TextChannel } from "eris" ; import type { HibikiClient } from "../../classes/Client" ; import { localizePunishments , tagUser } from "../../utils/format" ; import { validItems } from "../../utils/validItems" ; import { punishMute , punishPurge , punishWarn } from "./punishments" ; const punishedUsers = [ ] ; const defaultPunishments = ( validItems . find ( ( item ) => item . id === "spamPunishments" ) . default as unknown ) as string [ ] ; const defaultThreshold = validItems . find ( ( item ) => item . id === "spamThreshold" ) . default as number ; export async function automodAntiSpam ( msg , bot , cfg ) { const string = bot . localeSystem . getLocaleFunction ( cfg ?. guildLocale ? cfg ?. guildLocale : bot . config . defaultLocale ) ; const spam = bot . antiSpam . filter ( ( s ) => s . guild === msg . channel . guild . id && s . id === msg . author . id && new Date ( ) . getTime ( ) - s . date < 2500 ) ; if ( ! cfg . spamThreshold ) cfg . spamThreshold = defaultThreshold ; if ( punishedUsers . indexOf ( msg . author . id ) !== - 1 ) return ; if ( spam . length >= cfg . spamThreshold ) { punishedUsers . push ( msg . author . id ) ; setTimeout ( ( ) => punishedUsers . splice ( punishedUsers . indexOf ( msg . author . id ) , 1 ) , 5000 ) ; const punishments = cfg . spamPunishments ? cfg . spamPunishments : defaultPunishments ; punishments . forEach ( async ( punishment ) => { switch ( punishment ) { case "Mute" : punishMute ( msg , bot , cfg , spam ) ; break ; case "Purge" : await punishPurge ( msg , bot . antiSpam . filter ( ( s ) => s . guild === msg . channel . guild . id && s . id === msg . author . id && new Date ( ) . getTime ( ) - s . date < 10000 ) . map ( ( s ) => s . msgid ) ) ; break ; case "Warn" : punishWarn ( msg , bot , "Spam (Automod)" ) ; break ; } } ) ; const localizedPunishments = [ ] ; punishments . forEach ( ( p ) => { const punishment = localizePunishments ( string , p ) ; localizedPunishments . push ( punishment ) ; } ) ; if ( cfg . msgOnPunishment ) { const pmsg = await msg . channel . createMessage ( { embed : { title : `🔨 ${ string ( "global.AUTOMOD_PUNISHED" , { member : tagUser ( msg . author ) , reason : string ( "global.SPAMMING" ) , punishments : `${ localizedPunishments . length > 1 ? localizedPunishments . filter ( ( p ) => p !== string ( "moderation.PURGED" ) ) . join ( ` ${ string ( "global.AND" ) } ` ) : localizedPunishments [ 0 ] }` , } ) }` , color : msg . convertHex ( "error" ) , } , } ) ; setTimeout ( ( ) => pmsg . delete ( "Automod message deletion" ) . catch ( ( ) => { } ) , 5000 ) ; } bot . antiSpam . forEach ( ( a ) => { if ( a . id !== msg . author . id ) return ; if ( a . guild !== msg . channel . guild . id ) return ; bot . antiSpam . splice ( bot . antiSpam . indexOf ( a ) , 1 ) ; } ) ; } bot . antiSpam . push ( { date : new Date ( ) . getTime ( ) , id : msg . author . id , guild : msg . channel . guild . id , content : msg . content , msgid : msg . id , } ) ; }