blob: 4e99c61132396bfcdd4cef912de1cce76ecb22dc [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'
3
4export const event = 'messageCreate'
5
pineafane625d782022-05-09 18:04:32 +01006export async function callback(client, message) {
7 if (message.author.bot) return
8 if (message.channel.type === 'dm') return
pineafan32767212022-03-14 21:27:39 +00009
pineafane625d782022-05-09 18:04:32 +010010 let content = message.content.toLowerCase() || ''
11 let config = await client.memory.readGuildInfo(message.guild.id);
pineafan32767212022-03-14 21:27:39 +000012
pineafane625d782022-05-09 18:04:32 +010013 if (config.filters.invite.enabled) {
14 if (!config.filters.invite.allowed.users.includes(message.author.id) ||
15 !config.filters.invite.allowed.channels.includes(message.channel.id) ||
16 !message.author.roles.cache.some(role => config.filters.invite.allowed.roles.includes(role.id))
17 ) {
18 if ((/(?:https?:\/\/)?discord(?:app)?\.(?:com\/invite|gg)\/[a-zA-Z0-9]+\/?/.test(content))) {
19 message.delete();
20 return toLog(message, 'invite', content.match(/(?:https?:\/\/)?discord(?:app)?\.(?:com\/invite|gg)\/[a-zA-Z0-9]+\/?/))
21 }
22 }
23 }
pineafan32767212022-03-14 21:27:39 +000024
pineafane625d782022-05-09 18:04:32 +010025 let attachments = message.attachments.map(element => element)
26 attachments = [...attachments, ...content.match(
27 /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/gi
28 ) ?? []].filter(element => (element.url ? element.url : element))
29 if (attachments.length > 0) {
30 attachments.forEach(async element => {
31 if(!message) return;
32 let url = element.url ? element.url : element
33 if (url != undefined) {
34 if(/\.+(webp|png|jpg|jpeg|bmp)/.test(url)) {
35 if (config.filters.images.NSFW && !message.channel.nsfw) {
36 if (await NSFWCheck(url)) {
37 await message.delete()
38 return toLog(message, 'NSFW', url)
39 }
40 }
41 if (config.filters.images.size) {
42 if(!url.match(/\.+(webp|png|jpg)$/gi)) return
43 if(!await SizeCheck(element)) {
44 await message.delete()
45 return toLog(message, 'size', url)
46 }
47 }
48 }
49 if (config.filters.malware) {
50 if (!MalwareCheck(url)) {
51 await message.delete()
52 return toLog(message, 'malware', url)
53 }
54 }
55 }
56 });
57 }
58 if(!message) return;
pineafan32767212022-03-14 21:27:39 +000059
pineafane625d782022-05-09 18:04:32 +010060 if (await LinkCheck(message)) {
61 await message.delete()
62 return toLog(message, 'link')
63 }
pineafan32767212022-03-14 21:27:39 +000064
pineafane625d782022-05-09 18:04:32 +010065 if (config.filters.wordFilter.enabled) {
66 let check = TestString(content, config.filters.wordFilter.words.loose, config.filters.wordFilter.words.strict)
67 if(check != "none") {
68 await message.delete()
69 return toLog(message, 'wordFilter', content)
70 }
71 }
pineafan32767212022-03-14 21:27:39 +000072
pineafane625d782022-05-09 18:04:32 +010073 if (!config.filters.pings.allowed.users.includes(message.author.id) ||
74 !config.filters.pings.allowed.channels.includes(message.channel.id) ||
75 !message.author.roles.cache.some(role => config.filters.pings.allowed.roles.includes(role.id))
76 ) {
77 if (config.filters.pings.everyone && message.mentions.everyone) {
78 message.delete();
79 return toLog(message, 'mention everyone')
80 }
81 if (config.filters.pings.roles) {
82 for(let role of message.mentions.roles) {
83 if(!message) return;
84 if (!config.filters.pings.allowed.roles.includes(role.id)) {
85 message.delete();
86 return toLog(message, 'mention role')
87 }
88 }
89 }
90 if(!message) return;
91 if (message.mentions.users.size >= config.filters.pings.mass && config.filters.pings.mass) {
92 message.delete();
93 return toLog(message, 'Mass Pings')
94 }
95 }
pineafan32767212022-03-14 21:27:39 +000096}
97
98async function toLog(message: Message, reason: string, data?: any) {
pineafane625d782022-05-09 18:04:32 +010099 // log(message.guild.id, {type: reason, data: data})
pineafan32767212022-03-14 21:27:39 +0000100}