blob: d0f059cd4d1683b950630207945bbb20476eefea [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 Grey11236ba2022-08-08 21:13:33 +01003import { LinkCheck, MalwareCheck, NSFWCheck, SizeCheck, TestString, TestImage } from "../reflex/scanners.js";
pineafan63fc5e22022-08-04 22:04:10 +01004import logAttachment from "../premium/attachmentLogs.js";
5import createLogException from "../utils/createLogException.js";
6import getEmojiByName from "../utils/getEmojiByName.js";
7import client from "../utils/client.js";
pineafan0f5cc782022-08-12 21:55:42 +01008import { callback as statsChannelUpdate } from "../reflex/statsChannelUpdate.js";
Skyler Greyc634e2b2022-08-06 17:50:48 +01009import { Message, ThreadChannel } from "discord.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
pineafan63fc5e22022-08-04 22:04:10 +010013export async function callback(_client: HaikuClient, message: Message) {
Skyler Grey75ea9172022-08-06 10:22:23 +010014 if (!message.guild) return;
pineafan63fc5e22022-08-04 22:04:10 +010015 if (message.author.bot) return;
16 if (message.channel.type === "DM") return;
Skyler Grey75ea9172022-08-06 10:22:23 +010017 try {
pineafan0f5cc782022-08-12 21:55:42 +010018 await statsChannelUpdate(client, await message.guild.members.fetch(message.author.id));
Skyler Grey75ea9172022-08-06 10:22:23 +010019 } catch (e) {
20 console.log(e);
21 }
pineafan813bdf42022-07-24 10:39:10 +010022
Skyler Grey11236ba2022-08-08 21:13:33 +010023 const { log, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger;
pineafan813bdf42022-07-24 10:39:10 +010024
pineafan63fc5e22022-08-04 22:04:10 +010025 const fileNames = await logAttachment(message);
pineafan813bdf42022-07-24 10:39:10 +010026
pineafan63fc5e22022-08-04 22:04:10 +010027 const content = message.content.toLowerCase() || "";
28 const config = await client.memory.readGuildInfo(message.guild.id);
29 const filter = getEmojiByName("ICONS.FILTER");
30 let attachmentJump = "";
Skyler Grey75ea9172022-08-06 10:22:23 +010031 if (config.logging.attachments.saved[message.channel.id + message.id]) {
Skyler Grey11236ba2022-08-08 21:13:33 +010032 attachmentJump = ` [[View attachments]](${config.logging.attachments.saved[message.channel.id + message.id]})`;
Skyler Grey75ea9172022-08-06 10:22:23 +010033 }
pineafan63fc5e22022-08-04 22:04:10 +010034 const list = {
pineafan813bdf42022-07-24 10:39:10 +010035 messageId: entry(message.id, `\`${message.id}\``),
36 sentBy: entry(message.author.id, renderUser(message.author)),
37 sentIn: entry(message.channel.id, renderChannel(message.channel)),
38 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
39 mentions: message.mentions.users.size,
Skyler Grey11236ba2022-08-08 21:13:33 +010040 attachments: entry(message.attachments.size, message.attachments.size + attachmentJump),
pineafan813bdf42022-07-24 10:39:10 +010041 repliedTo: entry(
42 message.reference ? message.reference.messageId : null,
Skyler Grey75ea9172022-08-06 10:22:23 +010043 message.reference
44 ? `[[Jump to message]](https://discord.com/channels/${message.guild.id}/${message.channel.id}/${message.reference.messageId})`
45 : "None"
pineafan813bdf42022-07-24 10:39:10 +010046 )
pineafan63fc5e22022-08-04 22:04:10 +010047 };
pineafan813bdf42022-07-24 10:39:10 +010048
49 if (config.filters.invite.enabled) {
Skyler Grey11236ba2022-08-08 21:13:33 +010050 if (!config.filters.invite.allowed.channels.includes(message.channel.id)) {
51 if (/(?:https?:\/\/)?discord(?:app)?\.(?:com\/invite|gg)\/[a-zA-Z0-9]+\/?/.test(content)) {
52 createLogException(message.guild.id, message.channel.id, message.id);
pineafan813bdf42022-07-24 10:39:10 +010053 message.delete();
pineafan63fc5e22022-08-04 22:04:10 +010054 const data = {
pineafan813bdf42022-07-24 10:39:10 +010055 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010056 type: "messageDelete",
Skyler Grey11236ba2022-08-08 21:13:33 +010057 displayName: "Message Deleted (Automated, Contained Invite)",
pineafan63fc5e22022-08-04 22:04:10 +010058 calculateType: "autoModeratorDeleted",
pineafan813bdf42022-07-24 10:39:10 +010059 color: NucleusColors.red,
pineafan63fc5e22022-08-04 22:04:10 +010060 emoji: "MESSAGE.DELETE",
pineafan813bdf42022-07-24 10:39:10 +010061 timestamp: new Date().getTime()
62 },
63 separate: {
Skyler Grey75ea9172022-08-06 10:22:23 +010064 start:
65 filter +
66 " Contained invite\n\n" +
Skyler Grey11236ba2022-08-08 21:13:33 +010067 (content ? `**Message:**\n\`\`\`${content}\`\`\`` : "**Message:** *Message had no content*")
pineafan813bdf42022-07-24 10:39:10 +010068 },
69 list: list,
70 hidden: {
71 guild: message.channel.guild.id
72 }
pineafan63fc5e22022-08-04 22:04:10 +010073 };
pineafan813bdf42022-07-24 10:39:10 +010074 return log(data);
75 }
76 }
77 }
78
79 if (fileNames.files.length > 0) {
pineafan63fc5e22022-08-04 22:04:10 +010080 for (const element of fileNames.files) {
pineafan63fc5e22022-08-04 22:04:10 +010081 const url = element.url ? element.url : element.local;
Skyler Grey11236ba2022-08-08 21:13:33 +010082 if (/\.(jpg|jpeg|png|gif|gifv|webm|webp|mp4|wav|mp3|ogg)$/.test(url)) {
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)) {
Skyler Grey11236ba2022-08-08 21:13:33 +010088 createLogException(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) {
Skyler Grey11236ba2022-08-08 21:13:33 +0100123 createLogException(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))) {
Skyler Grey11236ba2022-08-08 21:13:33 +0100153 createLogException(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))) {
Skyler Grey11236ba2022-08-08 21:13:33 +0100184 createLogException(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) {
pineafan63fc5e22022-08-04 22:04:10 +0100216 createLogException(message.guild.id, message.channel.id, message.id);
217 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) {
Skyler Grey11236ba2022-08-08 21:13:33 +0100248 createLogException(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)) {
Skyler Grey11236ba2022-08-08 21:13:33 +0100297 createLogException(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) {
pineafan63fc5e22022-08-04 22:04:10 +0100323 createLogException(message.guild.id, message.channel.id, message.id);
324 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}