blob: 7e4a52f8e84b644940def92f513c5c2a3b9cc99b [file] [log] [blame]
Skyler Grey75ea9172022-08-06 10:22:23 +01001// @ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01002import { HaikuClient } from "jshaiku";
Skyler Grey75ea9172022-08-06 10:22:23 +01003import {
4 LinkCheck,
5 MalwareCheck,
6 NSFWCheck,
7 SizeCheck,
8 TestString,
9 TestImage
10} from "../reflex/scanners.js";
pineafan63fc5e22022-08-04 22:04:10 +010011import logAttachment from "../premium/attachmentLogs.js";
12import createLogException from "../utils/createLogException.js";
13import getEmojiByName from "../utils/getEmojiByName.js";
14import client from "../utils/client.js";
Skyler Grey75ea9172022-08-06 10:22:23 +010015import { callback as a } from "../reflex/statsChannelUpdate.js";
Skyler Greyc634e2b2022-08-06 17:50:48 +010016import { Message, ThreadChannel } from "discord.js";
pineafan813bdf42022-07-24 10:39:10 +010017
pineafan63fc5e22022-08-04 22:04:10 +010018export const event = "messageCreate";
pineafan813bdf42022-07-24 10:39:10 +010019
pineafan63fc5e22022-08-04 22:04:10 +010020export async function callback(_client: HaikuClient, message: Message) {
Skyler Grey75ea9172022-08-06 10:22:23 +010021 if (!message.guild) return;
pineafan63fc5e22022-08-04 22:04:10 +010022 if (message.author.bot) return;
23 if (message.channel.type === "DM") return;
Skyler Grey75ea9172022-08-06 10:22:23 +010024 try {
25 await a(client, await message.guild.members.fetch(message.author.id));
26 } catch (e) {
27 console.log(e);
28 }
pineafan813bdf42022-07-24 10:39:10 +010029
Skyler Grey75ea9172022-08-06 10:22:23 +010030 const {
31 log,
32 NucleusColors,
33 entry,
34 renderUser,
35 renderDelta,
36 renderChannel
37 } = client.logger;
pineafan813bdf42022-07-24 10:39:10 +010038
pineafan63fc5e22022-08-04 22:04:10 +010039 const fileNames = await logAttachment(message);
pineafan813bdf42022-07-24 10:39:10 +010040
pineafan63fc5e22022-08-04 22:04:10 +010041 const content = message.content.toLowerCase() || "";
42 const config = await client.memory.readGuildInfo(message.guild.id);
43 const filter = getEmojiByName("ICONS.FILTER");
44 let attachmentJump = "";
Skyler Grey75ea9172022-08-06 10:22:23 +010045 if (config.logging.attachments.saved[message.channel.id + message.id]) {
46 attachmentJump = ` [[View attachments]](${
47 config.logging.attachments.saved[message.channel.id + message.id]
48 })`;
49 }
pineafan63fc5e22022-08-04 22:04:10 +010050 const list = {
pineafan813bdf42022-07-24 10:39:10 +010051 messageId: entry(message.id, `\`${message.id}\``),
52 sentBy: entry(message.author.id, renderUser(message.author)),
53 sentIn: entry(message.channel.id, renderChannel(message.channel)),
54 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
55 mentions: message.mentions.users.size,
Skyler Grey75ea9172022-08-06 10:22:23 +010056 attachments: entry(
57 message.attachments.size,
58 message.attachments.size + attachmentJump
59 ),
pineafan813bdf42022-07-24 10:39:10 +010060 repliedTo: entry(
61 message.reference ? message.reference.messageId : null,
Skyler Grey75ea9172022-08-06 10:22:23 +010062 message.reference
63 ? `[[Jump to message]](https://discord.com/channels/${message.guild.id}/${message.channel.id}/${message.reference.messageId})`
64 : "None"
pineafan813bdf42022-07-24 10:39:10 +010065 )
pineafan63fc5e22022-08-04 22:04:10 +010066 };
pineafan813bdf42022-07-24 10:39:10 +010067
68 if (config.filters.invite.enabled) {
Skyler Grey75ea9172022-08-06 10:22:23 +010069 if (
70 !config.filters.invite.allowed.channels.includes(message.channel.id)
71 ) {
72 if (
73 /(?:https?:\/\/)?discord(?:app)?\.(?:com\/invite|gg)\/[a-zA-Z0-9]+\/?/.test(
74 content
75 )
76 ) {
77 createLogException(
78 message.guild.id,
79 message.channel.id,
80 message.id
81 );
pineafan813bdf42022-07-24 10:39:10 +010082 message.delete();
pineafan63fc5e22022-08-04 22:04:10 +010083 const data = {
pineafan813bdf42022-07-24 10:39:10 +010084 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010085 type: "messageDelete",
Skyler Grey75ea9172022-08-06 10:22:23 +010086 displayName:
87 "Message Deleted (Automated, Contained Invite)",
pineafan63fc5e22022-08-04 22:04:10 +010088 calculateType: "autoModeratorDeleted",
pineafan813bdf42022-07-24 10:39:10 +010089 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +010090 emoji: "MESSAGE.DELETE",
pineafan813bdf42022-07-24 10:39:10 +010091 timestamp: new Date().getTime()
92 },
93 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +010094 start:
95 filter +
96 " Contained invite\n\n" +
97 (content
98 ? `**Message:**\n\`\`\`${content}\`\`\``
99 : "**Message:** *Message had no content*")
pineafan813bdf42022-07-24 10:39:10 +0100100 },
101 list: list,
102 hidden: {
103 guild: message.channel.guild.id
104 }
pineafan63fc5e22022-08-04 22:04:10 +0100105 };
pineafan813bdf42022-07-24 10:39:10 +0100106 return log(data);
107 }
108 }
109 }
110
111 if (fileNames.files.length > 0) {
pineafan63fc5e22022-08-04 22:04:10 +0100112 for (const element of fileNames.files) {
pineafan63fc5e22022-08-04 22:04:10 +0100113 const url = element.url ? element.url : element.local;
Skyler Greyc634e2b2022-08-06 17:50:48 +0100114 if (
115 /\.(jpg|jpeg|png|gif|gifv|webm|webp|mp4|wav|mp3|ogg)$/.test(url)
116 ) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100117 if (
Skyler Greyc634e2b2022-08-06 17:50:48 +0100118 config.filters.images.NSFW &&
119 !(message.channel instanceof ThreadChannel
120 ? message.channel.parent?.nsfw
121 : message.channel.nsfw)
Skyler Grey75ea9172022-08-06 10:22:23 +0100122 ) {
Skyler Greyc634e2b2022-08-06 17:50:48 +0100123 if (await NSFWCheck(url)) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100124 createLogException(
125 message.guild.id,
126 message.channel.id,
127 message.id
128 );
pineafan63fc5e22022-08-04 22:04:10 +0100129 await message.delete();
130 const data = {
pineafan813bdf42022-07-24 10:39:10 +0100131 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100132 type: "messageDelete",
133 displayName: "Message Deleted",
134 calculateType: "autoModeratorDeleted",
pineafan813bdf42022-07-24 10:39:10 +0100135 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +0100136 emoji: "MESSAGE.DELETE",
pineafan813bdf42022-07-24 10:39:10 +0100137 timestamp: new Date().getTime()
138 },
139 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100140 start:
141 filter +
Skyler Greyc634e2b2022-08-06 17:50:48 +0100142 " Image detected as NSFW\n\n" +
Skyler Grey75ea9172022-08-06 10:22:23 +0100143 (content
144 ? `**Message:**\n\`\`\`${content}\`\`\``
145 : "**Message:** *Message had no content*")
pineafan813bdf42022-07-24 10:39:10 +0100146 },
147 list: list,
148 hidden: {
149 guild: message.channel.guild.id
150 }
pineafan63fc5e22022-08-04 22:04:10 +0100151 };
pineafan813bdf42022-07-24 10:39:10 +0100152 return log(data);
153 }
154 }
Skyler Greyc634e2b2022-08-06 17:50:48 +0100155 if (config.filters.wordFilter.enabled) {
156 const text = await TestImage(url);
157 const check = TestString(
158 text ?? "",
159 config.filters.wordFilter.words.loose,
160 config.filters.wordFilter.words.strict
161 );
162 if (check !== null) {
163 createLogException(
164 message.guild.id,
165 message.channel.id,
166 message.id
167 );
168 await message.delete();
169 const data = {
170 meta: {
171 type: "messageDelete",
172 displayName: "Message Deleted",
173 calculateType: "autoModeratorDeleted",
174 color: NucleusColors.red,
175 emoji: "MESSAGE.DELETE",
176 timestamp: new Date().getTime()
177 },
178 separate: {
179 start:
180 filter +
181 " Image contained filtered word\n\n" +
182 (content
183 ? `**Message:**\n\`\`\`${content}\`\`\``
184 : "**Message:** *Message had no content*")
185 },
186 list: list,
187 hidden: {
188 guild: message.channel.guild.id
189 }
190 };
191 return log(data);
192 }
193 }
194 if (config.filters.images.size) {
195 if (url.match(/\.+(webp|png|jpg)$/gi)) {
196 if (!(await SizeCheck(element))) {
197 createLogException(
198 message.guild.id,
199 message.channel.id,
200 message.id
201 );
202 await message.delete();
203 const data = {
204 meta: {
205 type: "messageDelete",
206 displayName: "Message Deleted",
207 calculateType: "autoModeratorDeleted",
208 color: NucleusColors.red,
209 emoji: "MESSAGE.DELETE",
210 timestamp: new Date().getTime()
211 },
212 separate: {
213 start:
214 filter +
215 " Image was too small\n\n" +
216 (content
217 ? `**Message:**\n\`\`\`${content}\`\`\``
218 : "**Message:** *Message had no content*")
219 },
220 list: list,
221 hidden: {
222 guild: message.channel.guild.id
223 }
224 };
225 return log(data);
226 }
227 }
228 }
229 }
230 if (config.filters.malware) {
231 if (!(await MalwareCheck(url))) {
232 createLogException(
233 message.guild.id,
234 message.channel.id,
235 message.id
236 );
237 await message.delete();
238 const data = {
239 meta: {
240 type: "messageDelete",
241 displayName: "Message Deleted",
242 calculateType: "autoModeratorDeleted",
243 color: NucleusColors.red,
244 emoji: "MESSAGE.DELETE",
245 timestamp: new Date().getTime()
246 },
247 separate: {
248 start:
249 filter +
250 " File detected as malware\n\n" +
251 (content
252 ? `**Message:**\n\`\`\`${content}\`\`\``
253 : "**Message:** *Message had no content*")
254 },
255 list: list,
256 hidden: {
257 guild: message.channel.guild.id
258 }
259 };
260 return log(data);
261 }
pineafan813bdf42022-07-24 10:39:10 +0100262 }
pineafan63fc5e22022-08-04 22:04:10 +0100263 }
pineafan813bdf42022-07-24 10:39:10 +0100264 }
pineafan813bdf42022-07-24 10:39:10 +0100265
pineafan63fc5e22022-08-04 22:04:10 +0100266 const linkDetectionTypes = await LinkCheck(message);
pineafan813bdf42022-07-24 10:39:10 +0100267 if (linkDetectionTypes.length > 0) {
pineafan63fc5e22022-08-04 22:04:10 +0100268 createLogException(message.guild.id, message.channel.id, message.id);
269 await message.delete();
270 const data = {
pineafan813bdf42022-07-24 10:39:10 +0100271 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100272 type: "messageDelete",
273 displayName: "Message Deleted",
274 calculateType: "autoModeratorDeleted",
pineafan813bdf42022-07-24 10:39:10 +0100275 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +0100276 emoji: "MESSAGE.DELETE",
pineafan813bdf42022-07-24 10:39:10 +0100277 timestamp: new Date().getTime()
278 },
279 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100280 start:
281 filter +
282 ` Link filtered as ${linkDetectionTypes[0]?.toLowerCase()}\n\n` +
283 (content
284 ? `**Message:**\n\`\`\`${content}\`\`\``
285 : "**Message:** *Message had no content*")
pineafan813bdf42022-07-24 10:39:10 +0100286 },
287 list: list,
288 hidden: {
289 guild: message.channel.guild.id
290 }
pineafan63fc5e22022-08-04 22:04:10 +0100291 };
pineafan813bdf42022-07-24 10:39:10 +0100292 return log(data);
293 }
pineafane23c4ec2022-07-27 21:56:27 +0100294
pineafan813bdf42022-07-24 10:39:10 +0100295 if (config.filters.wordFilter.enabled) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100296 const check = TestString(
297 content,
298 config.filters.wordFilter.words.loose,
299 config.filters.wordFilter.words.strict
300 );
301 if (check !== null) {
302 createLogException(
303 message.guild.id,
304 message.channel.id,
305 message.id
306 );
pineafan63fc5e22022-08-04 22:04:10 +0100307 await message.delete();
308 const data = {
pineafan813bdf42022-07-24 10:39:10 +0100309 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100310 type: "messageDelete",
311 displayName: "Message Deleted",
312 calculateType: "autoModeratorDeleted",
pineafan813bdf42022-07-24 10:39:10 +0100313 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +0100314 emoji: "MESSAGE.DELETE",
pineafan813bdf42022-07-24 10:39:10 +0100315 timestamp: new Date().getTime()
316 },
317 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100318 start:
319 filter +
320 " Message contained filtered word\n\n" +
321 (content
322 ? `**Message:**\n\`\`\`${content}\`\`\``
323 : "**Message:** *Message had no content*")
pineafan813bdf42022-07-24 10:39:10 +0100324 },
325 list: list,
326 hidden: {
327 guild: message.channel.guild.id
328 }
pineafan63fc5e22022-08-04 22:04:10 +0100329 };
pineafan813bdf42022-07-24 10:39:10 +0100330 return log(data);
331 }
332 }
333
pineafane23c4ec2022-07-27 21:56:27 +0100334 if (config.filters.pings.everyone && message.mentions.everyone) {
pineafan63fc5e22022-08-04 22:04:10 +0100335 const data = {
pineafane23c4ec2022-07-27 21:56:27 +0100336 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100337 type: "everyonePing",
338 displayName: "Everyone Pinged",
339 calculateType: "messageMassPing",
pineafane23c4ec2022-07-27 21:56:27 +0100340 color: NucleusColors.yellow,
pineafan63fc5e22022-08-04 22:04:10 +0100341 emoji: "MESSAGE.PING.EVERYONE",
pineafane23c4ec2022-07-27 21:56:27 +0100342 timestamp: new Date().getTime()
343 },
344 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100345 start: content
346 ? `**Message:**\n\`\`\`${content}\`\`\``
347 : "**Message:** *Message had no content*"
pineafane23c4ec2022-07-27 21:56:27 +0100348 },
349 list: list,
350 hidden: {
351 guild: message.channel.guild.id
pineafan813bdf42022-07-24 10:39:10 +0100352 }
pineafan63fc5e22022-08-04 22:04:10 +0100353 };
pineafane23c4ec2022-07-27 21:56:27 +0100354 return log(data);
355 }
356 if (config.filters.pings.roles) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100357 for (const roleId in message.mentions.roles) {
pineafan63fc5e22022-08-04 22:04:10 +0100358 if (!config.filters.pings.allowed.roles.includes(roleId)) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100359 createLogException(
360 message.guild.id,
361 message.channel.id,
362 message.id
363 );
pineafan63fc5e22022-08-04 22:04:10 +0100364 await message.delete();
365 const data = {
pineafane23c4ec2022-07-27 21:56:27 +0100366 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100367 type: "rolePing",
368 displayName: "Role Pinged",
369 calculateType: "messageMassPing",
pineafane23c4ec2022-07-27 21:56:27 +0100370 color: NucleusColors.yellow,
pineafan63fc5e22022-08-04 22:04:10 +0100371 emoji: "MESSAGE.PING.ROLE",
pineafane23c4ec2022-07-27 21:56:27 +0100372 timestamp: new Date().getTime()
373 },
374 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100375 start: content
376 ? `**Message:**\n\`\`\`${content}\`\`\``
377 : "**Message:** *Message had no content*"
pineafane23c4ec2022-07-27 21:56:27 +0100378 },
379 list: list,
380 hidden: {
381 guild: message.channel.guild.id
pineafan813bdf42022-07-24 10:39:10 +0100382 }
pineafan63fc5e22022-08-04 22:04:10 +0100383 };
pineafane23c4ec2022-07-27 21:56:27 +0100384 return log(data);
pineafan813bdf42022-07-24 10:39:10 +0100385 }
386 }
pineafane23c4ec2022-07-27 21:56:27 +0100387 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100388 if (
389 message.mentions.users.size >= config.filters.pings.mass &&
390 config.filters.pings.mass
391 ) {
pineafan63fc5e22022-08-04 22:04:10 +0100392 createLogException(message.guild.id, message.channel.id, message.id);
393 await message.delete();
394 const data = {
pineafane23c4ec2022-07-27 21:56:27 +0100395 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100396 type: "massPing",
397 displayName: "Mass Ping",
398 calculateType: "messageMassPing",
pineafane23c4ec2022-07-27 21:56:27 +0100399 color: NucleusColors.yellow,
pineafan63fc5e22022-08-04 22:04:10 +0100400 emoji: "MESSAGE.PING.MASS",
pineafane23c4ec2022-07-27 21:56:27 +0100401 timestamp: new Date().getTime()
402 },
403 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100404 start: content
405 ? `**Message:**\n\`\`\`${content}\`\`\``
406 : "**Message:** *Message had no content*"
pineafane23c4ec2022-07-27 21:56:27 +0100407 },
408 list: list,
409 hidden: {
410 guild: message.channel.guild.id
pineafan813bdf42022-07-24 10:39:10 +0100411 }
pineafan63fc5e22022-08-04 22:04:10 +0100412 };
pineafane23c4ec2022-07-27 21:56:27 +0100413 return log(data);
pineafan813bdf42022-07-24 10:39:10 +0100414 }
415}