blob: d17dccba156e9f6ad075756c8d64fe1a55a41929 [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 Greyc634e2b2022-08-06 17:50:48 +01008import { 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;
pineafan63fc5e22022-08-04 22:04:10 +010014 if (message.author.bot) return;
PineaFan538d3752023-01-12 21:48:23 +000015 if (message.channel.isDMBased()) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010016 try {
pineafan0f5cc782022-08-12 21:55:42 +010017 await statsChannelUpdate(client, await message.guild.members.fetch(message.author.id));
Skyler Grey75ea9172022-08-06 10:22:23 +010018 } catch (e) {
19 console.log(e);
20 }
pineafan813bdf42022-07-24 10:39:10 +010021
Skyler Grey11236ba2022-08-08 21:13:33 +010022 const { log, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger;
pineafan813bdf42022-07-24 10:39:10 +010023
pineafan63fc5e22022-08-04 22:04:10 +010024 const fileNames = await logAttachment(message);
pineafan813bdf42022-07-24 10:39:10 +010025
pineafan63fc5e22022-08-04 22:04:10 +010026 const content = message.content.toLowerCase() || "";
27 const config = await client.memory.readGuildInfo(message.guild.id);
28 const filter = getEmojiByName("ICONS.FILTER");
29 let attachmentJump = "";
Skyler Grey75ea9172022-08-06 10:22:23 +010030 if (config.logging.attachments.saved[message.channel.id + message.id]) {
Skyler Grey11236ba2022-08-08 21:13:33 +010031 attachmentJump = ` [[View attachments]](${config.logging.attachments.saved[message.channel.id + message.id]})`;
Skyler Grey75ea9172022-08-06 10:22:23 +010032 }
pineafan63fc5e22022-08-04 22:04:10 +010033 const list = {
pineafan813bdf42022-07-24 10:39:10 +010034 messageId: entry(message.id, `\`${message.id}\``),
35 sentBy: entry(message.author.id, renderUser(message.author)),
36 sentIn: entry(message.channel.id, renderChannel(message.channel)),
37 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
38 mentions: message.mentions.users.size,
Skyler Grey11236ba2022-08-08 21:13:33 +010039 attachments: entry(message.attachments.size, message.attachments.size + attachmentJump),
pineafan813bdf42022-07-24 10:39:10 +010040 repliedTo: entry(
PineaFan538d3752023-01-12 21:48:23 +000041 (message.reference ? message.reference.messageId : null) ?? null,
Skyler Grey75ea9172022-08-06 10:22:23 +010042 message.reference
43 ? `[[Jump to message]](https://discord.com/channels/${message.guild.id}/${message.channel.id}/${message.reference.messageId})`
44 : "None"
pineafan813bdf42022-07-24 10:39:10 +010045 )
pineafan63fc5e22022-08-04 22:04:10 +010046 };
pineafan813bdf42022-07-24 10:39:10 +010047
48 if (config.filters.invite.enabled) {
Skyler Grey11236ba2022-08-08 21:13:33 +010049 if (!config.filters.invite.allowed.channels.includes(message.channel.id)) {
50 if (/(?:https?:\/\/)?discord(?:app)?\.(?:com\/invite|gg)\/[a-zA-Z0-9]+\/?/.test(content)) {
PineaFan0d06edc2023-01-17 22:10:31 +000051 messageException(message.guild.id, message.channel.id, message.id);
pineafan813bdf42022-07-24 10:39:10 +010052 message.delete();
pineafan63fc5e22022-08-04 22:04:10 +010053 const data = {
pineafan813bdf42022-07-24 10:39:10 +010054 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010055 type: "messageDelete",
Skyler Grey11236ba2022-08-08 21:13:33 +010056 displayName: "Message Deleted (Automated, Contained Invite)",
pineafan63fc5e22022-08-04 22:04:10 +010057 calculateType: "autoModeratorDeleted",
pineafan813bdf42022-07-24 10:39:10 +010058 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +010059 emoji: "MESSAGE.DELETE",
pineafan813bdf42022-07-24 10:39:10 +010060 timestamp: new Date().getTime()
61 },
62 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +010063 start:
64 filter +
65 " Contained invite\n\n" +
Skyler Grey11236ba2022-08-08 21:13:33 +010066 (content ? `**Message:**\n\`\`\`${content}\`\`\`` : "**Message:** *Message had no content*")
pineafan813bdf42022-07-24 10:39:10 +010067 },
68 list: list,
69 hidden: {
70 guild: message.channel.guild.id
71 }
pineafan63fc5e22022-08-04 22:04:10 +010072 };
pineafan813bdf42022-07-24 10:39:10 +010073 return log(data);
74 }
75 }
76 }
77
78 if (fileNames.files.length > 0) {
pineafan63fc5e22022-08-04 22:04:10 +010079 for (const element of fileNames.files) {
pineafan63fc5e22022-08-04 22:04:10 +010080 const url = element.url ? element.url : element.local;
TheCodedProf16113672023-02-03 16:05:23 -050081 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())) {
82 // 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 +010083 if (
Skyler Greyc634e2b2022-08-06 17:50:48 +010084 config.filters.images.NSFW &&
Skyler Grey11236ba2022-08-08 21:13:33 +010085 !(message.channel instanceof ThreadChannel ? message.channel.parent?.nsfw : message.channel.nsfw)
Skyler Grey75ea9172022-08-06 10:22:23 +010086 ) {
Skyler Greyc634e2b2022-08-06 17:50:48 +010087 if (await NSFWCheck(url)) {
PineaFan0d06edc2023-01-17 22:10:31 +000088 messageException(message.guild.id, message.channel.id, message.id);
pineafan63fc5e22022-08-04 22:04:10 +010089 await message.delete();
90 const data = {
pineafan813bdf42022-07-24 10:39:10 +010091 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010092 type: "messageDelete",
93 displayName: "Message Deleted",
94 calculateType: "autoModeratorDeleted",
pineafan813bdf42022-07-24 10:39:10 +010095 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +010096 emoji: "MESSAGE.DELETE",
pineafan813bdf42022-07-24 10:39:10 +010097 timestamp: new Date().getTime()
98 },
99 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100100 start:
101 filter +
Skyler Greyc634e2b2022-08-06 17:50:48 +0100102 " Image detected as NSFW\n\n" +
Skyler Grey75ea9172022-08-06 10:22:23 +0100103 (content
104 ? `**Message:**\n\`\`\`${content}\`\`\``
105 : "**Message:** *Message had no content*")
pineafan813bdf42022-07-24 10:39:10 +0100106 },
107 list: list,
108 hidden: {
109 guild: message.channel.guild.id
110 }
pineafan63fc5e22022-08-04 22:04:10 +0100111 };
pineafan813bdf42022-07-24 10:39:10 +0100112 return log(data);
113 }
114 }
Skyler Greyc634e2b2022-08-06 17:50:48 +0100115 if (config.filters.wordFilter.enabled) {
116 const text = await TestImage(url);
117 const check = TestString(
118 text ?? "",
119 config.filters.wordFilter.words.loose,
120 config.filters.wordFilter.words.strict
121 );
122 if (check !== null) {
PineaFan0d06edc2023-01-17 22:10:31 +0000123 messageException(message.guild.id, message.channel.id, message.id);
Skyler Greyc634e2b2022-08-06 17:50:48 +0100124 await message.delete();
125 const data = {
126 meta: {
127 type: "messageDelete",
128 displayName: "Message Deleted",
129 calculateType: "autoModeratorDeleted",
130 color: NucleusColors.red,
131 emoji: "MESSAGE.DELETE",
132 timestamp: new Date().getTime()
133 },
134 separate: {
135 start:
136 filter +
137 " Image contained filtered word\n\n" +
138 (content
139 ? `**Message:**\n\`\`\`${content}\`\`\``
140 : "**Message:** *Message had no content*")
141 },
142 list: list,
143 hidden: {
144 guild: message.channel.guild.id
145 }
146 };
147 return log(data);
148 }
149 }
150 if (config.filters.images.size) {
151 if (url.match(/\.+(webp|png|jpg)$/gi)) {
152 if (!(await SizeCheck(element))) {
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",
162 timestamp: new Date().getTime()
163 },
164 separate: {
165 start:
166 filter +
167 " Image was too small\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 }
181 }
182 if (config.filters.malware) {
183 if (!(await MalwareCheck(url))) {
PineaFan0d06edc2023-01-17 22:10:31 +0000184 messageException(message.guild.id, message.channel.id, message.id);
Skyler Greyc634e2b2022-08-06 17:50:48 +0100185 await message.delete();
186 const data = {
187 meta: {
188 type: "messageDelete",
189 displayName: "Message Deleted",
190 calculateType: "autoModeratorDeleted",
191 color: NucleusColors.red,
192 emoji: "MESSAGE.DELETE",
193 timestamp: new Date().getTime()
194 },
195 separate: {
196 start:
197 filter +
198 " File detected as malware\n\n" +
199 (content
200 ? `**Message:**\n\`\`\`${content}\`\`\``
201 : "**Message:** *Message had no content*")
202 },
203 list: list,
204 hidden: {
205 guild: message.channel.guild.id
206 }
207 };
208 return log(data);
209 }
pineafan813bdf42022-07-24 10:39:10 +0100210 }
pineafan63fc5e22022-08-04 22:04:10 +0100211 }
pineafan813bdf42022-07-24 10:39:10 +0100212 }
pineafan813bdf42022-07-24 10:39:10 +0100213
pineafan63fc5e22022-08-04 22:04:10 +0100214 const linkDetectionTypes = await LinkCheck(message);
pineafan813bdf42022-07-24 10:39:10 +0100215 if (linkDetectionTypes.length > 0) {
PineaFan0d06edc2023-01-17 22:10:31 +0000216 messageException(message.guild.id, message.channel.id, message.id);
pineafan63fc5e22022-08-04 22:04:10 +0100217 await message.delete();
218 const data = {
pineafan813bdf42022-07-24 10:39:10 +0100219 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100220 type: "messageDelete",
221 displayName: "Message Deleted",
222 calculateType: "autoModeratorDeleted",
pineafan813bdf42022-07-24 10:39:10 +0100223 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +0100224 emoji: "MESSAGE.DELETE",
pineafan813bdf42022-07-24 10:39:10 +0100225 timestamp: new Date().getTime()
226 },
227 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100228 start:
229 filter +
230 ` Link filtered as ${linkDetectionTypes[0]?.toLowerCase()}\n\n` +
Skyler Grey11236ba2022-08-08 21:13:33 +0100231 (content ? `**Message:**\n\`\`\`${content}\`\`\`` : "**Message:** *Message had no content*")
pineafan813bdf42022-07-24 10:39:10 +0100232 },
233 list: list,
234 hidden: {
235 guild: message.channel.guild.id
236 }
pineafan63fc5e22022-08-04 22:04:10 +0100237 };
pineafan813bdf42022-07-24 10:39:10 +0100238 return log(data);
239 }
pineafane23c4ec2022-07-27 21:56:27 +0100240
pineafan813bdf42022-07-24 10:39:10 +0100241 if (config.filters.wordFilter.enabled) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100242 const check = TestString(
243 content,
244 config.filters.wordFilter.words.loose,
245 config.filters.wordFilter.words.strict
246 );
247 if (check !== null) {
PineaFan0d06edc2023-01-17 22:10:31 +0000248 messageException(message.guild.id, message.channel.id, message.id);
pineafan63fc5e22022-08-04 22:04:10 +0100249 await message.delete();
250 const data = {
pineafan813bdf42022-07-24 10:39:10 +0100251 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100252 type: "messageDelete",
253 displayName: "Message Deleted",
254 calculateType: "autoModeratorDeleted",
pineafan813bdf42022-07-24 10:39:10 +0100255 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +0100256 emoji: "MESSAGE.DELETE",
pineafan813bdf42022-07-24 10:39:10 +0100257 timestamp: new Date().getTime()
258 },
259 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100260 start:
261 filter +
262 " Message contained filtered word\n\n" +
Skyler Grey11236ba2022-08-08 21:13:33 +0100263 (content ? `**Message:**\n\`\`\`${content}\`\`\`` : "**Message:** *Message had no content*")
pineafan813bdf42022-07-24 10:39:10 +0100264 },
265 list: list,
266 hidden: {
267 guild: message.channel.guild.id
268 }
pineafan63fc5e22022-08-04 22:04:10 +0100269 };
pineafan813bdf42022-07-24 10:39:10 +0100270 return log(data);
271 }
272 }
273
pineafane23c4ec2022-07-27 21:56:27 +0100274 if (config.filters.pings.everyone && message.mentions.everyone) {
pineafan63fc5e22022-08-04 22:04:10 +0100275 const data = {
pineafane23c4ec2022-07-27 21:56:27 +0100276 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100277 type: "everyonePing",
278 displayName: "Everyone Pinged",
279 calculateType: "messageMassPing",
pineafane23c4ec2022-07-27 21:56:27 +0100280 color: NucleusColors.yellow,
pineafan63fc5e22022-08-04 22:04:10 +0100281 emoji: "MESSAGE.PING.EVERYONE",
pineafane23c4ec2022-07-27 21:56:27 +0100282 timestamp: new Date().getTime()
283 },
284 separate: {
Skyler Grey11236ba2022-08-08 21:13:33 +0100285 start: content ? `**Message:**\n\`\`\`${content}\`\`\`` : "**Message:** *Message had no content*"
pineafane23c4ec2022-07-27 21:56:27 +0100286 },
287 list: list,
288 hidden: {
289 guild: message.channel.guild.id
pineafan813bdf42022-07-24 10:39:10 +0100290 }
pineafan63fc5e22022-08-04 22:04:10 +0100291 };
pineafane23c4ec2022-07-27 21:56:27 +0100292 return log(data);
293 }
294 if (config.filters.pings.roles) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100295 for (const roleId in message.mentions.roles) {
pineafan63fc5e22022-08-04 22:04:10 +0100296 if (!config.filters.pings.allowed.roles.includes(roleId)) {
PineaFan0d06edc2023-01-17 22:10:31 +0000297 messageException(message.guild.id, message.channel.id, message.id);
pineafan63fc5e22022-08-04 22:04:10 +0100298 await message.delete();
299 const data = {
pineafane23c4ec2022-07-27 21:56:27 +0100300 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100301 type: "rolePing",
302 displayName: "Role Pinged",
303 calculateType: "messageMassPing",
pineafane23c4ec2022-07-27 21:56:27 +0100304 color: NucleusColors.yellow,
pineafan63fc5e22022-08-04 22:04:10 +0100305 emoji: "MESSAGE.PING.ROLE",
pineafane23c4ec2022-07-27 21:56:27 +0100306 timestamp: new Date().getTime()
307 },
308 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100309 start: content
310 ? `**Message:**\n\`\`\`${content}\`\`\``
311 : "**Message:** *Message had no content*"
pineafane23c4ec2022-07-27 21:56:27 +0100312 },
313 list: list,
314 hidden: {
315 guild: message.channel.guild.id
pineafan813bdf42022-07-24 10:39:10 +0100316 }
pineafan63fc5e22022-08-04 22:04:10 +0100317 };
pineafane23c4ec2022-07-27 21:56:27 +0100318 return log(data);
pineafan813bdf42022-07-24 10:39:10 +0100319 }
320 }
pineafane23c4ec2022-07-27 21:56:27 +0100321 }
Skyler Grey11236ba2022-08-08 21:13:33 +0100322 if (message.mentions.users.size >= config.filters.pings.mass && config.filters.pings.mass) {
PineaFan0d06edc2023-01-17 22:10:31 +0000323 messageException(message.guild.id, message.channel.id, message.id);
pineafan63fc5e22022-08-04 22:04:10 +0100324 await message.delete();
325 const data = {
pineafane23c4ec2022-07-27 21:56:27 +0100326 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100327 type: "massPing",
328 displayName: "Mass Ping",
329 calculateType: "messageMassPing",
pineafane23c4ec2022-07-27 21:56:27 +0100330 color: NucleusColors.yellow,
pineafan63fc5e22022-08-04 22:04:10 +0100331 emoji: "MESSAGE.PING.MASS",
pineafane23c4ec2022-07-27 21:56:27 +0100332 timestamp: new Date().getTime()
333 },
334 separate: {
Skyler Grey11236ba2022-08-08 21:13:33 +0100335 start: content ? `**Message:**\n\`\`\`${content}\`\`\`` : "**Message:** *Message had no content*"
pineafane23c4ec2022-07-27 21:56:27 +0100336 },
337 list: list,
338 hidden: {
339 guild: message.channel.guild.id
pineafan813bdf42022-07-24 10:39:10 +0100340 }
pineafan63fc5e22022-08-04 22:04:10 +0100341 };
pineafane23c4ec2022-07-27 21:56:27 +0100342 return log(data);
pineafan813bdf42022-07-24 10:39:10 +0100343 }
344}