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 readConfig from '../utils/readConfig.js' |
| 3 | import { Message } from 'discord.js' |
| 4 | |
| 5 | export const event = 'messageCreate' |
| 6 | |
| 7 | export async function callback(_, message) { |
| 8 | if (message.author.bot) return |
| 9 | if (message.channel.type === 'dm') return |
| 10 | |
| 11 | let content = message.content.toLowerCase() || '' |
| 12 | let config = await readConfig(message.guild.id); |
| 13 | |
| 14 | if (config.filters.invite.enabled) { |
| 15 | if (!config.filters.invite.allowed.users.includes(message.author.id) || |
| 16 | !config.filters.invite.allowed.channels.includes(message.channel.id) || |
| 17 | !message.author.roles.cache.some(role => config.filters.invite.allowed.roles.includes(role.id)) |
| 18 | ) { |
| 19 | if ((/(?:https?:\/\/)?discord(?:app)?\.(?:com\/invite|gg)\/[a-zA-Z0-9]+\/?/.test(content))) { |
| 20 | message.delete(); |
| 21 | return toLog(message, 'invite', content.match(/(?:https?:\/\/)?discord(?:app)?\.(?:com\/invite|gg)\/[a-zA-Z0-9]+\/?/)) |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | 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)) { |
| 38 | await message.delete() |
| 39 | return toLog(message, 'NSFW', url) |
| 40 | } |
| 41 | } |
| 42 | if (config.filters.images.size) { |
| 43 | if(!url.match(/\.+(webp|png|jpg)$/gi)) return |
| 44 | if(!await SizeCheck(element)) { |
| 45 | await message.delete() |
| 46 | return toLog(message, 'size', url) |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | if (config.filters.malware) { |
| 51 | if (!MalwareCheck(url)) { |
| 52 | await message.delete() |
| 53 | return toLog(message, 'malware', url) |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | }); |
| 58 | } |
| 59 | if(!message) return; |
| 60 | |
| 61 | if (await LinkCheck(message)) { |
| 62 | await message.delete() |
| 63 | return toLog(message, 'link') |
| 64 | } |
| 65 | |
| 66 | if (config.filters.wordFilter.enabled) { |
| 67 | let check = TestString(content, config.filters.wordFilter.words.loose, config.filters.wordFilter.words.strict) |
| 68 | if(check != "none") { |
| 69 | await message.delete() |
| 70 | return toLog(message, 'wordFilter', content) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if (!config.filters.pings.allowed.users.includes(message.author.id) || |
| 75 | !config.filters.pings.allowed.channels.includes(message.channel.id) || |
| 76 | !message.author.roles.cache.some(role => config.filters.pings.allowed.roles.includes(role.id)) |
| 77 | ) { |
| 78 | if (config.filters.pings.everyone && message.mentions.everyone) { |
| 79 | message.delete(); |
| 80 | return toLog(message, 'mention everyone') |
| 81 | } |
| 82 | if (config.filters.pings.roles) { |
| 83 | for(let role of message.mentions.roles) { |
| 84 | if(!message) return; |
| 85 | if (!config.filters.pings.allowed.roles.includes(role.id)) { |
| 86 | message.delete(); |
| 87 | return toLog(message, 'mention role') |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | if(!message) return; |
| 92 | if (message.mentions.users.size >= config.filters.pings.mass && config.filters.pings.mass) { |
| 93 | message.delete(); |
| 94 | return toLog(message, 'Mass Pings') |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | async function toLog(message: Message, reason: string, data?: any) { |
| 100 | // log(message.guild.id, {type: reason, data: data}) |
| 101 | } |