1
0
mirror of https://github.com/toast-ts/Daggerbot-TS.git synced 2024-11-18 08:50:59 -05:00
Daggerbot-TS/src/components/Automod.ts

63 lines
3.9 KiB
TypeScript
Raw Normal View History

2023-12-24 10:21:40 -05:00
import Discord from 'discord.js';
import TClient from '../client.js';
import Logger from '../helpers/Logger.js';
export default class Automoderator {
private static lockQuery:Set<Discord.Snowflake> = new Set();
2024-01-16 06:41:30 -05:00
static scanMsg =(message:Discord.Message)=>message.content.toLowerCase().replaceAll(/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?\n?0-9]|[]|ing\b/g, '').split(' ').join('');
2023-12-24 10:21:40 -05:00
static async repeatedMessages(client:TClient, message:Discord.Message, thresholdTime:number, thresholdAmount:number, type:string, duration:string, reason:string) {
const now = Date.now();
if (!client.repeatedMessages[message.author.id]) client.repeatedMessages[message.author.id] = {type: type, count:1, firstTime:now, timeout: null};
else {
const data = client.repeatedMessages[message.author.id];
if (now - data.firstTime < thresholdTime) {
// If the message is within the threshold time, increment.
data.count++;
if (data.count >= thresholdAmount) {
// If the count has reached the threshold amount, punish the user like most daddy would do to their child.
if (!this.lockQuery.has(message.author.id)) {
this.lockQuery.add(message.author.id);
2024-01-16 11:23:59 -05:00
Logger.console('log', 'AUTOMOD', `Lock acquired for ${message.author.tag} with reason: ${reason}`);
2023-12-24 10:21:40 -05:00
await client.punishments.punishmentAdd('mute', {time: duration}, client.user.id, `AUTOMOD:${reason}`, message.author, message.member as Discord.GuildMember);
setTimeout(()=>{
this.lockQuery.delete(message.author.id);
2024-01-16 11:23:59 -05:00
Logger.console('log', 'AUTOMOD', `Lock released for ${message.author.tag}`);
2023-12-24 10:21:40 -05:00
}, 3500); // Wait 3.5 seconds before releasing the lock.
}
delete client.repeatedMessages[message.author.id];
}
} else {
// If the message is outside the threshold time, reset the count and timestamp.
data.count = 1;
data.firstTime = now;
}
// Reset the timer.
clearTimeout(data.timeout);
data.timeout = setTimeout(()=>delete client.repeatedMessages[message.author.id], thresholdTime);
}
}// Explanation: If spammer sends same message 5 or more times in every channel, they will be served a rotten fish and chips for dinner.
static async crosspostSpam(client:TClient, message:Discord.Message, reason:string) {
if (!client.crosspostSpam[message.author.id]) client.crosspostSpam[message.author.id] = {message: message.content, channelId: [message.channelId]};
else if (!client.crosspostSpam[message.author.id].channelId.includes(message.channelId)) client.crosspostSpam[message.author.id].channelId.push(message.channelId);
if (client.crosspostSpam[message.author.id].channelId.length >= 5) {
if (!this.lockQuery.has(message.author.id)) {
this.lockQuery.add(message.author.id);
Logger.console('log', 'AUTOMOD', `Lock acquired for ${message.author.tag} with reason: ${reason}`);
await client.punishments.punishmentAdd('ban', {}, client.user.id, `AUTOMOD:${reason}`, message.author, message.member as Discord.GuildMember);
setTimeout(()=>{
this.lockQuery.delete(message.author.id);
Logger.console('log', 'AUTOMOD', `Lock released for ${message.author.tag}`);
}, 5000); // Wait 5 seconds before releasing the lock.
}
delete client.crosspostSpam[message.author.id];
}
setTimeout(()=>delete client.crosspostSpam[message.author.id], 180000); // Delete the data after 3 minutes.
2023-12-24 10:21:40 -05:00
}
static async imageOnly(message:Discord.Message) {
const io_channels = ['468896467688620032'];
let deleteReason:string = 'This is an image-only channel and your message did not contain any images.';
if (io_channels.includes(message.channelId) && message.attachments.size < 1 && message.attachments.every(x=>!x.contentType.includes('image/'))) await message.delete().then(()=>message.channel.send(deleteReason).then((msg:Discord.Message)=>setTimeout(()=>msg.delete(), 8000)));
}
2023-12-24 10:21:40 -05:00
}