blob: 38c767495b330fb8a43848538ce8b470c11ab5f1 [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";
TheCodedProfb7a7b992023-03-05 16:11:59 -05009import singleNotify from "../utils/singleNotify.js";
pineafan813bdf42022-07-24 10:39:10 +010010
pineafan63fc5e22022-08-04 22:04:10 +010011export const event = "messageCreate";
pineafan813bdf42022-07-24 10:39:10 +010012
PineaFan752af462022-12-31 21:59:38 +000013export async function callback(_client: NucleusClient, message: Message) {
Skyler Grey75ea9172022-08-06 10:22:23 +010014 if (!message.guild) return;
PineappleFan13892c62023-03-05 07:00:07 +000015 const config = await client.memory.readGuildInfo(message.guild.id);
Skyler9f5fca82023-03-05 09:12:03 +000016
Skyler Greyda16adf2023-03-05 10:22:12 +000017 if (
18 config.autoPublish.enabled &&
19 config.autoPublish.channels.includes(message.channel.id) &&
20 message.channel.type === ChannelType.GuildAnnouncement &&
21 message.reference === null
Skyler9f5fca82023-03-05 09:12:03 +000022 ) {
Skyler Grey67691762023-03-06 09:58:19 +000023 if (message.channel.permissionsFor(message.guild.members.me!)!.has("ManageMessages")) {
TheCodedProfb7a7b992023-03-05 16:11:59 -050024 await message.crosspost();
25 } else {
Skyler Greyf4f21c42023-03-08 14:36:29 +000026 await singleNotify(
27 `Nucleus does not have Manage Messages in <#${message.channel.id}>`,
28 message.guild.id,
29 true
30 );
TheCodedProfb7a7b992023-03-05 16:11:59 -050031 }
PineappleFan13892c62023-03-05 07:00:07 +000032 }
33
pineafan63fc5e22022-08-04 22:04:10 +010034 if (message.author.bot) return;
PineaFan538d3752023-01-12 21:48:23 +000035 if (message.channel.isDMBased()) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010036 try {
pineafan6de4da52023-03-07 20:43:44 +000037 await statsChannelUpdate((await message.guild.members.fetch(message.author.id)).user, message.guild);
Skyler Grey75ea9172022-08-06 10:22:23 +010038 } catch (e) {
39 console.log(e);
40 }
pineafan813bdf42022-07-24 10:39:10 +010041
TheCodedProf6ec331b2023-02-20 12:13:06 -050042 const { log, isLogging, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger;
pineafan813bdf42022-07-24 10:39:10 +010043
pineafan63fc5e22022-08-04 22:04:10 +010044 const fileNames = await logAttachment(message);
pineafan813bdf42022-07-24 10:39:10 +010045
pineafan63fc5e22022-08-04 22:04:10 +010046 const content = message.content.toLowerCase() || "";
Skyler Greyda16adf2023-03-05 10:22:12 +000047 if (config.filters.clean.channels.includes(message.channel.id)) {
48 const memberRoles = message.member!.roles.cache.map((role) => role.id);
49 const roleAllow = config.filters.clean.allowed.roles.some((role) => memberRoles.includes(role));
TheCodedProff8ef7942023-03-03 15:32:32 -050050 const userAllow = config.filters.clean.allowed.users.includes(message.author.id);
Skyler Greyda16adf2023-03-05 10:22:12 +000051 if (!roleAllow && !userAllow) return await message.delete();
TheCodedProfad0b8202023-02-14 14:27:09 -050052 }
TheCodedProfbaee2c12023-02-18 16:11:06 -050053
pineafan63fc5e22022-08-04 22:04:10 +010054 const filter = getEmojiByName("ICONS.FILTER");
55 let attachmentJump = "";
Skyler Grey75ea9172022-08-06 10:22:23 +010056 if (config.logging.attachments.saved[message.channel.id + message.id]) {
Skyler Grey11236ba2022-08-08 21:13:33 +010057 attachmentJump = ` [[View attachments]](${config.logging.attachments.saved[message.channel.id + message.id]})`;
Skyler Grey75ea9172022-08-06 10:22:23 +010058 }
pineafan63fc5e22022-08-04 22:04:10 +010059 const list = {
pineafan813bdf42022-07-24 10:39:10 +010060 messageId: entry(message.id, `\`${message.id}\``),
61 sentBy: entry(message.author.id, renderUser(message.author)),
62 sentIn: entry(message.channel.id, renderChannel(message.channel)),
TheCodedProf6ec331b2023-02-20 12:13:06 -050063 deleted: entry(Date.now(), renderDelta(Date.now())),
pineafan813bdf42022-07-24 10:39:10 +010064 mentions: message.mentions.users.size,
Skyler Grey11236ba2022-08-08 21:13:33 +010065 attachments: entry(message.attachments.size, message.attachments.size + attachmentJump),
pineafan813bdf42022-07-24 10:39:10 +010066 repliedTo: entry(
PineaFan538d3752023-01-12 21:48:23 +000067 (message.reference ? message.reference.messageId : null) ?? null,
Skyler Grey75ea9172022-08-06 10:22:23 +010068 message.reference
69 ? `[[Jump to message]](https://discord.com/channels/${message.guild.id}/${message.channel.id}/${message.reference.messageId})`
70 : "None"
pineafan813bdf42022-07-24 10:39:10 +010071 )
pineafan63fc5e22022-08-04 22:04:10 +010072 };
pineafan813bdf42022-07-24 10:39:10 +010073
74 if (config.filters.invite.enabled) {
Skyler Grey11236ba2022-08-08 21:13:33 +010075 if (!config.filters.invite.allowed.channels.includes(message.channel.id)) {
76 if (/(?:https?:\/\/)?discord(?:app)?\.(?:com\/invite|gg)\/[a-zA-Z0-9]+\/?/.test(content)) {
PineaFan0d06edc2023-01-17 22:10:31 +000077 messageException(message.guild.id, message.channel.id, message.id);
Skyler Greyf4f21c42023-03-08 14:36:29 +000078 await message.delete();
pineafan63fc5e22022-08-04 22:04:10 +010079 const data = {
pineafan813bdf42022-07-24 10:39:10 +010080 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010081 type: "messageDelete",
pineafanfa445992023-03-06 16:21:51 +000082 displayName: "Message Deleted",
pineafan63fc5e22022-08-04 22:04:10 +010083 calculateType: "autoModeratorDeleted",
pineafan813bdf42022-07-24 10:39:10 +010084 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +010085 emoji: "MESSAGE.DELETE",
TheCodedProf6ec331b2023-02-20 12:13:06 -050086 timestamp: Date.now()
pineafan813bdf42022-07-24 10:39:10 +010087 },
88 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +010089 start:
90 filter +
91 " Contained invite\n\n" +
Skyler Grey11236ba2022-08-08 21:13:33 +010092 (content ? `**Message:**\n\`\`\`${content}\`\`\`` : "**Message:** *Message had no content*")
pineafan813bdf42022-07-24 10:39:10 +010093 },
94 list: list,
95 hidden: {
96 guild: message.channel.guild.id
97 }
pineafan63fc5e22022-08-04 22:04:10 +010098 };
pineafan813bdf42022-07-24 10:39:10 +010099 return log(data);
100 }
101 }
102 }
103
104 if (fileNames.files.length > 0) {
pineafan63fc5e22022-08-04 22:04:10 +0100105 for (const element of fileNames.files) {
pineafan63fc5e22022-08-04 22:04:10 +0100106 const url = element.url ? element.url : element.local;
Skyler Greyda16adf2023-03-05 10:22:12 +0000107 if (
Skyler Grey0d885222023-03-08 21:46:37 +0000108 /\.(jpg|jpeg|png|apng|gif|gifv|webm|webp|mp4|wav|mp3|ogg|jfif|mpeg-\d|avi|h\.264|h\.265)$/.test(
Skyler Greyda16adf2023-03-05 10:22:12 +0000109 url.toLowerCase()
110 )
111 ) {
Skyler Grey0d885222023-03-08 21:46:37 +0000112 // j(pe?g|fif)|a?png|gifv?|w(eb[mp]|av)|mp([34]|eg-\d)|ogg|avi|h\.26(4|5)
113 // ^no
Skyler Grey75ea9172022-08-06 10:22:23 +0100114 if (
Skyler Greyc634e2b2022-08-06 17:50:48 +0100115 config.filters.images.NSFW &&
Skyler Grey0d885222023-03-08 21:46:37 +0000116 !(message.channel instanceof ThreadChannel ? message.channel.parent?.nsfw : message.channel.nsfw) &&
117 (await NSFWCheck(element))
Skyler Grey75ea9172022-08-06 10:22:23 +0100118 ) {
Skyler Grey0d885222023-03-08 21:46:37 +0000119 messageException(message.guild.id, message.channel.id, message.id);
120 await message.delete();
121 const data = {
122 meta: {
123 type: "messageDelete",
124 displayName: "Message Deleted",
125 calculateType: "autoModeratorDeleted",
126 color: NucleusColors.red,
127 emoji: "MESSAGE.DELETE",
128 timestamp: Date.now()
129 },
130 separate: {
131 start:
132 filter +
133 " Image detected as NSFW\n\n" +
134 (content
135 ? `**Message:**\n\`\`\`${content}\`\`\``
136 : "**Message:** *Message had no content*")
137 },
138 list: list,
139 hidden: {
140 guild: message.channel.guild.id
141 }
142 };
143 return log(data);
pineafan813bdf42022-07-24 10:39:10 +0100144 }
Skyler Greyc634e2b2022-08-06 17:50:48 +0100145 if (config.filters.wordFilter.enabled) {
146 const text = await TestImage(url);
147 const check = TestString(
148 text ?? "",
149 config.filters.wordFilter.words.loose,
150 config.filters.wordFilter.words.strict
151 );
152 if (check !== null) {
PineaFan0d06edc2023-01-17 22:10:31 +0000153 messageException(message.guild.id, message.channel.id, message.id);
Skyler Greyc634e2b2022-08-06 17:50:48 +0100154 await message.delete();
155 const data = {
156 meta: {
157 type: "messageDelete",
158 displayName: "Message Deleted",
159 calculateType: "autoModeratorDeleted",
160 color: NucleusColors.red,
161 emoji: "MESSAGE.DELETE",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500162 timestamp: Date.now()
Skyler Greyc634e2b2022-08-06 17:50:48 +0100163 },
164 separate: {
165 start:
166 filter +
167 " Image contained filtered word\n\n" +
168 (content
169 ? `**Message:**\n\`\`\`${content}\`\`\``
170 : "**Message:** *Message had no content*")
171 },
172 list: list,
173 hidden: {
174 guild: message.channel.guild.id
175 }
176 };
177 return log(data);
178 }
179 }
180 if (config.filters.images.size) {
181 if (url.match(/\.+(webp|png|jpg)$/gi)) {
182 if (!(await SizeCheck(element))) {
PineaFan0d06edc2023-01-17 22:10:31 +0000183 messageException(message.guild.id, message.channel.id, message.id);
Skyler Greyc634e2b2022-08-06 17:50:48 +0100184 await message.delete();
185 const data = {
186 meta: {
187 type: "messageDelete",
188 displayName: "Message Deleted",
189 calculateType: "autoModeratorDeleted",
190 color: NucleusColors.red,
191 emoji: "MESSAGE.DELETE",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500192 timestamp: Date.now()
Skyler Greyc634e2b2022-08-06 17:50:48 +0100193 },
194 separate: {
195 start:
196 filter +
197 " Image was too small\n\n" +
198 (content
199 ? `**Message:**\n\`\`\`${content}\`\`\``
200 : "**Message:** *Message had no content*")
201 },
202 list: list,
203 hidden: {
204 guild: message.channel.guild.id
205 }
206 };
207 return log(data);
208 }
209 }
210 }
211 }
Skyler Grey0d885222023-03-08 21:46:37 +0000212 if (config.filters.malware && (await MalwareCheck(url))) {
213 messageException(message.guild.id, message.channel.id, message.id);
214 await message.delete();
215 const data = {
216 meta: {
217 type: "messageDelete",
218 displayName: "Message Deleted",
219 calculateType: "autoModeratorDeleted",
220 color: NucleusColors.red,
221 emoji: "MESSAGE.DELETE",
222 timestamp: Date.now()
223 },
224 separate: {
225 start:
226 filter +
227 " File detected as malware\n\n" +
228 (content ? `**Message:**\n\`\`\`${content}\`\`\`` : "**Message:** *Message had no content*")
229 },
230 list: list,
231 hidden: {
232 guild: message.channel.guild.id
233 }
234 };
235 return log(data);
pineafan813bdf42022-07-24 10:39:10 +0100236 }
pineafan63fc5e22022-08-04 22:04:10 +0100237 }
pineafan813bdf42022-07-24 10:39:10 +0100238 }
pineafan813bdf42022-07-24 10:39:10 +0100239
pineafan63fc5e22022-08-04 22:04:10 +0100240 const linkDetectionTypes = await LinkCheck(message);
pineafan813bdf42022-07-24 10:39:10 +0100241 if (linkDetectionTypes.length > 0) {
PineaFan0d06edc2023-01-17 22:10:31 +0000242 messageException(message.guild.id, message.channel.id, message.id);
pineafan63fc5e22022-08-04 22:04:10 +0100243 await message.delete();
244 const data = {
pineafan813bdf42022-07-24 10:39:10 +0100245 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100246 type: "messageDelete",
247 displayName: "Message Deleted",
248 calculateType: "autoModeratorDeleted",
pineafan813bdf42022-07-24 10:39:10 +0100249 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +0100250 emoji: "MESSAGE.DELETE",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500251 timestamp: Date.now()
pineafan813bdf42022-07-24 10:39:10 +0100252 },
253 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100254 start:
255 filter +
256 ` Link filtered as ${linkDetectionTypes[0]?.toLowerCase()}\n\n` +
Skyler Grey11236ba2022-08-08 21:13:33 +0100257 (content ? `**Message:**\n\`\`\`${content}\`\`\`` : "**Message:** *Message had no content*")
pineafan813bdf42022-07-24 10:39:10 +0100258 },
259 list: list,
260 hidden: {
261 guild: message.channel.guild.id
262 }
pineafan63fc5e22022-08-04 22:04:10 +0100263 };
pineafan813bdf42022-07-24 10:39:10 +0100264 return log(data);
265 }
pineafane23c4ec2022-07-27 21:56:27 +0100266
pineafan813bdf42022-07-24 10:39:10 +0100267 if (config.filters.wordFilter.enabled) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100268 const check = TestString(
269 content,
270 config.filters.wordFilter.words.loose,
271 config.filters.wordFilter.words.strict
272 );
273 if (check !== null) {
PineaFan0d06edc2023-01-17 22:10:31 +0000274 messageException(message.guild.id, message.channel.id, message.id);
pineafan63fc5e22022-08-04 22:04:10 +0100275 await message.delete();
276 const data = {
pineafan813bdf42022-07-24 10:39:10 +0100277 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100278 type: "messageDelete",
279 displayName: "Message Deleted",
280 calculateType: "autoModeratorDeleted",
pineafan813bdf42022-07-24 10:39:10 +0100281 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +0100282 emoji: "MESSAGE.DELETE",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500283 timestamp: Date.now()
pineafan813bdf42022-07-24 10:39:10 +0100284 },
285 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100286 start:
287 filter +
288 " Message contained filtered word\n\n" +
Skyler Grey11236ba2022-08-08 21:13:33 +0100289 (content ? `**Message:**\n\`\`\`${content}\`\`\`` : "**Message:** *Message had no content*")
pineafan813bdf42022-07-24 10:39:10 +0100290 },
291 list: list,
292 hidden: {
293 guild: message.channel.guild.id
294 }
pineafan63fc5e22022-08-04 22:04:10 +0100295 };
pineafan813bdf42022-07-24 10:39:10 +0100296 return log(data);
297 }
298 }
299
pineafane23c4ec2022-07-27 21:56:27 +0100300 if (config.filters.pings.everyone && message.mentions.everyone) {
Skyler Greyda16adf2023-03-05 10:22:12 +0000301 if (!(await isLogging(message.guild.id, "messageMassPing"))) return;
pineafan63fc5e22022-08-04 22:04:10 +0100302 const data = {
pineafane23c4ec2022-07-27 21:56:27 +0100303 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100304 type: "everyonePing",
305 displayName: "Everyone Pinged",
306 calculateType: "messageMassPing",
pineafane23c4ec2022-07-27 21:56:27 +0100307 color: NucleusColors.yellow,
pineafan63fc5e22022-08-04 22:04:10 +0100308 emoji: "MESSAGE.PING.EVERYONE",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500309 timestamp: Date.now()
pineafane23c4ec2022-07-27 21:56:27 +0100310 },
311 separate: {
Skyler Grey11236ba2022-08-08 21:13:33 +0100312 start: content ? `**Message:**\n\`\`\`${content}\`\`\`` : "**Message:** *Message had no content*"
pineafane23c4ec2022-07-27 21:56:27 +0100313 },
314 list: list,
315 hidden: {
316 guild: message.channel.guild.id
pineafan813bdf42022-07-24 10:39:10 +0100317 }
pineafan63fc5e22022-08-04 22:04:10 +0100318 };
pineafane23c4ec2022-07-27 21:56:27 +0100319 return log(data);
320 }
321 if (config.filters.pings.roles) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100322 for (const roleId in message.mentions.roles) {
pineafan63fc5e22022-08-04 22:04:10 +0100323 if (!config.filters.pings.allowed.roles.includes(roleId)) {
PineaFan0d06edc2023-01-17 22:10:31 +0000324 messageException(message.guild.id, message.channel.id, message.id);
pineafan63fc5e22022-08-04 22:04:10 +0100325 await message.delete();
Skyler Greyda16adf2023-03-05 10:22:12 +0000326 if (!(await isLogging(message.guild.id, "messageMassPing"))) return;
pineafan63fc5e22022-08-04 22:04:10 +0100327 const data = {
pineafane23c4ec2022-07-27 21:56:27 +0100328 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100329 type: "rolePing",
330 displayName: "Role Pinged",
331 calculateType: "messageMassPing",
pineafane23c4ec2022-07-27 21:56:27 +0100332 color: NucleusColors.yellow,
pineafan63fc5e22022-08-04 22:04:10 +0100333 emoji: "MESSAGE.PING.ROLE",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500334 timestamp: Date.now()
pineafane23c4ec2022-07-27 21:56:27 +0100335 },
336 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100337 start: content
338 ? `**Message:**\n\`\`\`${content}\`\`\``
339 : "**Message:** *Message had no content*"
pineafane23c4ec2022-07-27 21:56:27 +0100340 },
341 list: list,
342 hidden: {
343 guild: message.channel.guild.id
pineafan813bdf42022-07-24 10:39:10 +0100344 }
pineafan63fc5e22022-08-04 22:04:10 +0100345 };
pineafane23c4ec2022-07-27 21:56:27 +0100346 return log(data);
pineafan813bdf42022-07-24 10:39:10 +0100347 }
348 }
pineafane23c4ec2022-07-27 21:56:27 +0100349 }
Skyler Grey11236ba2022-08-08 21:13:33 +0100350 if (message.mentions.users.size >= config.filters.pings.mass && config.filters.pings.mass) {
PineaFan0d06edc2023-01-17 22:10:31 +0000351 messageException(message.guild.id, message.channel.id, message.id);
pineafan63fc5e22022-08-04 22:04:10 +0100352 await message.delete();
Skyler Greyda16adf2023-03-05 10:22:12 +0000353 if (!(await isLogging(message.guild.id, "messageMassPing"))) return;
pineafan63fc5e22022-08-04 22:04:10 +0100354 const data = {
pineafane23c4ec2022-07-27 21:56:27 +0100355 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100356 type: "massPing",
357 displayName: "Mass Ping",
358 calculateType: "messageMassPing",
pineafane23c4ec2022-07-27 21:56:27 +0100359 color: NucleusColors.yellow,
pineafan63fc5e22022-08-04 22:04:10 +0100360 emoji: "MESSAGE.PING.MASS",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500361 timestamp: Date.now()
pineafane23c4ec2022-07-27 21:56:27 +0100362 },
363 separate: {
Skyler Grey11236ba2022-08-08 21:13:33 +0100364 start: content ? `**Message:**\n\`\`\`${content}\`\`\`` : "**Message:** *Message had no content*"
pineafane23c4ec2022-07-27 21:56:27 +0100365 },
366 list: list,
367 hidden: {
368 guild: message.channel.guild.id
pineafan813bdf42022-07-24 10:39:10 +0100369 }
pineafan63fc5e22022-08-04 22:04:10 +0100370 };
pineafane23c4ec2022-07-27 21:56:27 +0100371 return log(data);
pineafan813bdf42022-07-24 10:39:10 +0100372 }
373}