blob: 2755ff1460a4dc92b9770ad4246cc68a97690e9d [file] [log] [blame]
pineafan377794f2022-04-18 19:01:01 +01001import { LinkCheck, MalwareCheck, NSFWCheck, SizeCheck, TestString, TestImage } from '../automations/unscan.js'
pineafan32767212022-03-14 21:27:39 +00002import { Message } from 'discord.js'
pineafanda6e5342022-07-03 10:03:16 +01003import client from '../utils/client.js'
pineafan32767212022-03-14 21:27:39 +00004
5export const event = 'messageCreate'
6
pineafane625d782022-05-09 18:04:32 +01007export async function callback(client, message) {
pineafanda6e5342022-07-03 10:03:16 +01008 const { log, NucleusColors, entry, renderUser } = client.logger
pineafane625d782022-05-09 18:04:32 +01009 if (message.author.bot) return
10 if (message.channel.type === 'dm') return
pineafan32767212022-03-14 21:27:39 +000011
pineafane625d782022-05-09 18:04:32 +010012 let content = message.content.toLowerCase() || ''
13 let config = await client.memory.readGuildInfo(message.guild.id);
pineafan32767212022-03-14 21:27:39 +000014
pineafane625d782022-05-09 18:04:32 +010015 if (config.filters.invite.enabled) {
16 if (!config.filters.invite.allowed.users.includes(message.author.id) ||
17 !config.filters.invite.allowed.channels.includes(message.channel.id) ||
18 !message.author.roles.cache.some(role => config.filters.invite.allowed.roles.includes(role.id))
19 ) {
20 if ((/(?:https?:\/\/)?discord(?:app)?\.(?:com\/invite|gg)\/[a-zA-Z0-9]+\/?/.test(content))) {
pineafanda6e5342022-07-03 10:03:16 +010021 return message.delete();
pineafane625d782022-05-09 18:04:32 +010022 }
23 }
24 }
pineafan32767212022-03-14 21:27:39 +000025
pineafane625d782022-05-09 18:04:32 +010026 let attachments = message.attachments.map(element => element)
27 attachments = [...attachments, ...content.match(
28 /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/gi
29 ) ?? []].filter(element => (element.url ? element.url : element))
30 if (attachments.length > 0) {
31 attachments.forEach(async element => {
32 if(!message) return;
33 let url = element.url ? element.url : element
34 if (url != undefined) {
35 if(/\.+(webp|png|jpg|jpeg|bmp)/.test(url)) {
36 if (config.filters.images.NSFW && !message.channel.nsfw) {
37 if (await NSFWCheck(url)) {
pineafanda6e5342022-07-03 10:03:16 +010038 return await message.delete()
pineafane625d782022-05-09 18:04:32 +010039 }
40 }
41 if (config.filters.images.size) {
42 if(!url.match(/\.+(webp|png|jpg)$/gi)) return
43 if(!await SizeCheck(element)) {
pineafanda6e5342022-07-03 10:03:16 +010044 return await message.delete()
pineafane625d782022-05-09 18:04:32 +010045 }
46 }
47 }
48 if (config.filters.malware) {
49 if (!MalwareCheck(url)) {
pineafanda6e5342022-07-03 10:03:16 +010050 return await message.delete()
pineafane625d782022-05-09 18:04:32 +010051 }
52 }
53 }
54 });
55 }
56 if(!message) return;
pineafan32767212022-03-14 21:27:39 +000057
pineafane625d782022-05-09 18:04:32 +010058 if (await LinkCheck(message)) {
pineafanda6e5342022-07-03 10:03:16 +010059 return await message.delete()
pineafane625d782022-05-09 18:04:32 +010060 }
pineafan32767212022-03-14 21:27:39 +000061
pineafane625d782022-05-09 18:04:32 +010062 if (config.filters.wordFilter.enabled) {
63 let check = TestString(content, config.filters.wordFilter.words.loose, config.filters.wordFilter.words.strict)
pineafanda6e5342022-07-03 10:03:16 +010064 if(check !== null) {
65 return await message.delete()
pineafane625d782022-05-09 18:04:32 +010066 }
67 }
pineafan32767212022-03-14 21:27:39 +000068
pineafane625d782022-05-09 18:04:32 +010069 if (!config.filters.pings.allowed.users.includes(message.author.id) ||
70 !config.filters.pings.allowed.channels.includes(message.channel.id) ||
71 !message.author.roles.cache.some(role => config.filters.pings.allowed.roles.includes(role.id))
72 ) {
73 if (config.filters.pings.everyone && message.mentions.everyone) {
pineafanda6e5342022-07-03 10:03:16 +010074 return message.delete();
pineafane625d782022-05-09 18:04:32 +010075 }
76 if (config.filters.pings.roles) {
77 for(let role of message.mentions.roles) {
78 if(!message) return;
79 if (!config.filters.pings.allowed.roles.includes(role.id)) {
pineafanda6e5342022-07-03 10:03:16 +010080 return message.delete();
pineafane625d782022-05-09 18:04:32 +010081 }
82 }
83 }
84 if(!message) return;
85 if (message.mentions.users.size >= config.filters.pings.mass && config.filters.pings.mass) {
pineafanda6e5342022-07-03 10:03:16 +010086 return message.delete();
pineafane625d782022-05-09 18:04:32 +010087 }
88 }
pineafan32767212022-03-14 21:27:39 +000089}