blob: 14ec7e54977037325842021a2490907b895a6e3f [file] [log] [blame]
PineaFan752af462022-12-31 21:59:38 +00001import type { NucleusClient } from "../utils/client.js";
Skyler Grey11236ba2022-08-08 21:13:33 +01002import { LinkCheck, MalwareCheck, NSFWCheck, SizeCheck, TestString, TestImage } from "../reflex/scanners.js";
pineafan63fc5e22022-08-04 22:04:10 +01003import logAttachment from "../premium/attachmentLogs.js";
PineaFan0d06edc2023-01-17 22:10:31 +00004import { messageException } from "../utils/createTemporaryStorage.js";
pineafan63fc5e22022-08-04 22:04:10 +01005import getEmojiByName from "../utils/getEmojiByName.js";
6import client from "../utils/client.js";
pineafan0f5cc782022-08-12 21:55:42 +01007import { callback as statsChannelUpdate } from "../reflex/statsChannelUpdate.js";
Skyler Grey3331c9f2023-03-05 07:34:15 +00008import { ChannelType, Message, ThreadChannel } from "discord.js";
pineafan813bdf42022-07-24 10:39:10 +01009
pineafan63fc5e22022-08-04 22:04:10 +010010export const event = "messageCreate";
pineafan813bdf42022-07-24 10:39:10 +010011
PineaFan752af462022-12-31 21:59:38 +000012export async function callback(_client: NucleusClient, message: Message) {
Skyler Grey75ea9172022-08-06 10:22:23 +010013 if (!message.guild) return;
PineappleFan13892c62023-03-05 07:00:07 +000014 const config = await client.memory.readGuildInfo(message.guild.id);
Skyler9f5fca82023-03-05 09:12:03 +000015
16 if (config.autoPublish.enabled
17 && config.autoPublish.channels.includes(message.channel.id)
18 && message.channel.type === ChannelType.GuildAnnouncement
19 && message.reference === null
20 ) {
PineappleFan13892c62023-03-05 07:00:07 +000021 await message.crosspost();
22 }
23
pineafan63fc5e22022-08-04 22:04:10 +010024 if (message.author.bot) return;
PineaFan538d3752023-01-12 21:48:23 +000025 if (message.channel.isDMBased()) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010026 try {
pineafan0f5cc782022-08-12 21:55:42 +010027 await statsChannelUpdate(client, await message.guild.members.fetch(message.author.id));
Skyler Grey75ea9172022-08-06 10:22:23 +010028 } catch (e) {
29 console.log(e);
30 }
pineafan813bdf42022-07-24 10:39:10 +010031
TheCodedProf6ec331b2023-02-20 12:13:06 -050032 const { log, isLogging, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger;
pineafan813bdf42022-07-24 10:39:10 +010033
pineafan63fc5e22022-08-04 22:04:10 +010034 const fileNames = await logAttachment(message);
pineafan813bdf42022-07-24 10:39:10 +010035
pineafan63fc5e22022-08-04 22:04:10 +010036 const content = message.content.toLowerCase() || "";
TheCodedProfad0b8202023-02-14 14:27:09 -050037 if(config.filters.clean.channels.includes(message.channel.id)) {
TheCodedProf1807fb32023-02-20 14:33:48 -050038 const memberRoles = message.member!.roles.cache.map(role => role.id);
39 const roleAllow = config.filters.clean.allowed.roles.some(role => memberRoles.includes(role));
TheCodedProff8ef7942023-03-03 15:32:32 -050040 const userAllow = config.filters.clean.allowed.users.includes(message.author.id);
TheCodedProfad0b8202023-02-14 14:27:09 -050041 if(!roleAllow && !userAllow) return await message.delete();
42 }
TheCodedProfbaee2c12023-02-18 16:11:06 -050043
pineafan63fc5e22022-08-04 22:04:10 +010044 const filter = getEmojiByName("ICONS.FILTER");
45 let attachmentJump = "";
Skyler Grey75ea9172022-08-06 10:22:23 +010046 if (config.logging.attachments.saved[message.channel.id + message.id]) {
Skyler Grey11236ba2022-08-08 21:13:33 +010047 attachmentJump = ` [[View attachments]](${config.logging.attachments.saved[message.channel.id + message.id]})`;
Skyler Grey75ea9172022-08-06 10:22:23 +010048 }
pineafan63fc5e22022-08-04 22:04:10 +010049 const list = {
pineafan813bdf42022-07-24 10:39:10 +010050 messageId: entry(message.id, `\`${message.id}\``),
51 sentBy: entry(message.author.id, renderUser(message.author)),
52 sentIn: entry(message.channel.id, renderChannel(message.channel)),
TheCodedProf6ec331b2023-02-20 12:13:06 -050053 deleted: entry(Date.now(), renderDelta(Date.now())),
pineafan813bdf42022-07-24 10:39:10 +010054 mentions: message.mentions.users.size,
Skyler Grey11236ba2022-08-08 21:13:33 +010055 attachments: entry(message.attachments.size, message.attachments.size + attachmentJump),
pineafan813bdf42022-07-24 10:39:10 +010056 repliedTo: entry(
PineaFan538d3752023-01-12 21:48:23 +000057 (message.reference ? message.reference.messageId : null) ?? null,
Skyler Grey75ea9172022-08-06 10:22:23 +010058 message.reference
59 ? `[[Jump to message]](https://discord.com/channels/${message.guild.id}/${message.channel.id}/${message.reference.messageId})`
60 : "None"
pineafan813bdf42022-07-24 10:39:10 +010061 )
pineafan63fc5e22022-08-04 22:04:10 +010062 };
pineafan813bdf42022-07-24 10:39:10 +010063
64 if (config.filters.invite.enabled) {
Skyler Grey11236ba2022-08-08 21:13:33 +010065 if (!config.filters.invite.allowed.channels.includes(message.channel.id)) {
66 if (/(?:https?:\/\/)?discord(?:app)?\.(?:com\/invite|gg)\/[a-zA-Z0-9]+\/?/.test(content)) {
PineaFan0d06edc2023-01-17 22:10:31 +000067 messageException(message.guild.id, message.channel.id, message.id);
pineafan813bdf42022-07-24 10:39:10 +010068 message.delete();
pineafan63fc5e22022-08-04 22:04:10 +010069 const data = {
pineafan813bdf42022-07-24 10:39:10 +010070 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010071 type: "messageDelete",
Skyler Grey11236ba2022-08-08 21:13:33 +010072 displayName: "Message Deleted (Automated, Contained Invite)",
pineafan63fc5e22022-08-04 22:04:10 +010073 calculateType: "autoModeratorDeleted",
pineafan813bdf42022-07-24 10:39:10 +010074 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +010075 emoji: "MESSAGE.DELETE",
TheCodedProf6ec331b2023-02-20 12:13:06 -050076 timestamp: Date.now()
pineafan813bdf42022-07-24 10:39:10 +010077 },
78 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +010079 start:
80 filter +
81 " Contained invite\n\n" +
Skyler Grey11236ba2022-08-08 21:13:33 +010082 (content ? `**Message:**\n\`\`\`${content}\`\`\`` : "**Message:** *Message had no content*")
pineafan813bdf42022-07-24 10:39:10 +010083 },
84 list: list,
85 hidden: {
86 guild: message.channel.guild.id
87 }
pineafan63fc5e22022-08-04 22:04:10 +010088 };
pineafan813bdf42022-07-24 10:39:10 +010089 return log(data);
90 }
91 }
92 }
93
94 if (fileNames.files.length > 0) {
pineafan63fc5e22022-08-04 22:04:10 +010095 for (const element of fileNames.files) {
pineafan63fc5e22022-08-04 22:04:10 +010096 const url = element.url ? element.url : element.local;
TheCodedProf16113672023-02-03 16:05:23 -050097 if (/\.(j(pe?g|fif)|a?png|gifv?|w(eb[mp]|av)|mp([34]|eg-\d)|ogg|avi|h\.26(4|5)|cda)$/.test(url.toLowerCase())) {
98 // jpg|jpeg|png|apng|gif|gifv|webm|webp|mp4|wav|mp3|ogg|jfif|MPEG-#|avi|h.264|h.265
Skyler Grey75ea9172022-08-06 10:22:23 +010099 if (
Skyler Greyc634e2b2022-08-06 17:50:48 +0100100 config.filters.images.NSFW &&
Skyler Grey11236ba2022-08-08 21:13:33 +0100101 !(message.channel instanceof ThreadChannel ? message.channel.parent?.nsfw : message.channel.nsfw)
Skyler Grey75ea9172022-08-06 10:22:23 +0100102 ) {
Skyler Greyc634e2b2022-08-06 17:50:48 +0100103 if (await NSFWCheck(url)) {
PineaFan0d06edc2023-01-17 22:10:31 +0000104 messageException(message.guild.id, message.channel.id, message.id);
pineafan63fc5e22022-08-04 22:04:10 +0100105 await message.delete();
106 const data = {
pineafan813bdf42022-07-24 10:39:10 +0100107 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100108 type: "messageDelete",
109 displayName: "Message Deleted",
110 calculateType: "autoModeratorDeleted",
pineafan813bdf42022-07-24 10:39:10 +0100111 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +0100112 emoji: "MESSAGE.DELETE",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500113 timestamp: Date.now()
pineafan813bdf42022-07-24 10:39:10 +0100114 },
115 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100116 start:
117 filter +
Skyler Greyc634e2b2022-08-06 17:50:48 +0100118 " Image detected as NSFW\n\n" +
Skyler Grey75ea9172022-08-06 10:22:23 +0100119 (content
120 ? `**Message:**\n\`\`\`${content}\`\`\``
121 : "**Message:** *Message had no content*")
pineafan813bdf42022-07-24 10:39:10 +0100122 },
123 list: list,
124 hidden: {
125 guild: message.channel.guild.id
126 }
pineafan63fc5e22022-08-04 22:04:10 +0100127 };
pineafan813bdf42022-07-24 10:39:10 +0100128 return log(data);
129 }
130 }
Skyler Greyc634e2b2022-08-06 17:50:48 +0100131 if (config.filters.wordFilter.enabled) {
132 const text = await TestImage(url);
133 const check = TestString(
134 text ?? "",
135 config.filters.wordFilter.words.loose,
136 config.filters.wordFilter.words.strict
137 );
138 if (check !== null) {
PineaFan0d06edc2023-01-17 22:10:31 +0000139 messageException(message.guild.id, message.channel.id, message.id);
Skyler Greyc634e2b2022-08-06 17:50:48 +0100140 await message.delete();
141 const data = {
142 meta: {
143 type: "messageDelete",
144 displayName: "Message Deleted",
145 calculateType: "autoModeratorDeleted",
146 color: NucleusColors.red,
147 emoji: "MESSAGE.DELETE",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500148 timestamp: Date.now()
Skyler Greyc634e2b2022-08-06 17:50:48 +0100149 },
150 separate: {
151 start:
152 filter +
153 " Image contained filtered word\n\n" +
154 (content
155 ? `**Message:**\n\`\`\`${content}\`\`\``
156 : "**Message:** *Message had no content*")
157 },
158 list: list,
159 hidden: {
160 guild: message.channel.guild.id
161 }
162 };
163 return log(data);
164 }
165 }
166 if (config.filters.images.size) {
167 if (url.match(/\.+(webp|png|jpg)$/gi)) {
168 if (!(await SizeCheck(element))) {
PineaFan0d06edc2023-01-17 22:10:31 +0000169 messageException(message.guild.id, message.channel.id, message.id);
Skyler Greyc634e2b2022-08-06 17:50:48 +0100170 await message.delete();
171 const data = {
172 meta: {
173 type: "messageDelete",
174 displayName: "Message Deleted",
175 calculateType: "autoModeratorDeleted",
176 color: NucleusColors.red,
177 emoji: "MESSAGE.DELETE",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500178 timestamp: Date.now()
Skyler Greyc634e2b2022-08-06 17:50:48 +0100179 },
180 separate: {
181 start:
182 filter +
183 " Image was too small\n\n" +
184 (content
185 ? `**Message:**\n\`\`\`${content}\`\`\``
186 : "**Message:** *Message had no content*")
187 },
188 list: list,
189 hidden: {
190 guild: message.channel.guild.id
191 }
192 };
193 return log(data);
194 }
195 }
196 }
197 }
198 if (config.filters.malware) {
199 if (!(await MalwareCheck(url))) {
PineaFan0d06edc2023-01-17 22:10:31 +0000200 messageException(message.guild.id, message.channel.id, message.id);
Skyler Greyc634e2b2022-08-06 17:50:48 +0100201 await message.delete();
202 const data = {
203 meta: {
204 type: "messageDelete",
205 displayName: "Message Deleted",
206 calculateType: "autoModeratorDeleted",
207 color: NucleusColors.red,
208 emoji: "MESSAGE.DELETE",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500209 timestamp: Date.now()
Skyler Greyc634e2b2022-08-06 17:50:48 +0100210 },
211 separate: {
212 start:
213 filter +
214 " File detected as malware\n\n" +
215 (content
216 ? `**Message:**\n\`\`\`${content}\`\`\``
217 : "**Message:** *Message had no content*")
218 },
219 list: list,
220 hidden: {
221 guild: message.channel.guild.id
222 }
223 };
224 return log(data);
225 }
pineafan813bdf42022-07-24 10:39:10 +0100226 }
pineafan63fc5e22022-08-04 22:04:10 +0100227 }
pineafan813bdf42022-07-24 10:39:10 +0100228 }
pineafan813bdf42022-07-24 10:39:10 +0100229
pineafan63fc5e22022-08-04 22:04:10 +0100230 const linkDetectionTypes = await LinkCheck(message);
pineafan813bdf42022-07-24 10:39:10 +0100231 if (linkDetectionTypes.length > 0) {
PineaFan0d06edc2023-01-17 22:10:31 +0000232 messageException(message.guild.id, message.channel.id, message.id);
pineafan63fc5e22022-08-04 22:04:10 +0100233 await message.delete();
234 const data = {
pineafan813bdf42022-07-24 10:39:10 +0100235 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100236 type: "messageDelete",
237 displayName: "Message Deleted",
238 calculateType: "autoModeratorDeleted",
pineafan813bdf42022-07-24 10:39:10 +0100239 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +0100240 emoji: "MESSAGE.DELETE",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500241 timestamp: Date.now()
pineafan813bdf42022-07-24 10:39:10 +0100242 },
243 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100244 start:
245 filter +
246 ` Link filtered as ${linkDetectionTypes[0]?.toLowerCase()}\n\n` +
Skyler Grey11236ba2022-08-08 21:13:33 +0100247 (content ? `**Message:**\n\`\`\`${content}\`\`\`` : "**Message:** *Message had no content*")
pineafan813bdf42022-07-24 10:39:10 +0100248 },
249 list: list,
250 hidden: {
251 guild: message.channel.guild.id
252 }
pineafan63fc5e22022-08-04 22:04:10 +0100253 };
pineafan813bdf42022-07-24 10:39:10 +0100254 return log(data);
255 }
pineafane23c4ec2022-07-27 21:56:27 +0100256
pineafan813bdf42022-07-24 10:39:10 +0100257 if (config.filters.wordFilter.enabled) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100258 const check = TestString(
259 content,
260 config.filters.wordFilter.words.loose,
261 config.filters.wordFilter.words.strict
262 );
263 if (check !== null) {
PineaFan0d06edc2023-01-17 22:10:31 +0000264 messageException(message.guild.id, message.channel.id, message.id);
pineafan63fc5e22022-08-04 22:04:10 +0100265 await message.delete();
266 const data = {
pineafan813bdf42022-07-24 10:39:10 +0100267 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100268 type: "messageDelete",
269 displayName: "Message Deleted",
270 calculateType: "autoModeratorDeleted",
pineafan813bdf42022-07-24 10:39:10 +0100271 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +0100272 emoji: "MESSAGE.DELETE",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500273 timestamp: Date.now()
pineafan813bdf42022-07-24 10:39:10 +0100274 },
275 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100276 start:
277 filter +
278 " Message contained filtered word\n\n" +
Skyler Grey11236ba2022-08-08 21:13:33 +0100279 (content ? `**Message:**\n\`\`\`${content}\`\`\`` : "**Message:** *Message had no content*")
pineafan813bdf42022-07-24 10:39:10 +0100280 },
281 list: list,
282 hidden: {
283 guild: message.channel.guild.id
284 }
pineafan63fc5e22022-08-04 22:04:10 +0100285 };
pineafan813bdf42022-07-24 10:39:10 +0100286 return log(data);
287 }
288 }
289
pineafane23c4ec2022-07-27 21:56:27 +0100290 if (config.filters.pings.everyone && message.mentions.everyone) {
TheCodedProf6ec331b2023-02-20 12:13:06 -0500291 if(!await isLogging(message.guild.id, "messageMassPing")) return;
pineafan63fc5e22022-08-04 22:04:10 +0100292 const data = {
pineafane23c4ec2022-07-27 21:56:27 +0100293 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100294 type: "everyonePing",
295 displayName: "Everyone Pinged",
296 calculateType: "messageMassPing",
pineafane23c4ec2022-07-27 21:56:27 +0100297 color: NucleusColors.yellow,
pineafan63fc5e22022-08-04 22:04:10 +0100298 emoji: "MESSAGE.PING.EVERYONE",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500299 timestamp: Date.now()
pineafane23c4ec2022-07-27 21:56:27 +0100300 },
301 separate: {
Skyler Grey11236ba2022-08-08 21:13:33 +0100302 start: content ? `**Message:**\n\`\`\`${content}\`\`\`` : "**Message:** *Message had no content*"
pineafane23c4ec2022-07-27 21:56:27 +0100303 },
304 list: list,
305 hidden: {
306 guild: message.channel.guild.id
pineafan813bdf42022-07-24 10:39:10 +0100307 }
pineafan63fc5e22022-08-04 22:04:10 +0100308 };
pineafane23c4ec2022-07-27 21:56:27 +0100309 return log(data);
310 }
311 if (config.filters.pings.roles) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100312 for (const roleId in message.mentions.roles) {
pineafan63fc5e22022-08-04 22:04:10 +0100313 if (!config.filters.pings.allowed.roles.includes(roleId)) {
PineaFan0d06edc2023-01-17 22:10:31 +0000314 messageException(message.guild.id, message.channel.id, message.id);
pineafan63fc5e22022-08-04 22:04:10 +0100315 await message.delete();
TheCodedProf6ec331b2023-02-20 12:13:06 -0500316 if(!await isLogging(message.guild.id, "messageMassPing")) return;
pineafan63fc5e22022-08-04 22:04:10 +0100317 const data = {
pineafane23c4ec2022-07-27 21:56:27 +0100318 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100319 type: "rolePing",
320 displayName: "Role Pinged",
321 calculateType: "messageMassPing",
pineafane23c4ec2022-07-27 21:56:27 +0100322 color: NucleusColors.yellow,
pineafan63fc5e22022-08-04 22:04:10 +0100323 emoji: "MESSAGE.PING.ROLE",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500324 timestamp: Date.now()
pineafane23c4ec2022-07-27 21:56:27 +0100325 },
326 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100327 start: content
328 ? `**Message:**\n\`\`\`${content}\`\`\``
329 : "**Message:** *Message had no content*"
pineafane23c4ec2022-07-27 21:56:27 +0100330 },
331 list: list,
332 hidden: {
333 guild: message.channel.guild.id
pineafan813bdf42022-07-24 10:39:10 +0100334 }
pineafan63fc5e22022-08-04 22:04:10 +0100335 };
pineafane23c4ec2022-07-27 21:56:27 +0100336 return log(data);
pineafan813bdf42022-07-24 10:39:10 +0100337 }
338 }
pineafane23c4ec2022-07-27 21:56:27 +0100339 }
Skyler Grey11236ba2022-08-08 21:13:33 +0100340 if (message.mentions.users.size >= config.filters.pings.mass && config.filters.pings.mass) {
PineaFan0d06edc2023-01-17 22:10:31 +0000341 messageException(message.guild.id, message.channel.id, message.id);
pineafan63fc5e22022-08-04 22:04:10 +0100342 await message.delete();
TheCodedProf6ec331b2023-02-20 12:13:06 -0500343 if(!await isLogging(message.guild.id, "messageMassPing")) return;
pineafan63fc5e22022-08-04 22:04:10 +0100344 const data = {
pineafane23c4ec2022-07-27 21:56:27 +0100345 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100346 type: "massPing",
347 displayName: "Mass Ping",
348 calculateType: "messageMassPing",
pineafane23c4ec2022-07-27 21:56:27 +0100349 color: NucleusColors.yellow,
pineafan63fc5e22022-08-04 22:04:10 +0100350 emoji: "MESSAGE.PING.MASS",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500351 timestamp: Date.now()
pineafane23c4ec2022-07-27 21:56:27 +0100352 },
353 separate: {
Skyler Grey11236ba2022-08-08 21:13:33 +0100354 start: content ? `**Message:**\n\`\`\`${content}\`\`\`` : "**Message:** *Message had no content*"
pineafane23c4ec2022-07-27 21:56:27 +0100355 },
356 list: list,
357 hidden: {
358 guild: message.channel.guild.id
pineafan813bdf42022-07-24 10:39:10 +0100359 }
pineafan63fc5e22022-08-04 22:04:10 +0100360 };
pineafane23c4ec2022-07-27 21:56:27 +0100361 return log(data);
pineafan813bdf42022-07-24 10:39:10 +0100362 }
363}