pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 1 | import { LinkCheck, MalwareCheck, NSFWCheck, SizeCheck, TestString, TestImage } from '../reflex/scanners.js' |
| 2 | import logAttachment from '../premium/attachmentLogs.js' |
| 3 | import createLogException from '../utils/createLogException.js' |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame^] | 4 | import getEmojiByName from '../utils/getEmojiByName.js'; |
| 5 | import client from '../utils/client.js'; |
| 6 | import {callback as a} from '../reflex/statsChannelUpdate.js' |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 7 | |
| 8 | export const event = 'messageCreate' |
| 9 | |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame^] | 10 | export async function callback(_, message) { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 11 | if(!message) return; |
| 12 | if (message.author.bot) return |
| 13 | if (message.channel.type === 'dm') return |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame^] | 14 | try { await a(client, await message.guild.members.fetch(message.author.id)); } catch(e) { console.log(e)} |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 15 | |
| 16 | const { log, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger |
| 17 | |
| 18 | let fileNames = await logAttachment(message); |
| 19 | |
| 20 | let content = message.content.toLowerCase() || '' |
| 21 | let config = await client.memory.readGuildInfo(message.guild.id); |
| 22 | const filter = getEmojiByName("ICONS.FILTER") |
| 23 | let attachmentJump = "" |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 24 | if (config.logging.attachments.saved[message.channel.id + message.id]) { attachmentJump = ` [[View attachments]](${config.logging.attachments.saved[message.channel.id + message.id]})` } |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 25 | let list = { |
| 26 | messageId: entry(message.id, `\`${message.id}\``), |
| 27 | sentBy: entry(message.author.id, renderUser(message.author)), |
| 28 | sentIn: entry(message.channel.id, renderChannel(message.channel)), |
| 29 | deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 30 | mentions: message.mentions.users.size, |
| 31 | attachments: entry(message.attachments.size, message.attachments.size + attachmentJump), |
| 32 | repliedTo: entry( |
| 33 | message.reference ? message.reference.messageId : null, |
| 34 | message.reference ? `[[Jump to message]](https://discord.com/channels/${message.guild.id}/${message.channel.id}/${message.reference.messageId})` : "None" |
| 35 | ) |
| 36 | } |
| 37 | |
| 38 | if (config.filters.invite.enabled) { |
| 39 | if (!config.filters.invite.allowed.users.includes(message.author.id) || |
| 40 | !config.filters.invite.allowed.channels.includes(message.channel.id) || |
| 41 | !message.author.roles.cache.some(role => config.filters.invite.allowed.roles.includes(role.id)) |
| 42 | ) { |
| 43 | if ((/(?:https?:\/\/)?discord(?:app)?\.(?:com\/invite|gg)\/[a-zA-Z0-9]+\/?/.test(content))) { |
| 44 | createLogException(message.guild.id, message.channel.id, message.id) |
| 45 | message.delete(); |
| 46 | let data = { |
| 47 | meta: { |
| 48 | type: 'messageDelete', |
| 49 | displayName: 'Message Deleted (Automated, Contained Invite)', |
| 50 | calculateType: 'autoModeratorDeleted', |
| 51 | color: NucleusColors.red, |
| 52 | emoji: 'MESSAGE.DELETE', |
| 53 | timestamp: new Date().getTime() |
| 54 | }, |
| 55 | separate: { |
| 56 | start: filter + " Contained invite\n\n" + (content ? `**Message:**\n\`\`\`${content}\`\`\`` : '**Message:** *Message had no content*'), |
| 57 | }, |
| 58 | list: list, |
| 59 | hidden: { |
| 60 | guild: message.channel.guild.id |
| 61 | } |
| 62 | } |
| 63 | return log(data); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if (fileNames.files.length > 0) { |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 69 | for (let element of fileNames.files) { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 70 | if(!message) return; |
| 71 | let url = element.url ? element.url : element.local |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame^] | 72 | if (url !== undefined) { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 73 | if(/\.(jpg|jpeg|png|gif|gifv|webm|webp|mp4|wav|mp3|ogg)$/.test(url)) { |
| 74 | if (config.filters.images.NSFW && !message.channel.nsfw) { |
| 75 | if (await NSFWCheck(url)) { |
| 76 | createLogException(message.guild.id, message.channel.id, message.id) |
| 77 | await message.delete() |
| 78 | let data = { |
| 79 | meta: { |
| 80 | type: 'messageDelete', |
| 81 | displayName: 'Message Deleted', |
| 82 | calculateType: 'autoModeratorDeleted', |
| 83 | color: NucleusColors.red, |
| 84 | emoji: 'MESSAGE.DELETE', |
| 85 | timestamp: new Date().getTime() |
| 86 | }, |
| 87 | separate: { |
| 88 | start: filter + " Image detected as NSFW\n\n" + (content ? `**Message:**\n\`\`\`${content}\`\`\`` : '**Message:** *Message had no content*'), |
| 89 | }, |
| 90 | list: list, |
| 91 | hidden: { |
| 92 | guild: message.channel.guild.id |
| 93 | } |
| 94 | } |
| 95 | return log(data); |
| 96 | } |
| 97 | } |
| 98 | if (config.filters.wordFilter.enabled) { |
| 99 | let text = await TestImage(url) |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame^] | 100 | let check = TestString(text, config.filters.wordFilter.words.loose, config.filters.wordFilter.words.strict) |
| 101 | if(check !== null) { |
| 102 | createLogException(message.guild.id, message.channel.id, message.id) |
| 103 | await message.delete() |
| 104 | let data = { |
| 105 | meta: { |
| 106 | type: 'messageDelete', |
| 107 | displayName: 'Message Deleted', |
| 108 | calculateType: 'autoModeratorDeleted', |
| 109 | color: NucleusColors.red, |
| 110 | emoji: 'MESSAGE.DELETE', |
| 111 | timestamp: new Date().getTime() |
| 112 | }, |
| 113 | separate: { |
| 114 | start: filter + " Image contained filtered word\n\n" + (content ? `**Message:**\n\`\`\`${content}\`\`\`` : '**Message:** *Message had no content*'), |
| 115 | }, |
| 116 | list: list, |
| 117 | hidden: { |
| 118 | guild: message.channel.guild.id |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 119 | } |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 120 | } |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame^] | 121 | return log(data); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | if (config.filters.images.size) { |
| 125 | if(url.match(/\.+(webp|png|jpg)$/gi)) { |
| 126 | if(!await SizeCheck(element)) { |
| 127 | createLogException(message.guild.id, message.channel.id, message.id) |
| 128 | await message.delete() |
| 129 | let data = { |
| 130 | meta: { |
| 131 | type: 'messageDelete', |
| 132 | displayName: 'Message Deleted', |
| 133 | calculateType: 'autoModeratorDeleted', |
| 134 | color: NucleusColors.red, |
| 135 | emoji: 'MESSAGE.DELETE', |
| 136 | timestamp: new Date().getTime() |
| 137 | }, |
| 138 | separate: { |
| 139 | start: filter + " Image was too small\n\n" + (content ? `**Message:**\n\`\`\`${content}\`\`\`` : '**Message:** *Message had no content*'), |
| 140 | }, |
| 141 | list: list, |
| 142 | hidden: { |
| 143 | guild: message.channel.guild.id |
| 144 | } |
| 145 | } |
| 146 | return log(data); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | if (config.filters.malware) { |
| 152 | if (!MalwareCheck(url)) { |
| 153 | createLogException(message.guild.id, message.channel.id, message.id) |
| 154 | await message.delete() |
| 155 | let data = { |
| 156 | meta: { |
| 157 | type: 'messageDelete', |
| 158 | displayName: 'Message Deleted', |
| 159 | calculateType: 'autoModeratorDeleted', |
| 160 | color: NucleusColors.red, |
| 161 | emoji: 'MESSAGE.DELETE', |
| 162 | timestamp: new Date().getTime() |
| 163 | }, |
| 164 | separate: { |
| 165 | start: filter + " File detected as malware\n\n" + (content ? `**Message:**\n\`\`\`${content}\`\`\`` : '**Message:** *Message had no content*'), |
| 166 | }, |
| 167 | list: list, |
| 168 | hidden: { |
| 169 | guild: message.channel.guild.id |
| 170 | } |
| 171 | } |
| 172 | return log(data); |
| 173 | } |
| 174 | } |
| 175 | } |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 176 | }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 177 | } |
| 178 | if(!message) return; |
| 179 | |
| 180 | let linkDetectionTypes = await LinkCheck(message) |
| 181 | if (linkDetectionTypes.length > 0) { |
| 182 | createLogException(message.guild.id, message.channel.id, message.id) |
| 183 | await message.delete() |
| 184 | let data = { |
| 185 | meta: { |
| 186 | type: 'messageDelete', |
| 187 | displayName: `Message Deleted`, |
| 188 | calculateType: 'autoModeratorDeleted', |
| 189 | color: NucleusColors.red, |
| 190 | emoji: 'MESSAGE.DELETE', |
| 191 | timestamp: new Date().getTime() |
| 192 | }, |
| 193 | separate: { |
| 194 | start: filter + ` Link filtered as ${linkDetectionTypes[0].toLowerCase()}\n\n` + (content ? `**Message:**\n\`\`\`${content}\`\`\`` : '**Message:** *Message had no content*'), |
| 195 | }, |
| 196 | list: list, |
| 197 | hidden: { |
| 198 | guild: message.channel.guild.id |
| 199 | } |
| 200 | } |
| 201 | return log(data); |
| 202 | } |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame^] | 203 | |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 204 | if (config.filters.wordFilter.enabled) { |
| 205 | let check = TestString(content, config.filters.wordFilter.words.loose, config.filters.wordFilter.words.strict) |
| 206 | if(check !== null) { |
| 207 | createLogException(message.guild.id, message.channel.id, message.id) |
| 208 | await message.delete() |
| 209 | let data = { |
| 210 | meta: { |
| 211 | type: 'messageDelete', |
| 212 | displayName: 'Message Deleted', |
| 213 | calculateType: 'autoModeratorDeleted', |
| 214 | color: NucleusColors.red, |
| 215 | emoji: 'MESSAGE.DELETE', |
| 216 | timestamp: new Date().getTime() |
| 217 | }, |
| 218 | separate: { |
| 219 | start: filter + ` Message contained filtered word\n\n` + (content ? `**Message:**\n\`\`\`${content}\`\`\`` : '**Message:** *Message had no content*'), |
| 220 | }, |
| 221 | list: list, |
| 222 | hidden: { |
| 223 | guild: message.channel.guild.id |
| 224 | } |
| 225 | } |
| 226 | return log(data); |
| 227 | } |
| 228 | } |
| 229 | |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame^] | 230 | if (config.filters.pings.everyone && message.mentions.everyone) { |
| 231 | let data = { |
| 232 | meta: { |
| 233 | type: 'everyonePing', |
| 234 | displayName: 'Everyone Pinged', |
| 235 | calculateType: 'messageMassPing', |
| 236 | color: NucleusColors.yellow, |
| 237 | emoji: 'MESSAGE.PING.EVERYONE', |
| 238 | timestamp: new Date().getTime() |
| 239 | }, |
| 240 | separate: { |
| 241 | start: content ? `**Message:**\n\`\`\`${content}\`\`\`` : '**Message:** *Message had no content*', |
| 242 | }, |
| 243 | list: list, |
| 244 | hidden: { |
| 245 | guild: message.channel.guild.id |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 246 | } |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 247 | } |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame^] | 248 | return log(data); |
| 249 | } |
| 250 | if (config.filters.pings.roles) { |
| 251 | for(let role of message.mentions.roles) { |
| 252 | if(!message) return; |
| 253 | if (!config.filters.pings.allowed.roles.includes(role.id)) { |
| 254 | createLogException(message.guild.id, message.channel.id, message.id) |
| 255 | await message.delete() |
| 256 | let data = { |
| 257 | meta: { |
| 258 | type: 'rolePing', |
| 259 | displayName: 'Role Pinged', |
| 260 | calculateType: 'messageMassPing', |
| 261 | color: NucleusColors.yellow, |
| 262 | emoji: 'MESSAGE.PING.ROLE', |
| 263 | timestamp: new Date().getTime() |
| 264 | }, |
| 265 | separate: { |
| 266 | start: content ? `**Message:**\n\`\`\`${content}\`\`\`` : '**Message:** *Message had no content*', |
| 267 | }, |
| 268 | list: list, |
| 269 | hidden: { |
| 270 | guild: message.channel.guild.id |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 271 | } |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 272 | } |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame^] | 273 | return log(data); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 274 | } |
| 275 | } |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame^] | 276 | } |
| 277 | if (message.mentions.users.size >= config.filters.pings.mass && config.filters.pings.mass) { |
| 278 | createLogException(message.guild.id, message.channel.id, message.id) |
| 279 | await message.delete() |
| 280 | let data = { |
| 281 | meta: { |
| 282 | type: 'massPing', |
| 283 | displayName: `Mass Ping`, |
| 284 | calculateType: 'messageMassPing', |
| 285 | color: NucleusColors.yellow, |
| 286 | emoji: 'MESSAGE.PING.MASS', |
| 287 | timestamp: new Date().getTime() |
| 288 | }, |
| 289 | separate: { |
| 290 | start: content ? `**Message:**\n\`\`\`${content}\`\`\`` : '**Message:** *Message had no content*', |
| 291 | }, |
| 292 | list: list, |
| 293 | hidden: { |
| 294 | guild: message.channel.guild.id |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 295 | } |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 296 | } |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame^] | 297 | return log(data); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 298 | } |
| 299 | } |