blob: 459b1d68ad55df1817cd42e7359ffdb32f2b91d7 [file] [log] [blame]
Skyler Greyda16adf2023-03-05 10:22:12 +00001import {
2 ButtonStyle,
3 CommandInteraction,
4 ComponentType,
5 GuildMember,
6 Message,
7 MessageComponentInteraction
8} from "discord.js";
pineafan63fc5e22022-08-04 22:04:10 +01009import type Discord from "discord.js";
10import { Collection, MongoClient } from "mongodb";
pineafana2e39c72023-02-21 18:37:32 +000011import config from "../config/main.js";
TheCodedProf633866f2023-02-03 17:06:00 -050012import client from "../utils/client.js";
TheCodedProf088b1b22023-02-28 17:31:11 -050013import * as crypto from "crypto";
TheCodedProff8ef7942023-03-03 15:32:32 -050014import _ from "lodash";
Skyler Greyda16adf2023-03-05 10:22:12 +000015import defaultData from "../config/default.js";
TheCodedProf75276572023-03-04 13:49:16 -050016
pineafan6de4da52023-03-07 20:43:44 +000017let username, password;
18
Skyler Grey5b78b422023-03-07 22:36:20 +000019if ("username" in config.mongoOptions) username = encodeURIComponent(config.mongoOptions.username as string);
20if ("password" in config.mongoOptions) password = encodeURIComponent(config.mongoOptions.password as string);
Samuel Shuertd66098b2023-03-04 14:05:26 -050021
Skyler Greyda16adf2023-03-05 10:22:12 +000022const mongoClient = new MongoClient(
23 username
pineafan6de4da52023-03-07 20:43:44 +000024 ? `mongodb://${username}:${password}@${config.mongoOptions.host}?authMechanism=DEFAULT&authSource=${config.mongoOptions.authSource}`
25 : `mongodb://${config.mongoOptions.host}`
Skyler Greyda16adf2023-03-05 10:22:12 +000026);
pineafan63fc5e22022-08-04 22:04:10 +010027await mongoClient.connect();
TheCodedProf3d54ade2023-04-22 21:40:42 -040028export const database = mongoClient.db();
pineafan6fb3e072022-05-20 19:27:23 +010029
TheCodedProf78b90332023-03-04 14:02:21 -050030const collectionOptions = { authdb: config.mongoOptions.authSource, w: "majority" };
TheCodedProf75c51be2023-03-03 17:18:18 -050031const getIV = () => crypto.randomBytes(16);
TheCodedProffaae5332023-03-01 18:16:05 -050032
pineafan4edb7762022-06-26 19:21:04 +010033export class Guilds {
pineafan6fb3e072022-05-20 19:27:23 +010034 guilds: Collection<GuildConfig>;
TheCodedProf8a2d7cd2023-03-05 14:53:59 -050035 oldGuilds: Collection<GuildConfig>;
TheCodedProff8ef7942023-03-03 15:32:32 -050036 defaultData: GuildConfig;
pineafan63fc5e22022-08-04 22:04:10 +010037
38 constructor() {
pineafan4edb7762022-06-26 19:21:04 +010039 this.guilds = database.collection<GuildConfig>("guilds");
TheCodedProff8ef7942023-03-03 15:32:32 -050040 this.defaultData = defaultData;
TheCodedProf8a2d7cd2023-03-05 14:53:59 -050041 this.oldGuilds = database.collection<GuildConfig>("oldGuilds");
42 }
43
44 async readOld(guild: string): Promise<Partial<GuildConfig>> {
45 // console.log("Guild read")
46 const entry = await this.oldGuilds.findOne({ id: guild });
47 return entry ?? {};
pineafan63fc5e22022-08-04 22:04:10 +010048 }
49
TheCodedProfb7a7b992023-03-05 16:11:59 -050050 async updateAllGuilds() {
51 const guilds = await this.guilds.find().toArray();
52 for (const guild of guilds) {
53 let guildObj;
54 try {
55 guildObj = await client.guilds.fetch(guild.id);
56 } catch (e) {
57 guildObj = null;
58 }
Skyler Grey67691762023-03-06 09:58:19 +000059 if (!guildObj) await this.delete(guild.id);
TheCodedProfb7a7b992023-03-05 16:11:59 -050060 }
61 }
62
Skyler Greyad002172022-08-16 18:48:26 +010063 async read(guild: string): Promise<GuildConfig> {
TheCodedProff8ef7942023-03-03 15:32:32 -050064 // console.log("Guild read")
pineafan63fc5e22022-08-04 22:04:10 +010065 const entry = await this.guilds.findOne({ id: guild });
TheCodedProf9f4cf9f2023-03-04 14:18:19 -050066 const data = _.cloneDeep(this.defaultData);
TheCodedProff8ef7942023-03-03 15:32:32 -050067 return _.merge(data, entry ?? {});
pineafan6fb3e072022-05-20 19:27:23 +010068 }
69
Skyler Grey11236ba2022-08-08 21:13:33 +010070 async write(guild: string, set: object | null, unset: string[] | string = []) {
TheCodedProff8ef7942023-03-03 15:32:32 -050071 // console.log("Guild write")
pineafan63fc5e22022-08-04 22:04:10 +010072 // eslint-disable-next-line @typescript-eslint/no-explicit-any
73 const uo: Record<string, any> = {};
74 if (!Array.isArray(unset)) unset = [unset];
75 for (const key of unset) {
pineafan0bc04162022-07-25 17:22:26 +010076 uo[key] = null;
pineafan6702cef2022-06-13 17:52:37 +010077 }
Skyler Grey75ea9172022-08-06 10:22:23 +010078 const out = { $set: {}, $unset: {} };
79 if (set) out.$set = set;
80 if (unset.length) out.$unset = uo;
pineafan0bc04162022-07-25 17:22:26 +010081 await this.guilds.updateOne({ id: guild }, out, { upsert: true });
pineafan6702cef2022-06-13 17:52:37 +010082 }
83
pineafan63fc5e22022-08-04 22:04:10 +010084 // eslint-disable-next-line @typescript-eslint/no-explicit-any
pineafan6702cef2022-06-13 17:52:37 +010085 async append(guild: string, key: string, value: any) {
TheCodedProff8ef7942023-03-03 15:32:32 -050086 // console.log("Guild append")
pineafan6702cef2022-06-13 17:52:37 +010087 if (Array.isArray(value)) {
Skyler Grey75ea9172022-08-06 10:22:23 +010088 await this.guilds.updateOne(
89 { id: guild },
90 {
91 $addToSet: { [key]: { $each: value } }
92 },
93 { upsert: true }
94 );
pineafan6702cef2022-06-13 17:52:37 +010095 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +010096 await this.guilds.updateOne(
97 { id: guild },
98 {
99 $addToSet: { [key]: value }
100 },
101 { upsert: true }
102 );
pineafan6702cef2022-06-13 17:52:37 +0100103 }
104 }
105
Skyler Grey75ea9172022-08-06 10:22:23 +0100106 async remove(
107 guild: string,
108 key: string,
Skyler Greyc634e2b2022-08-06 17:50:48 +0100109 // eslint-disable-next-line @typescript-eslint/no-explicit-any
Skyler Grey75ea9172022-08-06 10:22:23 +0100110 value: any,
111 innerKey?: string | null
112 ) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500113 // console.log("Guild remove")
pineafan02ba0232022-07-24 22:16:15 +0100114 if (innerKey) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100115 await this.guilds.updateOne(
116 { id: guild },
117 {
118 $pull: { [key]: { [innerKey]: { $eq: value } } }
119 },
120 { upsert: true }
121 );
pineafan0bc04162022-07-25 17:22:26 +0100122 } else if (Array.isArray(value)) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100123 await this.guilds.updateOne(
124 { id: guild },
125 {
126 $pullAll: { [key]: value }
127 },
128 { upsert: true }
129 );
pineafan6702cef2022-06-13 17:52:37 +0100130 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100131 await this.guilds.updateOne(
132 { id: guild },
133 {
134 $pullAll: { [key]: [value] }
135 },
136 { upsert: true }
137 );
pineafan6702cef2022-06-13 17:52:37 +0100138 }
pineafan6fb3e072022-05-20 19:27:23 +0100139 }
pineafane23c4ec2022-07-27 21:56:27 +0100140
141 async delete(guild: string) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500142 // console.log("Guild delete")
pineafane23c4ec2022-07-27 21:56:27 +0100143 await this.guilds.deleteOne({ id: guild });
144 }
TheCodedProfa38cbb32023-03-11 17:22:25 -0500145
146 async staffChannels(): Promise<string[]> {
Skyler Grey6a0bab52023-03-15 00:10:26 +0000147 const entries = (
148 await this.guilds
149 .find(
150 { "logging.staff.channel": { $exists: true } },
151 { projection: { "logging.staff.channel": 1, _id: 0 } }
152 )
153 .toArray()
154 ).map((e) => e.logging.staff.channel);
TheCodedProfe4ca5142023-03-14 18:09:03 -0400155 const out: string[] = [];
156 for (const entry of entries) {
157 if (entry) out.push(entry);
158 }
159 return out;
TheCodedProfa38cbb32023-03-11 17:22:25 -0500160 }
pineafan6fb3e072022-05-20 19:27:23 +0100161}
162
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500163interface TranscriptEmbed {
164 title?: string;
165 description?: string;
166 fields?: {
167 name: string;
168 value: string;
169 inline: boolean;
170 }[];
171 footer?: {
172 text: string;
173 iconURL?: string;
174 };
TheCodedProffaae5332023-03-01 18:16:05 -0500175 color?: number;
176 timestamp?: string;
177 author?: {
178 name: string;
179 iconURL?: string;
180 url?: string;
181 };
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500182}
183
184interface TranscriptComponent {
185 type: number;
186 style?: ButtonStyle;
187 label?: string;
188 description?: string;
189 placeholder?: string;
190 emojiURL?: string;
191}
192
193interface TranscriptAuthor {
194 username: string;
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500195 nickname?: string;
196 id: string;
197 iconURL?: string;
198 topRole: {
199 color: number;
200 badgeURL?: string;
TheCodedProf088b1b22023-02-28 17:31:11 -0500201 };
202 bot: boolean;
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500203}
204
205interface TranscriptAttachment {
206 url: string;
207 filename: string;
208 size: number;
209 log?: string;
210}
211
212interface TranscriptMessage {
213 id: string;
214 author: TranscriptAuthor;
215 content?: string;
216 embeds?: TranscriptEmbed[];
217 components?: TranscriptComponent[][];
218 editedTimestamp?: number;
219 createdTimestamp: number;
220 flags?: string[];
221 attachments?: TranscriptAttachment[];
222 stickerURLs?: string[];
Skyler Greyda16adf2023-03-05 10:22:12 +0000223 referencedMessage?: string | [string, string, string]; // the message id, the channel id, the guild id
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500224}
225
226interface TranscriptSchema {
227 code: string;
228 for: TranscriptAuthor;
Skyler Greyda16adf2023-03-05 10:22:12 +0000229 type: "ticket" | "purge";
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500230 guild: string;
231 channel: string;
232 messages: TranscriptMessage[];
233 createdTimestamp: number;
234 createdBy: TranscriptAuthor;
235}
236
Skyler Greyda16adf2023-03-05 10:22:12 +0000237interface findDocSchema {
238 channelID: string;
239 messageID: string;
Skyler Grey5b78b422023-03-07 22:36:20 +0000240 code: string;
Skyler Greyda16adf2023-03-05 10:22:12 +0000241}
TheCodedProf003160f2023-03-04 17:09:40 -0500242
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500243export class Transcript {
244 transcripts: Collection<TranscriptSchema>;
TheCodedProf003160f2023-03-04 17:09:40 -0500245 messageToTranscript: Collection<findDocSchema>;
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500246
247 constructor() {
248 this.transcripts = database.collection<TranscriptSchema>("transcripts");
TheCodedProf003160f2023-03-04 17:09:40 -0500249 this.messageToTranscript = database.collection<findDocSchema>("messageToTranscript");
250 }
251
252 async upload(data: findDocSchema) {
253 // console.log("Transcript upload")
254 await this.messageToTranscript.insertOne(data);
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500255 }
256
257 async create(transcript: Omit<TranscriptSchema, "code">) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500258 // console.log("Transcript create")
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500259 let code;
260 do {
TheCodedProf088b1b22023-02-28 17:31:11 -0500261 code = crypto.randomBytes(64).toString("base64").replace(/=/g, "").replace(/\//g, "_").replace(/\+/g, "-");
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500262 } while (await this.transcripts.findOne({ code: code }));
Skyler Greyda16adf2023-03-05 10:22:12 +0000263 const key = crypto
264 .randomBytes(32 ** 2)
265 .toString("base64")
266 .replace(/=/g, "")
267 .replace(/\//g, "_")
268 .replace(/\+/g, "-")
269 .substring(0, 32);
Skyler Grey67691762023-03-06 09:58:19 +0000270 const iv = getIV()
271 .toString("base64")
272 .substring(0, 16)
273 .replace(/=/g, "")
274 .replace(/\//g, "_")
275 .replace(/\+/g, "-");
Skyler Greyda16adf2023-03-05 10:22:12 +0000276 for (const message of transcript.messages) {
277 if (message.content) {
TheCodedProf75c51be2023-03-03 17:18:18 -0500278 const encCipher = crypto.createCipheriv("AES-256-CBC", key, iv);
279 message.content = encCipher.update(message.content, "utf8", "base64") + encCipher.final("base64");
280 }
281 }
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500282
TheCodedProffaae5332023-03-01 18:16:05 -0500283 const doc = await this.transcripts.insertOne(Object.assign(transcript, { code: code }), collectionOptions);
Skyler Greyda16adf2023-03-05 10:22:12 +0000284 if (doc.acknowledged) {
Skyler Greyf4f21c42023-03-08 14:36:29 +0000285 await client.database.eventScheduler.schedule(
Skyler Greyda16adf2023-03-05 10:22:12 +0000286 "deleteTranscript",
287 (Date.now() + 1000 * 60 * 60 * 24 * 7).toString(),
288 { guild: transcript.guild, code: code, iv: iv, key: key }
289 );
TheCodedProf003160f2023-03-04 17:09:40 -0500290 return [code, key, iv];
Skyler Greyda16adf2023-03-05 10:22:12 +0000291 } else return [null, null, null];
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500292 }
293
TheCodedProf003160f2023-03-04 17:09:40 -0500294 async delete(code: string) {
295 // console.log("Transcript delete")
296 await this.transcripts.deleteOne({ code: code });
TheCodedProf75c51be2023-03-03 17:18:18 -0500297 }
298
299 async deleteAll(guild: string) {
300 // console.log("Transcript delete")
301 const filteredDocs = await this.transcripts.find({ guild: guild }).toArray();
302 for (const doc of filteredDocs) {
303 await this.transcripts.deleteOne({ code: doc.code });
304 }
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500305 }
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500306
TheCodedProf003160f2023-03-04 17:09:40 -0500307 async readEncrypted(code: string) {
308 // console.log("Transcript read")
309 let doc: TranscriptSchema | null = await this.transcripts.findOne({ code: code });
310 let findDoc: findDocSchema | null = null;
pineafan6de4da52023-03-07 20:43:44 +0000311 if (!doc) findDoc = await this.messageToTranscript.findOne({ transcript: code });
Skyler Greyda16adf2023-03-05 10:22:12 +0000312 if (findDoc) {
313 const message = await (
314 client.channels.cache.get(findDoc.channelID) as Discord.TextBasedChannel | null
315 )?.messages.fetch(findDoc.messageID);
316 if (!message) return null;
TheCodedProf003160f2023-03-04 17:09:40 -0500317 const attachment = message.attachments.first();
Skyler Greyda16adf2023-03-05 10:22:12 +0000318 if (!attachment) return null;
TheCodedProf003160f2023-03-04 17:09:40 -0500319 const transcript = (await fetch(attachment.url)).body;
Skyler Greyda16adf2023-03-05 10:22:12 +0000320 if (!transcript) return null;
TheCodedProf003160f2023-03-04 17:09:40 -0500321 const reader = transcript.getReader();
322 let data: Uint8Array | null = null;
323 let allPacketsReceived = false;
324 while (!allPacketsReceived) {
325 const { value, done } = await reader.read();
Skyler Greyda16adf2023-03-05 10:22:12 +0000326 if (done) {
327 allPacketsReceived = true;
328 continue;
329 }
330 if (!data) {
TheCodedProf003160f2023-03-04 17:09:40 -0500331 data = value;
332 } else {
333 data = new Uint8Array(Buffer.concat([data, value]));
334 }
335 }
Skyler Greyda16adf2023-03-05 10:22:12 +0000336 if (!data) return null;
Skyler Greycf771402023-03-05 07:06:37 +0000337 doc = JSON.parse(Buffer.from(data).toString()) as TranscriptSchema;
TheCodedProf003160f2023-03-04 17:09:40 -0500338 }
Skyler Greyda16adf2023-03-05 10:22:12 +0000339 if (!doc) return null;
TheCodedProf003160f2023-03-04 17:09:40 -0500340 return doc;
341 }
342
343 async read(code: string, key: string, iv: string) {
TheCodedProf003160f2023-03-04 17:09:40 -0500344 let doc: TranscriptSchema | null = await this.transcripts.findOne({ code: code });
345 let findDoc: findDocSchema | null = null;
pineafan6de4da52023-03-07 20:43:44 +0000346 if (!doc) findDoc = await this.messageToTranscript.findOne({ transcript: code });
Skyler Greyda16adf2023-03-05 10:22:12 +0000347 if (findDoc) {
348 const message = await (
349 client.channels.cache.get(findDoc.channelID) as Discord.TextBasedChannel | null
350 )?.messages.fetch(findDoc.messageID);
351 if (!message) return null;
TheCodedProf003160f2023-03-04 17:09:40 -0500352 const attachment = message.attachments.first();
Skyler Greyda16adf2023-03-05 10:22:12 +0000353 if (!attachment) return null;
TheCodedProf003160f2023-03-04 17:09:40 -0500354 const transcript = (await fetch(attachment.url)).body;
Skyler Greyda16adf2023-03-05 10:22:12 +0000355 if (!transcript) return null;
TheCodedProf003160f2023-03-04 17:09:40 -0500356 const reader = transcript.getReader();
357 let data: Uint8Array | null = null;
358 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, no-constant-condition
Skyler Greyda16adf2023-03-05 10:22:12 +0000359 while (true) {
TheCodedProf003160f2023-03-04 17:09:40 -0500360 const { value, done } = await reader.read();
361 if (done) break;
Skyler Greyda16adf2023-03-05 10:22:12 +0000362 if (!data) {
TheCodedProf003160f2023-03-04 17:09:40 -0500363 data = value;
364 } else {
365 data = new Uint8Array(Buffer.concat([data, value]));
366 }
367 }
Skyler Greyda16adf2023-03-05 10:22:12 +0000368 if (!data) return null;
Skyler Greycf771402023-03-05 07:06:37 +0000369 doc = JSON.parse(Buffer.from(data).toString()) as TranscriptSchema;
TheCodedProf003160f2023-03-04 17:09:40 -0500370 }
Skyler Greyda16adf2023-03-05 10:22:12 +0000371 if (!doc) return null;
372 for (const message of doc.messages) {
373 if (message.content) {
TheCodedProf003160f2023-03-04 17:09:40 -0500374 const decCipher = crypto.createDecipheriv("AES-256-CBC", key, iv);
375 message.content = decCipher.update(message.content, "base64", "utf8") + decCipher.final("utf8");
376 }
377 }
378 return doc;
379 }
380
Skyler Greyda16adf2023-03-05 10:22:12 +0000381 async createTranscript(
Skyler Greye0c511b2023-03-06 10:30:17 +0000382 type: "ticket" | "purge",
Skyler Greyda16adf2023-03-05 10:22:12 +0000383 messages: Message[],
384 interaction: MessageComponentInteraction | CommandInteraction,
385 member: GuildMember
386 ) {
387 const interactionMember = await interaction.guild?.members.fetch(interaction.user.id);
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500388 const newOut: Omit<TranscriptSchema, "code"> = {
Skyler Greye0c511b2023-03-06 10:30:17 +0000389 type: type,
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500390 for: {
391 username: member!.user.username,
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500392 id: member!.user.id,
393 topRole: {
394 color: member!.roles.highest.color
TheCodedProf088b1b22023-02-28 17:31:11 -0500395 },
Skyler Greyda16adf2023-03-05 10:22:12 +0000396 iconURL: member!.user.displayAvatarURL({ forceStatic: true }),
TheCodedProf088b1b22023-02-28 17:31:11 -0500397 bot: member!.user.bot
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500398 },
399 guild: interaction.guild!.id,
400 channel: interaction.channel!.id,
401 messages: [],
402 createdTimestamp: Date.now(),
403 createdBy: {
404 username: interaction.user.username,
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500405 id: interaction.user.id,
406 topRole: {
407 color: interactionMember?.roles.highest.color ?? 0x000000
TheCodedProf088b1b22023-02-28 17:31:11 -0500408 },
Skyler Greyda16adf2023-03-05 10:22:12 +0000409 iconURL: interaction.user.displayAvatarURL({ forceStatic: true }),
TheCodedProf088b1b22023-02-28 17:31:11 -0500410 bot: interaction.user.bot
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500411 }
Skyler Greyda16adf2023-03-05 10:22:12 +0000412 };
413 if (member.nickname) newOut.for.nickname = member.nickname;
414 if (interactionMember?.roles.icon) newOut.createdBy.topRole.badgeURL = interactionMember.roles.icon.iconURL()!;
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500415 messages.reverse().forEach((message) => {
416 const msg: TranscriptMessage = {
417 id: message.id,
418 author: {
419 username: message.author.username,
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500420 id: message.author.id,
421 topRole: {
Skyler Greya0c70242023-03-06 09:56:21 +0000422 color: message.member ? message.member.roles.highest.color : 0x000000
TheCodedProf088b1b22023-02-28 17:31:11 -0500423 },
TheCodedProfe92b9b52023-03-06 17:07:34 -0500424 iconURL: (message.member?.user ?? message.author).displayAvatarURL({ forceStatic: true }),
Skyler Grey67691762023-03-06 09:58:19 +0000425 bot: message.author.bot || false
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500426 },
427 createdTimestamp: message.createdTimestamp
428 };
Skyler Greyda16adf2023-03-05 10:22:12 +0000429 if (message.member?.nickname) msg.author.nickname = message.member.nickname;
Skyler Greya0c70242023-03-06 09:56:21 +0000430 if (message.member?.roles.icon) msg.author.topRole.badgeURL = message.member!.roles.icon.iconURL()!;
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500431 if (message.content) msg.content = message.content;
Skyler Greyda16adf2023-03-05 10:22:12 +0000432 if (message.embeds.length > 0)
433 msg.embeds = message.embeds.map((embed) => {
434 const obj: TranscriptEmbed = {};
435 if (embed.title) obj.title = embed.title;
436 if (embed.description) obj.description = embed.description;
437 if (embed.fields.length > 0)
438 obj.fields = embed.fields.map((field) => {
439 return {
440 name: field.name,
441 value: field.value,
442 inline: field.inline ?? false
443 };
444 });
445 if (embed.color) obj.color = embed.color;
446 if (embed.timestamp) obj.timestamp = embed.timestamp;
447 if (embed.footer)
448 obj.footer = {
449 text: embed.footer.text
450 };
451 if (embed.footer?.iconURL) obj.footer!.iconURL = embed.footer.iconURL;
452 if (embed.author)
453 obj.author = {
454 name: embed.author.name
455 };
456 if (embed.author?.iconURL) obj.author!.iconURL = embed.author.iconURL;
457 if (embed.author?.url) obj.author!.url = embed.author.url;
458 return obj;
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500459 });
Skyler Greyda16adf2023-03-05 10:22:12 +0000460 if (message.components.length > 0)
461 msg.components = message.components.map((component) =>
462 component.components.map((child) => {
463 const obj: TranscriptComponent = {
464 type: child.type
465 };
466 if (child.type === ComponentType.Button) {
467 obj.style = child.style;
468 obj.label = child.label ?? "";
469 } else if (child.type > 2) {
470 obj.placeholder = child.placeholder ?? "";
471 }
472 return obj;
473 })
474 );
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500475 if (message.editedTimestamp) msg.editedTimestamp = message.editedTimestamp;
476 msg.flags = message.flags.toArray();
477
Skyler Greyda16adf2023-03-05 10:22:12 +0000478 if (message.stickers.size > 0) msg.stickerURLs = message.stickers.map((sticker) => sticker.url);
479 if (message.reference)
480 msg.referencedMessage = [
481 message.reference.guildId ?? "",
482 message.reference.channelId,
483 message.reference.messageId ?? ""
484 ];
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500485 newOut.messages.push(msg);
486 });
487 return newOut;
488 }
489
490 toHumanReadable(transcript: Omit<TranscriptSchema, "code">): string {
491 let out = "";
492 for (const message of transcript.messages) {
493 if (message.referencedMessage) {
494 if (Array.isArray(message.referencedMessage)) {
495 out += `> [Crosspost From] ${message.referencedMessage[0]} in ${message.referencedMessage[1]} in ${message.referencedMessage[2]}\n`;
Skyler Greyda16adf2023-03-05 10:22:12 +0000496 } else out += `> [Reply To] ${message.referencedMessage}\n`;
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500497 }
pineafancfb149f2023-05-28 15:46:30 +0100498 out += `${message.author.nickname ?? message.author.username} (${message.author.id}) (${message.id})`;
TheCodedProff8ef7942023-03-03 15:32:32 -0500499 out += ` [${new Date(message.createdTimestamp).toISOString()}]`;
500 if (message.editedTimestamp) out += ` [Edited: ${new Date(message.editedTimestamp).toISOString()}]`;
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500501 out += "\n";
502 if (message.content) out += `[Content]\n${message.content}\n\n`;
503 if (message.embeds) {
504 for (const embed of message.embeds) {
505 out += `[Embed]\n`;
506 if (embed.title) out += `| Title: ${embed.title}\n`;
507 if (embed.description) out += `| Description: ${embed.description}\n`;
508 if (embed.fields) {
509 for (const field of embed.fields) {
510 out += `| Field: ${field.name} - ${field.value}\n`;
511 }
512 }
513 if (embed.footer) {
514 out += `|Footer: ${embed.footer.text}\n`;
515 }
516 out += "\n";
517 }
518 }
519 if (message.components) {
520 for (const component of message.components) {
521 out += `[Component]\n`;
522 for (const button of component) {
523 out += `| Button: ${button.label ?? button.description}\n`;
524 }
525 out += "\n";
526 }
527 }
528 if (message.attachments) {
529 for (const attachment of message.attachments) {
530 out += `[Attachment] ${attachment.filename} (${attachment.size} bytes) ${attachment.url}\n`;
531 }
532 }
Skyler Greyda16adf2023-03-05 10:22:12 +0000533 out += "\n\n";
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500534 }
Skyler Greyda16adf2023-03-05 10:22:12 +0000535 return out;
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500536 }
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500537}
538
pineafan4edb7762022-06-26 19:21:04 +0100539export class History {
540 histories: Collection<HistorySchema>;
pineafan4edb7762022-06-26 19:21:04 +0100541
pineafan3a02ea32022-08-11 21:35:04 +0100542 constructor() {
pineafan4edb7762022-06-26 19:21:04 +0100543 this.histories = database.collection<HistorySchema>("history");
pineafan4edb7762022-06-26 19:21:04 +0100544 }
545
Skyler Grey75ea9172022-08-06 10:22:23 +0100546 async create(
547 type: string,
548 guild: string,
549 user: Discord.User,
550 moderator: Discord.User | null,
551 reason: string | null,
pineafan3a02ea32022-08-11 21:35:04 +0100552 before?: string | null,
553 after?: string | null,
554 amount?: string | null
Skyler Grey75ea9172022-08-06 10:22:23 +0100555 ) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500556 // console.log("History create");
Skyler Greyda16adf2023-03-05 10:22:12 +0000557 await this.histories.insertOne(
558 {
559 type: type,
560 guild: guild,
561 user: user.id,
562 moderator: moderator ? moderator.id : null,
563 reason: reason,
564 occurredAt: new Date(),
565 before: before ?? null,
566 after: after ?? null,
567 amount: amount ?? null
568 },
569 collectionOptions
570 );
pineafan4edb7762022-06-26 19:21:04 +0100571 }
572
573 async read(guild: string, user: string, year: number) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500574 // console.log("History read");
Skyler Grey75ea9172022-08-06 10:22:23 +0100575 const entry = (await this.histories
576 .find({
577 guild: guild,
578 user: user,
579 occurredAt: {
580 $gte: new Date(year - 1, 11, 31, 23, 59, 59),
581 $lt: new Date(year + 1, 0, 1, 0, 0, 0)
582 }
583 })
584 .toArray()) as HistorySchema[];
pineafan4edb7762022-06-26 19:21:04 +0100585 return entry;
586 }
pineafane23c4ec2022-07-27 21:56:27 +0100587
588 async delete(guild: string) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500589 // console.log("History delete");
pineafane23c4ec2022-07-27 21:56:27 +0100590 await this.histories.deleteMany({ guild: guild });
591 }
pineafan4edb7762022-06-26 19:21:04 +0100592}
593
TheCodedProfb5e9d552023-01-29 15:43:26 -0500594interface ScanCacheSchema {
595 addedAt: Date;
596 hash: string;
Skyler Grey14432712023-03-07 23:40:50 +0000597 nsfw?: boolean;
598 malware?: boolean;
599 bad_link?: boolean;
Skyler Greyd1157312023-03-08 10:07:38 +0000600 tags?: string[];
TheCodedProfb5e9d552023-01-29 15:43:26 -0500601}
602
603export class ScanCache {
604 scanCache: Collection<ScanCacheSchema>;
605
606 constructor() {
607 this.scanCache = database.collection<ScanCacheSchema>("scanCache");
608 }
609
610 async read(hash: string) {
611 return await this.scanCache.findOne({ hash: hash });
612 }
613
Skyler Grey14432712023-03-07 23:40:50 +0000614 async write(hash: string, type: "nsfw" | "malware" | "bad_link", data: boolean, tags?: string[]) {
TheCodedProf21d4b0f2023-04-22 21:02:51 -0400615 // await this.scanCache.insertOne(
616 // { hash: hash, [type]: data, tags: tags ?? [], addedAt: new Date() },
617 // collectionOptions
TheCodedProf3be348c2023-04-22 20:59:14 -0400618 // );
TheCodedProf21d4b0f2023-04-22 21:02:51 -0400619 await this.scanCache.updateOne(
620 { hash: hash },
621 {
622 $set: (() => {
623 switch (type) {
624 case "nsfw": {
625 return { nsfw: data, addedAt: new Date() };
626 }
627 case "malware": {
628 return { malware: data, addedAt: new Date() };
629 }
630 case "bad_link": {
631 return { bad_link: data, tags: tags ?? [], addedAt: new Date() };
632 }
633 default: {
634 throw new Error("Invalid type");
635 }
636 }
637 })()
638 // No you can't just do { [type]: data }, yes it's a typescript error, no I don't know how to fix it
639 // cleanly, yes it would be marginally more elegant, no it's not essential, yes I'd be happy to review
640 // PRs that did improve this snippet
641 // Made an attempt... Gave up... Just Leave It
642 // Counter: 2
643 },
644 Object.assign({ upsert: true }, collectionOptions)
645 );
TheCodedProfb5e9d552023-01-29 15:43:26 -0500646 }
647
648 async cleanup() {
TheCodedProff8ef7942023-03-03 15:32:32 -0500649 // console.log("ScanCache cleanup");
Skyler Greyda16adf2023-03-05 10:22:12 +0000650 await this.scanCache.deleteMany({
651 addedAt: { $lt: new Date(Date.now() - 1000 * 60 * 60 * 24 * 31) },
652 hash: { $not$text: "http" }
653 });
TheCodedProfb5e9d552023-01-29 15:43:26 -0500654 }
655}
656
PineaFan538d3752023-01-12 21:48:23 +0000657export class PerformanceTest {
658 performanceData: Collection<PerformanceDataSchema>;
659
660 constructor() {
661 this.performanceData = database.collection<PerformanceDataSchema>("performance");
662 }
663
664 async record(data: PerformanceDataSchema) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500665 // console.log("PerformanceTest record");
PineaFan538d3752023-01-12 21:48:23 +0000666 data.timestamp = new Date();
TheCodedProffaae5332023-03-01 18:16:05 -0500667 await this.performanceData.insertOne(data, collectionOptions);
PineaFan538d3752023-01-12 21:48:23 +0000668 }
669 async read() {
TheCodedProff8ef7942023-03-03 15:32:32 -0500670 // console.log("PerformanceTest read");
PineaFan538d3752023-01-12 21:48:23 +0000671 return await this.performanceData.find({}).toArray();
672 }
673}
674
675export interface PerformanceDataSchema {
676 timestamp?: Date;
677 discord: number;
678 databaseRead: number;
679 resources: {
680 cpu: number;
681 memory: number;
682 temperature: number;
Skyler Greyda16adf2023-03-05 10:22:12 +0000683 };
PineaFan538d3752023-01-12 21:48:23 +0000684}
685
pineafan4edb7762022-06-26 19:21:04 +0100686export class ModNotes {
687 modNotes: Collection<ModNoteSchema>;
pineafan4edb7762022-06-26 19:21:04 +0100688
pineafan3a02ea32022-08-11 21:35:04 +0100689 constructor() {
pineafan4edb7762022-06-26 19:21:04 +0100690 this.modNotes = database.collection<ModNoteSchema>("modNotes");
pineafan4edb7762022-06-26 19:21:04 +0100691 }
692
TheCodedProfc016f9f2023-04-23 16:01:38 -0400693 async flag(guild: string, user: string, flag: FlagColors | null) {
694 const modNote = await this.modNotes.findOne({ guild: guild, user: user });
695 modNote
696 ? await this.modNotes.updateOne({ guild: guild, user: user }, { $set: { flag: flag } }, collectionOptions)
697 : await this.modNotes.insertOne({ guild: guild, user: user, note: null, flag: flag }, collectionOptions);
698 }
699
pineafan4edb7762022-06-26 19:21:04 +0100700 async create(guild: string, user: string, note: string | null) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500701 // console.log("ModNotes create");
TheCodedProfe49649c2023-04-23 14:31:00 -0400702 const modNote = await this.modNotes.findOne({ guild: guild, user: user });
703 modNote
704 ? await this.modNotes.updateOne({ guild: guild, user: user }, { $set: { note: note } }, collectionOptions)
TheCodedProfc016f9f2023-04-23 16:01:38 -0400705 : await this.modNotes.insertOne({ guild: guild, user: user, note: note, flag: null }, collectionOptions);
pineafan4edb7762022-06-26 19:21:04 +0100706 }
707
708 async read(guild: string, user: string) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500709 // console.log("ModNotes read");
pineafan63fc5e22022-08-04 22:04:10 +0100710 const entry = await this.modNotes.findOne({ guild: guild, user: user });
TheCodedProfc016f9f2023-04-23 16:01:38 -0400711 return entry ?? null;
pineafan4edb7762022-06-26 19:21:04 +0100712 }
TheCodedProf267563a2023-01-21 17:00:57 -0500713
714 async delete(guild: string) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500715 // console.log("ModNotes delete");
TheCodedProf267563a2023-01-21 17:00:57 -0500716 await this.modNotes.deleteMany({ guild: guild });
717 }
pineafan4edb7762022-06-26 19:21:04 +0100718}
719
pineafan73a7c4a2022-07-24 10:38:04 +0100720export class Premium {
721 premium: Collection<PremiumSchema>;
Skyler Greyda16adf2023-03-05 10:22:12 +0000722 cache: Map<string, [boolean, string, number, boolean, Date]>; // Date indicates the time one hour after it was created
723 cacheTimeout = 1000 * 60 * 60; // 1 hour
pineafan4edb7762022-06-26 19:21:04 +0100724
pineafan3a02ea32022-08-11 21:35:04 +0100725 constructor() {
pineafan73a7c4a2022-07-24 10:38:04 +0100726 this.premium = database.collection<PremiumSchema>("premium");
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500727 this.cache = new Map<string, [boolean, string, number, boolean, Date]>();
pineafan4edb7762022-06-26 19:21:04 +0100728 }
729
TheCodedProf633866f2023-02-03 17:06:00 -0500730 async updateUser(user: string, level: number) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500731 // console.log("Premium updateUser");
Skyler Greyda16adf2023-03-05 10:22:12 +0000732 if (!(await this.userExists(user))) await this.createUser(user, level);
TheCodedProf633866f2023-02-03 17:06:00 -0500733 await this.premium.updateOne({ user: user }, { $set: { level: level } }, { upsert: true });
734 }
735
736 async userExists(user: string): Promise<boolean> {
TheCodedProff8ef7942023-03-03 15:32:32 -0500737 // console.log("Premium userExists");
TheCodedProf633866f2023-02-03 17:06:00 -0500738 const entry = await this.premium.findOne({ user: user });
739 return entry ? true : false;
740 }
TheCodedProf633866f2023-02-03 17:06:00 -0500741 async createUser(user: string, level: number) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500742 // console.log("Premium createUser");
TheCodedProffaae5332023-03-01 18:16:05 -0500743 await this.premium.insertOne({ user: user, appliesTo: [], level: level }, collectionOptions);
TheCodedProf633866f2023-02-03 17:06:00 -0500744 }
745
TheCodedProfaa3fe992023-02-25 21:53:09 -0500746 async hasPremium(guild: string): Promise<[boolean, string, number, boolean] | null> {
TheCodedProff8ef7942023-03-03 15:32:32 -0500747 // console.log("Premium hasPremium");
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500748 // [Has premium, user giving premium, level, is mod: if given automatically]
749 const cached = this.cache.get(guild);
750 if (cached && cached[4].getTime() < Date.now()) return [cached[0], cached[1], cached[2], cached[3]];
TheCodedProf94ff6de2023-02-22 17:47:26 -0500751 const entries = await this.premium.find({}).toArray();
Skyler Greyda16adf2023-03-05 10:22:12 +0000752 const members = (await client.guilds.fetch(guild)).members.cache;
753 for (const { user } of entries) {
TheCodedProf94ff6de2023-02-22 17:47:26 -0500754 const member = members.get(user);
Skyler Greyda16adf2023-03-05 10:22:12 +0000755 if (member) {
756 //TODO: Notify user if they've given premium to a server that has since gotten premium via a mod.
TheCodedProf94ff6de2023-02-22 17:47:26 -0500757 const modPerms = //TODO: Create list in config for perms
Skyler Greyda16adf2023-03-05 10:22:12 +0000758 member.permissions.has("Administrator") ||
759 member.permissions.has("ManageChannels") ||
760 member.permissions.has("ManageRoles") ||
761 member.permissions.has("ManageEmojisAndStickers") ||
762 member.permissions.has("ManageWebhooks") ||
763 member.permissions.has("ManageGuild") ||
764 member.permissions.has("KickMembers") ||
765 member.permissions.has("BanMembers") ||
766 member.permissions.has("ManageEvents") ||
767 member.permissions.has("ManageMessages") ||
768 member.permissions.has("ManageThreads");
769 const entry = entries.find((e) => e.user === member.id);
770 if (entry && entry.level === 3 && modPerms) {
771 this.cache.set(guild, [
772 true,
773 member.id,
774 entry.level,
775 true,
776 new Date(Date.now() + this.cacheTimeout)
777 ]);
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500778 return [true, member.id, entry.level, true];
779 }
TheCodedProf94ff6de2023-02-22 17:47:26 -0500780 }
781 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100782 const entry = await this.premium.findOne({
TheCodedProf94ff6de2023-02-22 17:47:26 -0500783 appliesTo: {
784 $elemMatch: {
785 $eq: guild
786 }
787 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100788 });
Skyler Greyda16adf2023-03-05 10:22:12 +0000789 this.cache.set(guild, [
790 entry ? true : false,
791 entry?.user ?? "",
792 entry?.level ?? 0,
793 false,
794 new Date(Date.now() + this.cacheTimeout)
795 ]);
TheCodedProfaa3fe992023-02-25 21:53:09 -0500796 return entry ? [true, entry.user, entry.level, false] : null;
TheCodedProf267563a2023-01-21 17:00:57 -0500797 }
798
TheCodedProf633866f2023-02-03 17:06:00 -0500799 async fetchUser(user: string): Promise<PremiumSchema | null> {
TheCodedProff8ef7942023-03-03 15:32:32 -0500800 // console.log("Premium fetchUser");
TheCodedProf267563a2023-01-21 17:00:57 -0500801 const entry = await this.premium.findOne({ user: user });
TheCodedProf633866f2023-02-03 17:06:00 -0500802 if (!entry) return null;
803 return entry;
804 }
805
TheCodedProf94ff6de2023-02-22 17:47:26 -0500806 async checkAllPremium(member?: GuildMember) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500807 // console.log("Premium checkAllPremium");
TheCodedProf633866f2023-02-03 17:06:00 -0500808 const entries = await this.premium.find({}).toArray();
Skyler Greyda16adf2023-03-05 10:22:12 +0000809 if (member) {
810 const entry = entries.find((e) => e.user === member.id);
811 if (entry) {
TheCodedProf94ff6de2023-02-22 17:47:26 -0500812 const expiresAt = entry.expiresAt;
Skyler Greyda16adf2023-03-05 10:22:12 +0000813 if (expiresAt) expiresAt < Date.now() ? await this.premium.deleteOne({ user: member.id }) : null;
TheCodedProf94ff6de2023-02-22 17:47:26 -0500814 }
815 const roles = member.roles;
816 let level = 0;
817 if (roles.cache.has("1066468879309750313")) {
TheCodedProf633866f2023-02-03 17:06:00 -0500818 level = 99;
TheCodedProf94ff6de2023-02-22 17:47:26 -0500819 } else if (roles.cache.has("1066465491713003520")) {
TheCodedProf633866f2023-02-03 17:06:00 -0500820 level = 1;
TheCodedProf94ff6de2023-02-22 17:47:26 -0500821 } else if (roles.cache.has("1066439526496604194")) {
TheCodedProf633866f2023-02-03 17:06:00 -0500822 level = 2;
TheCodedProf94ff6de2023-02-22 17:47:26 -0500823 } else if (roles.cache.has("1066464134322978912")) {
TheCodedProf633866f2023-02-03 17:06:00 -0500824 level = 3;
825 }
TheCodedProf94ff6de2023-02-22 17:47:26 -0500826 await this.updateUser(member.id, level);
TheCodedProf633866f2023-02-03 17:06:00 -0500827 if (level > 0) {
Skyler Greyda16adf2023-03-05 10:22:12 +0000828 await this.premium.updateOne({ user: member.id }, { $unset: { expiresAt: "" } });
TheCodedProf633866f2023-02-03 17:06:00 -0500829 } else {
Skyler Greyda16adf2023-03-05 10:22:12 +0000830 await this.premium.updateOne(
831 { user: member.id },
832 { $set: { expiresAt: Date.now() + 1000 * 60 * 60 * 24 * 3 } }
833 );
TheCodedProf94ff6de2023-02-22 17:47:26 -0500834 }
835 } else {
Skyler Greyda16adf2023-03-05 10:22:12 +0000836 const members = await (await client.guilds.fetch("684492926528651336")).members.fetch();
837 for (const { roles, id } of members.values()) {
838 const entry = entries.find((e) => e.user === id);
839 if (entry) {
TheCodedProf94ff6de2023-02-22 17:47:26 -0500840 const expiresAt = entry.expiresAt;
Skyler Greyda16adf2023-03-05 10:22:12 +0000841 if (expiresAt) expiresAt < Date.now() ? await this.premium.deleteOne({ user: id }) : null;
TheCodedProf94ff6de2023-02-22 17:47:26 -0500842 }
843 let level: number = 0;
844 if (roles.cache.has("1066468879309750313")) {
845 level = 99;
846 } else if (roles.cache.has("1066465491713003520")) {
847 level = 1;
848 } else if (roles.cache.has("1066439526496604194")) {
849 level = 2;
850 } else if (roles.cache.has("1066464134322978912")) {
851 level = 3;
852 }
853 await this.updateUser(id, level);
854 if (level > 0) {
Skyler Greyda16adf2023-03-05 10:22:12 +0000855 await this.premium.updateOne({ user: id }, { $unset: { expiresAt: "" } });
TheCodedProf94ff6de2023-02-22 17:47:26 -0500856 } else {
Skyler Greyda16adf2023-03-05 10:22:12 +0000857 await this.premium.updateOne(
858 { user: id },
859 { $set: { expiresAt: Date.now() + 1000 * 60 * 60 * 24 * 3 } }
860 );
TheCodedProf94ff6de2023-02-22 17:47:26 -0500861 }
TheCodedProf633866f2023-02-03 17:06:00 -0500862 }
863 }
TheCodedProf267563a2023-01-21 17:00:57 -0500864 }
865
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500866 async addPremium(user: string, guild: string) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500867 // console.log("Premium addPremium");
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500868 const { level } = (await this.fetchUser(user))!;
869 this.cache.set(guild, [true, user, level, false, new Date(Date.now() + this.cacheTimeout)]);
TheCodedProf267563a2023-01-21 17:00:57 -0500870 return this.premium.updateOne({ user: user }, { $addToSet: { appliesTo: guild } }, { upsert: true });
pineafan4edb7762022-06-26 19:21:04 +0100871 }
TheCodedProffc420b72023-01-24 17:14:38 -0500872
TheCodedProf48865eb2023-03-05 15:25:25 -0500873 async removePremium(user: string, guild: string) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500874 // console.log("Premium removePremium");
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500875 this.cache.set(guild, [false, "", 0, false, new Date(Date.now() + this.cacheTimeout)]);
TheCodedProf48865eb2023-03-05 15:25:25 -0500876 return await this.premium.updateOne({ user: user }, { $pull: { appliesTo: guild } });
TheCodedProffc420b72023-01-24 17:14:38 -0500877 }
pineafan4edb7762022-06-26 19:21:04 +0100878}
879
pineafan1e462ab2023-03-07 21:34:06 +0000880// export class Plugins {}
pineafan6de4da52023-03-07 20:43:44 +0000881
pineafan6fb3e072022-05-20 19:27:23 +0100882export interface GuildConfig {
Skyler Grey75ea9172022-08-06 10:22:23 +0100883 id: string;
884 version: number;
PineaFan100df682023-01-02 13:26:08 +0000885 singleEventNotifications: Record<string, boolean>;
pineafan6fb3e072022-05-20 19:27:23 +0100886 filters: {
887 images: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100888 NSFW: boolean;
889 size: boolean;
890 };
891 malware: boolean;
pineafan6fb3e072022-05-20 19:27:23 +0100892 wordFilter: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100893 enabled: boolean;
pineafan6fb3e072022-05-20 19:27:23 +0100894 words: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100895 strict: string[];
896 loose: string[];
897 };
pineafan6fb3e072022-05-20 19:27:23 +0100898 allowed: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100899 users: string[];
900 roles: string[];
901 channels: string[];
902 };
903 };
pineafan6fb3e072022-05-20 19:27:23 +0100904 invite: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100905 enabled: boolean;
PineaFan538d3752023-01-12 21:48:23 +0000906 allowed: {
907 channels: string[];
908 roles: string[];
909 users: string[];
910 };
Skyler Grey75ea9172022-08-06 10:22:23 +0100911 };
pineafan6fb3e072022-05-20 19:27:23 +0100912 pings: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100913 mass: number;
914 everyone: boolean;
915 roles: boolean;
pineafan6fb3e072022-05-20 19:27:23 +0100916 allowed: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100917 roles: string[];
918 rolesToMention: string[];
919 users: string[];
920 channels: string[];
921 };
922 };
TheCodedProfad0b8202023-02-14 14:27:09 -0500923 clean: {
924 channels: string[];
925 allowed: {
TheCodedProff8ef7942023-03-03 15:32:32 -0500926 users: string[];
TheCodedProfad0b8202023-02-14 14:27:09 -0500927 roles: string[];
Skyler Greyda16adf2023-03-05 10:22:12 +0000928 };
929 };
Skyler Grey75ea9172022-08-06 10:22:23 +0100930 };
TheCodedProfbaee2c12023-02-18 16:11:06 -0500931 autoPublish: {
932 enabled: boolean;
933 channels: string[];
Skyler Greyda16adf2023-03-05 10:22:12 +0000934 };
pineafan6fb3e072022-05-20 19:27:23 +0100935 welcome: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100936 enabled: boolean;
Skyler Grey75ea9172022-08-06 10:22:23 +0100937 role: string | null;
938 ping: string | null;
939 channel: string | null;
940 message: string | null;
941 };
942 stats: Record<string, { name: string; enabled: boolean }>;
pineafan6fb3e072022-05-20 19:27:23 +0100943 logging: {
944 logs: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100945 enabled: boolean;
946 channel: string | null;
Skyler Greyad002172022-08-16 18:48:26 +0100947 toLog: string;
Skyler Grey75ea9172022-08-06 10:22:23 +0100948 };
pineafan6fb3e072022-05-20 19:27:23 +0100949 staff: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100950 channel: string | null;
951 };
pineafan73a7c4a2022-07-24 10:38:04 +0100952 attachments: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100953 channel: string | null;
954 saved: Record<string, string>;
955 };
956 };
pineafan6fb3e072022-05-20 19:27:23 +0100957 verify: {
PineaFandf4996f2023-01-01 14:20:06 +0000958 enabled: boolean;
Skyler Grey75ea9172022-08-06 10:22:23 +0100959 role: string | null;
960 };
pineafan6fb3e072022-05-20 19:27:23 +0100961 tickets: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100962 enabled: boolean;
963 category: string | null;
Skyler Greyad002172022-08-16 18:48:26 +0100964 types: string;
965 customTypes: string[] | null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100966 useCustom: boolean;
967 supportRole: string | null;
968 maxTickets: number;
969 };
pineafan6fb3e072022-05-20 19:27:23 +0100970 moderation: {
971 mute: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100972 timeout: boolean;
973 role: string | null;
974 text: string | null;
975 link: string | null;
976 };
pineafan6fb3e072022-05-20 19:27:23 +0100977 kick: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100978 text: string | null;
979 link: string | null;
980 };
pineafan6fb3e072022-05-20 19:27:23 +0100981 ban: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100982 text: string | null;
983 link: string | null;
984 };
pineafan6fb3e072022-05-20 19:27:23 +0100985 softban: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100986 text: string | null;
987 link: string | null;
988 };
pineafan6fb3e072022-05-20 19:27:23 +0100989 warn: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100990 text: string | null;
991 link: string | null;
992 };
pineafan6fb3e072022-05-20 19:27:23 +0100993 role: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100994 role: string | null;
TheCodedProfd9636e82023-01-17 22:13:06 -0500995 text: null;
996 link: null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100997 };
PineaFane6ba7882023-01-18 20:41:16 +0000998 nick: {
999 text: string | null;
1000 link: string | null;
Skyler Greyda16adf2023-03-05 10:22:12 +00001001 };
Skyler Grey75ea9172022-08-06 10:22:23 +01001002 };
pineafan6fb3e072022-05-20 19:27:23 +01001003 tracks: {
Skyler Grey75ea9172022-08-06 10:22:23 +01001004 name: string;
1005 retainPrevious: boolean;
1006 nullable: boolean;
1007 track: string[];
1008 manageableBy: string[];
1009 }[];
pineafan6fb3e072022-05-20 19:27:23 +01001010 roleMenu: {
Skyler Grey75ea9172022-08-06 10:22:23 +01001011 enabled: boolean;
1012 allowWebUI: boolean;
pineafan6fb3e072022-05-20 19:27:23 +01001013 options: {
Skyler Grey75ea9172022-08-06 10:22:23 +01001014 name: string;
1015 description: string;
1016 min: number;
1017 max: number;
pineafan6fb3e072022-05-20 19:27:23 +01001018 options: {
Skyler Grey75ea9172022-08-06 10:22:23 +01001019 name: string;
1020 description: string | null;
1021 role: string;
1022 }[];
1023 }[];
1024 };
1025 tags: Record<string, string>;
pineafan63fc5e22022-08-04 22:04:10 +01001026}
pineafan4edb7762022-06-26 19:21:04 +01001027
1028export interface HistorySchema {
Skyler Grey75ea9172022-08-06 10:22:23 +01001029 type: string;
1030 guild: string;
1031 user: string;
1032 moderator: string | null;
pineafan3a02ea32022-08-11 21:35:04 +01001033 reason: string | null;
Skyler Grey75ea9172022-08-06 10:22:23 +01001034 occurredAt: Date;
1035 before: string | null;
1036 after: string | null;
1037 amount: string | null;
pineafan4edb7762022-06-26 19:21:04 +01001038}
1039
TheCodedProfc016f9f2023-04-23 16:01:38 -04001040export type FlagColors = "red" | "yellow" | "green" | "blue" | "purple" | "gray";
1041
pineafan4edb7762022-06-26 19:21:04 +01001042export interface ModNoteSchema {
Skyler Grey75ea9172022-08-06 10:22:23 +01001043 guild: string;
1044 user: string;
pineafan3a02ea32022-08-11 21:35:04 +01001045 note: string | null;
TheCodedProfc016f9f2023-04-23 16:01:38 -04001046 flag: FlagColors | null;
pineafan4edb7762022-06-26 19:21:04 +01001047}
1048
pineafan73a7c4a2022-07-24 10:38:04 +01001049export interface PremiumSchema {
Skyler Grey75ea9172022-08-06 10:22:23 +01001050 user: string;
1051 level: number;
Skyler Grey75ea9172022-08-06 10:22:23 +01001052 appliesTo: string[];
TheCodedProf633866f2023-02-03 17:06:00 -05001053 expiresAt?: number;
Skyler Grey75ea9172022-08-06 10:22:23 +01001054}