pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 1 | import { LinkCheck, MalwareCheck, NSFWCheck, SizeCheck, TestString, TestImage } from '../automations/unscan.js' |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 2 | import { Message } from 'discord.js' |
| 3 | |
| 4 | export const event = 'messageCreate' |
| 5 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 6 | export async function callback(client, message) { |
| 7 | if (message.author.bot) return |
| 8 | if (message.channel.type === 'dm') return |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 9 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 10 | let content = message.content.toLowerCase() || '' |
| 11 | let config = await client.memory.readGuildInfo(message.guild.id); |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 12 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 13 | 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 | } |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 24 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 25 | 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; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 59 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 60 | if (await LinkCheck(message)) { |
| 61 | await message.delete() |
| 62 | return toLog(message, 'link') |
| 63 | } |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 64 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 65 | 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 | } |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 72 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 73 | 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 | } |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | async function toLog(message: Message, reason: string, data?: any) { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 99 | // log(message.guild.id, {type: reason, data: data}) |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 100 | } |