blob: 4fa93c2c9e7c95f3381324824e004d804a6b40e5 [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
TheCodedProfc8054ae2023-06-14 13:55:30 -040024 ? `mongodb://${username}:${password}@${config.mongoOptions.host}/${config.mongoOptions.database}`
25 + `?authMechanism=DEFAULT&authSource=${config.mongoOptions.authSource}`
pineafan6de4da52023-03-07 20:43:44 +000026 : `mongodb://${config.mongoOptions.host}`
Skyler Greyda16adf2023-03-05 10:22:12 +000027);
pineafan63fc5e22022-08-04 22:04:10 +010028await mongoClient.connect();
TheCodedProf3d54ade2023-04-22 21:40:42 -040029export const database = mongoClient.db();
pineafan6fb3e072022-05-20 19:27:23 +010030
TheCodedProf78b90332023-03-04 14:02:21 -050031const collectionOptions = { authdb: config.mongoOptions.authSource, w: "majority" };
TheCodedProf75c51be2023-03-03 17:18:18 -050032const getIV = () => crypto.randomBytes(16);
TheCodedProffaae5332023-03-01 18:16:05 -050033
pineafan4edb7762022-06-26 19:21:04 +010034export class Guilds {
pineafan6fb3e072022-05-20 19:27:23 +010035 guilds: Collection<GuildConfig>;
TheCodedProf8a2d7cd2023-03-05 14:53:59 -050036 oldGuilds: Collection<GuildConfig>;
TheCodedProff8ef7942023-03-03 15:32:32 -050037 defaultData: GuildConfig;
pineafan63fc5e22022-08-04 22:04:10 +010038
39 constructor() {
pineafan4edb7762022-06-26 19:21:04 +010040 this.guilds = database.collection<GuildConfig>("guilds");
TheCodedProff8ef7942023-03-03 15:32:32 -050041 this.defaultData = defaultData;
TheCodedProf8a2d7cd2023-03-05 14:53:59 -050042 this.oldGuilds = database.collection<GuildConfig>("oldGuilds");
43 }
44
45 async readOld(guild: string): Promise<Partial<GuildConfig>> {
46 // console.log("Guild read")
47 const entry = await this.oldGuilds.findOne({ id: guild });
48 return entry ?? {};
pineafan63fc5e22022-08-04 22:04:10 +010049 }
50
TheCodedProfb7a7b992023-03-05 16:11:59 -050051 async updateAllGuilds() {
52 const guilds = await this.guilds.find().toArray();
53 for (const guild of guilds) {
54 let guildObj;
55 try {
56 guildObj = await client.guilds.fetch(guild.id);
57 } catch (e) {
58 guildObj = null;
59 }
Skyler Grey67691762023-03-06 09:58:19 +000060 if (!guildObj) await this.delete(guild.id);
TheCodedProfb7a7b992023-03-05 16:11:59 -050061 }
62 }
63
Skyler Greyad002172022-08-16 18:48:26 +010064 async read(guild: string): Promise<GuildConfig> {
TheCodedProff8ef7942023-03-03 15:32:32 -050065 // console.log("Guild read")
pineafan63fc5e22022-08-04 22:04:10 +010066 const entry = await this.guilds.findOne({ id: guild });
TheCodedProf9f4cf9f2023-03-04 14:18:19 -050067 const data = _.cloneDeep(this.defaultData);
TheCodedProff8ef7942023-03-03 15:32:32 -050068 return _.merge(data, entry ?? {});
pineafan6fb3e072022-05-20 19:27:23 +010069 }
70
Skyler Grey11236ba2022-08-08 21:13:33 +010071 async write(guild: string, set: object | null, unset: string[] | string = []) {
TheCodedProff8ef7942023-03-03 15:32:32 -050072 // console.log("Guild write")
pineafan63fc5e22022-08-04 22:04:10 +010073 // eslint-disable-next-line @typescript-eslint/no-explicit-any
74 const uo: Record<string, any> = {};
75 if (!Array.isArray(unset)) unset = [unset];
76 for (const key of unset) {
pineafan0bc04162022-07-25 17:22:26 +010077 uo[key] = null;
pineafan6702cef2022-06-13 17:52:37 +010078 }
Skyler Grey75ea9172022-08-06 10:22:23 +010079 const out = { $set: {}, $unset: {} };
80 if (set) out.$set = set;
81 if (unset.length) out.$unset = uo;
pineafan0bc04162022-07-25 17:22:26 +010082 await this.guilds.updateOne({ id: guild }, out, { upsert: true });
pineafan6702cef2022-06-13 17:52:37 +010083 }
84
pineafan63fc5e22022-08-04 22:04:10 +010085 // eslint-disable-next-line @typescript-eslint/no-explicit-any
pineafan6702cef2022-06-13 17:52:37 +010086 async append(guild: string, key: string, value: any) {
TheCodedProff8ef7942023-03-03 15:32:32 -050087 // console.log("Guild append")
pineafan6702cef2022-06-13 17:52:37 +010088 if (Array.isArray(value)) {
Skyler Grey75ea9172022-08-06 10:22:23 +010089 await this.guilds.updateOne(
90 { id: guild },
91 {
92 $addToSet: { [key]: { $each: value } }
93 },
94 { upsert: true }
95 );
pineafan6702cef2022-06-13 17:52:37 +010096 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +010097 await this.guilds.updateOne(
98 { id: guild },
99 {
100 $addToSet: { [key]: value }
101 },
102 { upsert: true }
103 );
pineafan6702cef2022-06-13 17:52:37 +0100104 }
105 }
106
Skyler Grey75ea9172022-08-06 10:22:23 +0100107 async remove(
108 guild: string,
109 key: string,
Skyler Greyc634e2b2022-08-06 17:50:48 +0100110 // eslint-disable-next-line @typescript-eslint/no-explicit-any
Skyler Grey75ea9172022-08-06 10:22:23 +0100111 value: any,
112 innerKey?: string | null
113 ) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500114 // console.log("Guild remove")
pineafan02ba0232022-07-24 22:16:15 +0100115 if (innerKey) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100116 await this.guilds.updateOne(
117 { id: guild },
118 {
119 $pull: { [key]: { [innerKey]: { $eq: value } } }
120 },
121 { upsert: true }
122 );
pineafan0bc04162022-07-25 17:22:26 +0100123 } else if (Array.isArray(value)) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100124 await this.guilds.updateOne(
125 { id: guild },
126 {
127 $pullAll: { [key]: value }
128 },
129 { upsert: true }
130 );
pineafan6702cef2022-06-13 17:52:37 +0100131 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100132 await this.guilds.updateOne(
133 { id: guild },
134 {
135 $pullAll: { [key]: [value] }
136 },
137 { upsert: true }
138 );
pineafan6702cef2022-06-13 17:52:37 +0100139 }
pineafan6fb3e072022-05-20 19:27:23 +0100140 }
pineafane23c4ec2022-07-27 21:56:27 +0100141
142 async delete(guild: string) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500143 // console.log("Guild delete")
pineafane23c4ec2022-07-27 21:56:27 +0100144 await this.guilds.deleteOne({ id: guild });
145 }
TheCodedProfa38cbb32023-03-11 17:22:25 -0500146
147 async staffChannels(): Promise<string[]> {
Skyler Grey6a0bab52023-03-15 00:10:26 +0000148 const entries = (
149 await this.guilds
150 .find(
151 { "logging.staff.channel": { $exists: true } },
152 { projection: { "logging.staff.channel": 1, _id: 0 } }
153 )
154 .toArray()
155 ).map((e) => e.logging.staff.channel);
TheCodedProfe4ca5142023-03-14 18:09:03 -0400156 const out: string[] = [];
157 for (const entry of entries) {
158 if (entry) out.push(entry);
159 }
160 return out;
TheCodedProfa38cbb32023-03-11 17:22:25 -0500161 }
pineafan6fb3e072022-05-20 19:27:23 +0100162}
163
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500164interface TranscriptEmbed {
165 title?: string;
166 description?: string;
167 fields?: {
168 name: string;
169 value: string;
170 inline: boolean;
171 }[];
172 footer?: {
173 text: string;
174 iconURL?: string;
175 };
TheCodedProffaae5332023-03-01 18:16:05 -0500176 color?: number;
177 timestamp?: string;
178 author?: {
179 name: string;
180 iconURL?: string;
181 url?: string;
182 };
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500183}
184
185interface TranscriptComponent {
186 type: number;
187 style?: ButtonStyle;
188 label?: string;
189 description?: string;
190 placeholder?: string;
191 emojiURL?: string;
192}
193
194interface TranscriptAuthor {
195 username: string;
Skyler Grey0df04eb2023-05-29 18:40:56 +0200196 discriminator: string | undefined;
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500197 nickname?: string;
198 id: string;
199 iconURL?: string;
200 topRole: {
201 color: number;
202 badgeURL?: string;
TheCodedProf088b1b22023-02-28 17:31:11 -0500203 };
204 bot: boolean;
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500205}
206
207interface TranscriptAttachment {
208 url: string;
209 filename: string;
210 size: number;
211 log?: string;
212}
213
214interface TranscriptMessage {
215 id: string;
216 author: TranscriptAuthor;
217 content?: string;
218 embeds?: TranscriptEmbed[];
219 components?: TranscriptComponent[][];
220 editedTimestamp?: number;
221 createdTimestamp: number;
222 flags?: string[];
223 attachments?: TranscriptAttachment[];
224 stickerURLs?: string[];
Skyler Greyda16adf2023-03-05 10:22:12 +0000225 referencedMessage?: string | [string, string, string]; // the message id, the channel id, the guild id
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500226}
227
228interface TranscriptSchema {
229 code: string;
230 for: TranscriptAuthor;
Skyler Greyda16adf2023-03-05 10:22:12 +0000231 type: "ticket" | "purge";
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500232 guild: string;
233 channel: string;
234 messages: TranscriptMessage[];
235 createdTimestamp: number;
236 createdBy: TranscriptAuthor;
237}
238
Skyler Greyda16adf2023-03-05 10:22:12 +0000239interface findDocSchema {
240 channelID: string;
241 messageID: string;
Skyler Grey5b78b422023-03-07 22:36:20 +0000242 code: string;
Skyler Greyda16adf2023-03-05 10:22:12 +0000243}
TheCodedProf003160f2023-03-04 17:09:40 -0500244
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500245export class Transcript {
246 transcripts: Collection<TranscriptSchema>;
TheCodedProf003160f2023-03-04 17:09:40 -0500247 messageToTranscript: Collection<findDocSchema>;
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500248
249 constructor() {
250 this.transcripts = database.collection<TranscriptSchema>("transcripts");
TheCodedProf003160f2023-03-04 17:09:40 -0500251 this.messageToTranscript = database.collection<findDocSchema>("messageToTranscript");
252 }
253
254 async upload(data: findDocSchema) {
255 // console.log("Transcript upload")
256 await this.messageToTranscript.insertOne(data);
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500257 }
258
259 async create(transcript: Omit<TranscriptSchema, "code">) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500260 // console.log("Transcript create")
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500261 let code;
262 do {
TheCodedProf088b1b22023-02-28 17:31:11 -0500263 code = crypto.randomBytes(64).toString("base64").replace(/=/g, "").replace(/\//g, "_").replace(/\+/g, "-");
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500264 } while (await this.transcripts.findOne({ code: code }));
Skyler Greyda16adf2023-03-05 10:22:12 +0000265 const key = crypto
266 .randomBytes(32 ** 2)
267 .toString("base64")
268 .replace(/=/g, "")
269 .replace(/\//g, "_")
270 .replace(/\+/g, "-")
271 .substring(0, 32);
Skyler Grey67691762023-03-06 09:58:19 +0000272 const iv = getIV()
273 .toString("base64")
274 .substring(0, 16)
275 .replace(/=/g, "")
276 .replace(/\//g, "_")
277 .replace(/\+/g, "-");
Skyler Greyda16adf2023-03-05 10:22:12 +0000278 for (const message of transcript.messages) {
279 if (message.content) {
TheCodedProf75c51be2023-03-03 17:18:18 -0500280 const encCipher = crypto.createCipheriv("AES-256-CBC", key, iv);
281 message.content = encCipher.update(message.content, "utf8", "base64") + encCipher.final("base64");
282 }
283 }
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500284
TheCodedProffaae5332023-03-01 18:16:05 -0500285 const doc = await this.transcripts.insertOne(Object.assign(transcript, { code: code }), collectionOptions);
Skyler Greyda16adf2023-03-05 10:22:12 +0000286 if (doc.acknowledged) {
Skyler Greyf4f21c42023-03-08 14:36:29 +0000287 await client.database.eventScheduler.schedule(
Skyler Greyda16adf2023-03-05 10:22:12 +0000288 "deleteTranscript",
289 (Date.now() + 1000 * 60 * 60 * 24 * 7).toString(),
290 { guild: transcript.guild, code: code, iv: iv, key: key }
291 );
TheCodedProf003160f2023-03-04 17:09:40 -0500292 return [code, key, iv];
Skyler Greyda16adf2023-03-05 10:22:12 +0000293 } else return [null, null, null];
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500294 }
295
TheCodedProf003160f2023-03-04 17:09:40 -0500296 async delete(code: string) {
297 // console.log("Transcript delete")
298 await this.transcripts.deleteOne({ code: code });
TheCodedProf75c51be2023-03-03 17:18:18 -0500299 }
300
301 async deleteAll(guild: string) {
302 // console.log("Transcript delete")
303 const filteredDocs = await this.transcripts.find({ guild: guild }).toArray();
304 for (const doc of filteredDocs) {
305 await this.transcripts.deleteOne({ code: doc.code });
306 }
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500307 }
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500308
TheCodedProf003160f2023-03-04 17:09:40 -0500309 async readEncrypted(code: string) {
310 // console.log("Transcript read")
311 let doc: TranscriptSchema | null = await this.transcripts.findOne({ code: code });
312 let findDoc: findDocSchema | null = null;
pineafan6de4da52023-03-07 20:43:44 +0000313 if (!doc) findDoc = await this.messageToTranscript.findOne({ transcript: code });
Skyler Greyda16adf2023-03-05 10:22:12 +0000314 if (findDoc) {
315 const message = await (
316 client.channels.cache.get(findDoc.channelID) as Discord.TextBasedChannel | null
317 )?.messages.fetch(findDoc.messageID);
318 if (!message) return null;
TheCodedProf003160f2023-03-04 17:09:40 -0500319 const attachment = message.attachments.first();
Skyler Greyda16adf2023-03-05 10:22:12 +0000320 if (!attachment) return null;
TheCodedProf003160f2023-03-04 17:09:40 -0500321 const transcript = (await fetch(attachment.url)).body;
Skyler Greyda16adf2023-03-05 10:22:12 +0000322 if (!transcript) return null;
TheCodedProf003160f2023-03-04 17:09:40 -0500323 const reader = transcript.getReader();
324 let data: Uint8Array | null = null;
325 let allPacketsReceived = false;
326 while (!allPacketsReceived) {
327 const { value, done } = await reader.read();
Skyler Greyda16adf2023-03-05 10:22:12 +0000328 if (done) {
329 allPacketsReceived = true;
330 continue;
331 }
332 if (!data) {
TheCodedProf003160f2023-03-04 17:09:40 -0500333 data = value;
334 } else {
335 data = new Uint8Array(Buffer.concat([data, value]));
336 }
337 }
Skyler Greyda16adf2023-03-05 10:22:12 +0000338 if (!data) return null;
Skyler Greycf771402023-03-05 07:06:37 +0000339 doc = JSON.parse(Buffer.from(data).toString()) as TranscriptSchema;
TheCodedProf003160f2023-03-04 17:09:40 -0500340 }
Skyler Greyda16adf2023-03-05 10:22:12 +0000341 if (!doc) return null;
TheCodedProf003160f2023-03-04 17:09:40 -0500342 return doc;
343 }
344
345 async read(code: string, key: string, iv: string) {
TheCodedProf003160f2023-03-04 17:09:40 -0500346 let doc: TranscriptSchema | null = await this.transcripts.findOne({ code: code });
347 let findDoc: findDocSchema | null = null;
pineafan6de4da52023-03-07 20:43:44 +0000348 if (!doc) findDoc = await this.messageToTranscript.findOne({ transcript: code });
Skyler Greyda16adf2023-03-05 10:22:12 +0000349 if (findDoc) {
350 const message = await (
351 client.channels.cache.get(findDoc.channelID) as Discord.TextBasedChannel | null
352 )?.messages.fetch(findDoc.messageID);
353 if (!message) return null;
TheCodedProf003160f2023-03-04 17:09:40 -0500354 const attachment = message.attachments.first();
Skyler Greyda16adf2023-03-05 10:22:12 +0000355 if (!attachment) return null;
TheCodedProf003160f2023-03-04 17:09:40 -0500356 const transcript = (await fetch(attachment.url)).body;
Skyler Greyda16adf2023-03-05 10:22:12 +0000357 if (!transcript) return null;
TheCodedProf003160f2023-03-04 17:09:40 -0500358 const reader = transcript.getReader();
359 let data: Uint8Array | null = null;
360 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, no-constant-condition
Skyler Greyda16adf2023-03-05 10:22:12 +0000361 while (true) {
TheCodedProf003160f2023-03-04 17:09:40 -0500362 const { value, done } = await reader.read();
363 if (done) break;
Skyler Greyda16adf2023-03-05 10:22:12 +0000364 if (!data) {
TheCodedProf003160f2023-03-04 17:09:40 -0500365 data = value;
366 } else {
367 data = new Uint8Array(Buffer.concat([data, value]));
368 }
369 }
Skyler Greyda16adf2023-03-05 10:22:12 +0000370 if (!data) return null;
Skyler Greycf771402023-03-05 07:06:37 +0000371 doc = JSON.parse(Buffer.from(data).toString()) as TranscriptSchema;
TheCodedProf003160f2023-03-04 17:09:40 -0500372 }
Skyler Greyda16adf2023-03-05 10:22:12 +0000373 if (!doc) return null;
374 for (const message of doc.messages) {
375 if (message.content) {
TheCodedProf003160f2023-03-04 17:09:40 -0500376 const decCipher = crypto.createDecipheriv("AES-256-CBC", key, iv);
377 message.content = decCipher.update(message.content, "base64", "utf8") + decCipher.final("utf8");
378 }
379 }
380 return doc;
381 }
382
Skyler Greyda16adf2023-03-05 10:22:12 +0000383 async createTranscript(
Skyler Greye0c511b2023-03-06 10:30:17 +0000384 type: "ticket" | "purge",
Skyler Greyda16adf2023-03-05 10:22:12 +0000385 messages: Message[],
386 interaction: MessageComponentInteraction | CommandInteraction,
387 member: GuildMember
388 ) {
389 const interactionMember = await interaction.guild?.members.fetch(interaction.user.id);
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500390 const newOut: Omit<TranscriptSchema, "code"> = {
Skyler Greye0c511b2023-03-06 10:30:17 +0000391 type: type,
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500392 for: {
393 username: member!.user.username,
Skyler Grey0df04eb2023-05-29 18:40:56 +0200394 discriminator: member!.user.discriminator === "0" ? undefined : member!.user.discriminator,
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500395 id: member!.user.id,
396 topRole: {
397 color: member!.roles.highest.color
TheCodedProf088b1b22023-02-28 17:31:11 -0500398 },
Skyler Greyda16adf2023-03-05 10:22:12 +0000399 iconURL: member!.user.displayAvatarURL({ forceStatic: true }),
TheCodedProf088b1b22023-02-28 17:31:11 -0500400 bot: member!.user.bot
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500401 },
402 guild: interaction.guild!.id,
403 channel: interaction.channel!.id,
404 messages: [],
405 createdTimestamp: Date.now(),
406 createdBy: {
407 username: interaction.user.username,
Skyler Grey0df04eb2023-05-29 18:40:56 +0200408 discriminator: interaction.user.discriminator === "0" ? undefined : interaction.user.discriminator,
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500409 id: interaction.user.id,
410 topRole: {
411 color: interactionMember?.roles.highest.color ?? 0x000000
TheCodedProf088b1b22023-02-28 17:31:11 -0500412 },
Skyler Greyda16adf2023-03-05 10:22:12 +0000413 iconURL: interaction.user.displayAvatarURL({ forceStatic: true }),
TheCodedProf088b1b22023-02-28 17:31:11 -0500414 bot: interaction.user.bot
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500415 }
Skyler Greyda16adf2023-03-05 10:22:12 +0000416 };
417 if (member.nickname) newOut.for.nickname = member.nickname;
418 if (interactionMember?.roles.icon) newOut.createdBy.topRole.badgeURL = interactionMember.roles.icon.iconURL()!;
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500419 messages.reverse().forEach((message) => {
420 const msg: TranscriptMessage = {
421 id: message.id,
422 author: {
423 username: message.author.username,
Skyler Grey0df04eb2023-05-29 18:40:56 +0200424 discriminator: message.author.discriminator === "0" ? undefined : message.author.discriminator,
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500425 id: message.author.id,
426 topRole: {
Skyler Greya0c70242023-03-06 09:56:21 +0000427 color: message.member ? message.member.roles.highest.color : 0x000000
TheCodedProf088b1b22023-02-28 17:31:11 -0500428 },
TheCodedProfe92b9b52023-03-06 17:07:34 -0500429 iconURL: (message.member?.user ?? message.author).displayAvatarURL({ forceStatic: true }),
Skyler Grey67691762023-03-06 09:58:19 +0000430 bot: message.author.bot || false
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500431 },
432 createdTimestamp: message.createdTimestamp
433 };
Skyler Greyda16adf2023-03-05 10:22:12 +0000434 if (message.member?.nickname) msg.author.nickname = message.member.nickname;
Skyler Greya0c70242023-03-06 09:56:21 +0000435 if (message.member?.roles.icon) msg.author.topRole.badgeURL = message.member!.roles.icon.iconURL()!;
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500436 if (message.content) msg.content = message.content;
Skyler Greyda16adf2023-03-05 10:22:12 +0000437 if (message.embeds.length > 0)
438 msg.embeds = message.embeds.map((embed) => {
439 const obj: TranscriptEmbed = {};
440 if (embed.title) obj.title = embed.title;
441 if (embed.description) obj.description = embed.description;
442 if (embed.fields.length > 0)
443 obj.fields = embed.fields.map((field) => {
444 return {
445 name: field.name,
446 value: field.value,
447 inline: field.inline ?? false
448 };
449 });
450 if (embed.color) obj.color = embed.color;
451 if (embed.timestamp) obj.timestamp = embed.timestamp;
452 if (embed.footer)
453 obj.footer = {
454 text: embed.footer.text
455 };
456 if (embed.footer?.iconURL) obj.footer!.iconURL = embed.footer.iconURL;
457 if (embed.author)
458 obj.author = {
459 name: embed.author.name
460 };
461 if (embed.author?.iconURL) obj.author!.iconURL = embed.author.iconURL;
462 if (embed.author?.url) obj.author!.url = embed.author.url;
463 return obj;
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500464 });
Skyler Greyda16adf2023-03-05 10:22:12 +0000465 if (message.components.length > 0)
466 msg.components = message.components.map((component) =>
467 component.components.map((child) => {
468 const obj: TranscriptComponent = {
469 type: child.type
470 };
471 if (child.type === ComponentType.Button) {
472 obj.style = child.style;
473 obj.label = child.label ?? "";
Skyler Greyf96a3372023-06-14 19:08:46 +0200474 } else if (child.type > 2) {
475 // FIXME: Can we write this more clearly to make it obvious what we mean by 2 here?
Skyler Greyda16adf2023-03-05 10:22:12 +0000476 obj.placeholder = child.placeholder ?? "";
477 }
478 return obj;
479 })
480 );
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500481 if (message.editedTimestamp) msg.editedTimestamp = message.editedTimestamp;
482 msg.flags = message.flags.toArray();
483
Skyler Greyda16adf2023-03-05 10:22:12 +0000484 if (message.stickers.size > 0) msg.stickerURLs = message.stickers.map((sticker) => sticker.url);
485 if (message.reference)
486 msg.referencedMessage = [
487 message.reference.guildId ?? "",
488 message.reference.channelId,
489 message.reference.messageId ?? ""
490 ];
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500491 newOut.messages.push(msg);
492 });
493 return newOut;
494 }
495
496 toHumanReadable(transcript: Omit<TranscriptSchema, "code">): string {
497 let out = "";
498 for (const message of transcript.messages) {
499 if (message.referencedMessage) {
500 if (Array.isArray(message.referencedMessage)) {
501 out += `> [Crosspost From] ${message.referencedMessage[0]} in ${message.referencedMessage[1]} in ${message.referencedMessage[2]}\n`;
Skyler Greyda16adf2023-03-05 10:22:12 +0000502 } else out += `> [Reply To] ${message.referencedMessage}\n`;
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500503 }
pineafancfb149f2023-05-28 15:46:30 +0100504 out += `${message.author.nickname ?? message.author.username} (${message.author.id}) (${message.id})`;
TheCodedProff8ef7942023-03-03 15:32:32 -0500505 out += ` [${new Date(message.createdTimestamp).toISOString()}]`;
506 if (message.editedTimestamp) out += ` [Edited: ${new Date(message.editedTimestamp).toISOString()}]`;
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500507 out += "\n";
508 if (message.content) out += `[Content]\n${message.content}\n\n`;
509 if (message.embeds) {
510 for (const embed of message.embeds) {
511 out += `[Embed]\n`;
512 if (embed.title) out += `| Title: ${embed.title}\n`;
513 if (embed.description) out += `| Description: ${embed.description}\n`;
514 if (embed.fields) {
515 for (const field of embed.fields) {
516 out += `| Field: ${field.name} - ${field.value}\n`;
517 }
518 }
519 if (embed.footer) {
520 out += `|Footer: ${embed.footer.text}\n`;
521 }
522 out += "\n";
523 }
524 }
525 if (message.components) {
526 for (const component of message.components) {
527 out += `[Component]\n`;
528 for (const button of component) {
529 out += `| Button: ${button.label ?? button.description}\n`;
530 }
531 out += "\n";
532 }
533 }
534 if (message.attachments) {
535 for (const attachment of message.attachments) {
536 out += `[Attachment] ${attachment.filename} (${attachment.size} bytes) ${attachment.url}\n`;
537 }
538 }
Skyler Greyda16adf2023-03-05 10:22:12 +0000539 out += "\n\n";
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500540 }
Skyler Greyda16adf2023-03-05 10:22:12 +0000541 return out;
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500542 }
TheCodedProfcfe8e9a2023-02-26 17:28:09 -0500543}
544
pineafan4edb7762022-06-26 19:21:04 +0100545export class History {
546 histories: Collection<HistorySchema>;
pineafan4edb7762022-06-26 19:21:04 +0100547
pineafan3a02ea32022-08-11 21:35:04 +0100548 constructor() {
pineafan4edb7762022-06-26 19:21:04 +0100549 this.histories = database.collection<HistorySchema>("history");
pineafan4edb7762022-06-26 19:21:04 +0100550 }
551
Skyler Grey75ea9172022-08-06 10:22:23 +0100552 async create(
553 type: string,
554 guild: string,
555 user: Discord.User,
556 moderator: Discord.User | null,
557 reason: string | null,
pineafan3a02ea32022-08-11 21:35:04 +0100558 before?: string | null,
559 after?: string | null,
560 amount?: string | null
Skyler Grey75ea9172022-08-06 10:22:23 +0100561 ) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500562 // console.log("History create");
Skyler Greyda16adf2023-03-05 10:22:12 +0000563 await this.histories.insertOne(
564 {
565 type: type,
566 guild: guild,
567 user: user.id,
568 moderator: moderator ? moderator.id : null,
569 reason: reason,
570 occurredAt: new Date(),
571 before: before ?? null,
572 after: after ?? null,
573 amount: amount ?? null
574 },
575 collectionOptions
576 );
pineafan4edb7762022-06-26 19:21:04 +0100577 }
578
579 async read(guild: string, user: string, year: number) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500580 // console.log("History read");
Skyler Grey75ea9172022-08-06 10:22:23 +0100581 const entry = (await this.histories
582 .find({
583 guild: guild,
584 user: user,
585 occurredAt: {
586 $gte: new Date(year - 1, 11, 31, 23, 59, 59),
587 $lt: new Date(year + 1, 0, 1, 0, 0, 0)
588 }
589 })
590 .toArray()) as HistorySchema[];
pineafan4edb7762022-06-26 19:21:04 +0100591 return entry;
592 }
pineafane23c4ec2022-07-27 21:56:27 +0100593
594 async delete(guild: string) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500595 // console.log("History delete");
pineafane23c4ec2022-07-27 21:56:27 +0100596 await this.histories.deleteMany({ guild: guild });
597 }
pineafan4edb7762022-06-26 19:21:04 +0100598}
599
TheCodedProfb5e9d552023-01-29 15:43:26 -0500600interface ScanCacheSchema {
601 addedAt: Date;
602 hash: string;
Skyler Grey14432712023-03-07 23:40:50 +0000603 nsfw?: boolean;
604 malware?: boolean;
605 bad_link?: boolean;
Skyler Greyd1157312023-03-08 10:07:38 +0000606 tags?: string[];
TheCodedProfb5e9d552023-01-29 15:43:26 -0500607}
608
609export class ScanCache {
610 scanCache: Collection<ScanCacheSchema>;
611
612 constructor() {
613 this.scanCache = database.collection<ScanCacheSchema>("scanCache");
614 }
615
616 async read(hash: string) {
617 return await this.scanCache.findOne({ hash: hash });
618 }
619
Skyler Grey14432712023-03-07 23:40:50 +0000620 async write(hash: string, type: "nsfw" | "malware" | "bad_link", data: boolean, tags?: string[]) {
TheCodedProf21d4b0f2023-04-22 21:02:51 -0400621 // await this.scanCache.insertOne(
622 // { hash: hash, [type]: data, tags: tags ?? [], addedAt: new Date() },
623 // collectionOptions
TheCodedProf3be348c2023-04-22 20:59:14 -0400624 // );
TheCodedProf21d4b0f2023-04-22 21:02:51 -0400625 await this.scanCache.updateOne(
626 { hash: hash },
627 {
628 $set: (() => {
629 switch (type) {
630 case "nsfw": {
631 return { nsfw: data, addedAt: new Date() };
632 }
633 case "malware": {
634 return { malware: data, addedAt: new Date() };
635 }
636 case "bad_link": {
637 return { bad_link: data, tags: tags ?? [], addedAt: new Date() };
638 }
639 default: {
640 throw new Error("Invalid type");
641 }
642 }
643 })()
644 // No you can't just do { [type]: data }, yes it's a typescript error, no I don't know how to fix it
645 // cleanly, yes it would be marginally more elegant, no it's not essential, yes I'd be happy to review
646 // PRs that did improve this snippet
647 // Made an attempt... Gave up... Just Leave It
648 // Counter: 2
649 },
650 Object.assign({ upsert: true }, collectionOptions)
651 );
TheCodedProfb5e9d552023-01-29 15:43:26 -0500652 }
653
654 async cleanup() {
TheCodedProff8ef7942023-03-03 15:32:32 -0500655 // console.log("ScanCache cleanup");
Skyler Greyda16adf2023-03-05 10:22:12 +0000656 await this.scanCache.deleteMany({
657 addedAt: { $lt: new Date(Date.now() - 1000 * 60 * 60 * 24 * 31) },
658 hash: { $not$text: "http" }
659 });
TheCodedProfb5e9d552023-01-29 15:43:26 -0500660 }
661}
662
PineaFan538d3752023-01-12 21:48:23 +0000663export class PerformanceTest {
664 performanceData: Collection<PerformanceDataSchema>;
665
666 constructor() {
667 this.performanceData = database.collection<PerformanceDataSchema>("performance");
668 }
669
670 async record(data: PerformanceDataSchema) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500671 // console.log("PerformanceTest record");
PineaFan538d3752023-01-12 21:48:23 +0000672 data.timestamp = new Date();
TheCodedProffaae5332023-03-01 18:16:05 -0500673 await this.performanceData.insertOne(data, collectionOptions);
PineaFan538d3752023-01-12 21:48:23 +0000674 }
675 async read() {
TheCodedProff8ef7942023-03-03 15:32:32 -0500676 // console.log("PerformanceTest read");
PineaFan538d3752023-01-12 21:48:23 +0000677 return await this.performanceData.find({}).toArray();
678 }
679}
680
681export interface PerformanceDataSchema {
682 timestamp?: Date;
683 discord: number;
684 databaseRead: number;
685 resources: {
686 cpu: number;
687 memory: number;
688 temperature: number;
Skyler Greyda16adf2023-03-05 10:22:12 +0000689 };
PineaFan538d3752023-01-12 21:48:23 +0000690}
691
pineafan4edb7762022-06-26 19:21:04 +0100692export class ModNotes {
693 modNotes: Collection<ModNoteSchema>;
pineafan4edb7762022-06-26 19:21:04 +0100694
pineafan3a02ea32022-08-11 21:35:04 +0100695 constructor() {
pineafan4edb7762022-06-26 19:21:04 +0100696 this.modNotes = database.collection<ModNoteSchema>("modNotes");
pineafan4edb7762022-06-26 19:21:04 +0100697 }
698
TheCodedProfc016f9f2023-04-23 16:01:38 -0400699 async flag(guild: string, user: string, flag: FlagColors | null) {
700 const modNote = await this.modNotes.findOne({ guild: guild, user: user });
701 modNote
702 ? await this.modNotes.updateOne({ guild: guild, user: user }, { $set: { flag: flag } }, collectionOptions)
703 : await this.modNotes.insertOne({ guild: guild, user: user, note: null, flag: flag }, collectionOptions);
704 }
705
pineafan4edb7762022-06-26 19:21:04 +0100706 async create(guild: string, user: string, note: string | null) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500707 // console.log("ModNotes create");
TheCodedProfe49649c2023-04-23 14:31:00 -0400708 const modNote = await this.modNotes.findOne({ guild: guild, user: user });
709 modNote
710 ? await this.modNotes.updateOne({ guild: guild, user: user }, { $set: { note: note } }, collectionOptions)
TheCodedProfc016f9f2023-04-23 16:01:38 -0400711 : await this.modNotes.insertOne({ guild: guild, user: user, note: note, flag: null }, collectionOptions);
pineafan4edb7762022-06-26 19:21:04 +0100712 }
713
714 async read(guild: string, user: string) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500715 // console.log("ModNotes read");
pineafan63fc5e22022-08-04 22:04:10 +0100716 const entry = await this.modNotes.findOne({ guild: guild, user: user });
TheCodedProfc016f9f2023-04-23 16:01:38 -0400717 return entry ?? null;
pineafan4edb7762022-06-26 19:21:04 +0100718 }
TheCodedProf267563a2023-01-21 17:00:57 -0500719
720 async delete(guild: string) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500721 // console.log("ModNotes delete");
TheCodedProf267563a2023-01-21 17:00:57 -0500722 await this.modNotes.deleteMany({ guild: guild });
723 }
pineafan4edb7762022-06-26 19:21:04 +0100724}
725
pineafan73a7c4a2022-07-24 10:38:04 +0100726export class Premium {
727 premium: Collection<PremiumSchema>;
Skyler Greyda16adf2023-03-05 10:22:12 +0000728 cache: Map<string, [boolean, string, number, boolean, Date]>; // Date indicates the time one hour after it was created
729 cacheTimeout = 1000 * 60 * 60; // 1 hour
pineafan4edb7762022-06-26 19:21:04 +0100730
pineafan3a02ea32022-08-11 21:35:04 +0100731 constructor() {
pineafan73a7c4a2022-07-24 10:38:04 +0100732 this.premium = database.collection<PremiumSchema>("premium");
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500733 this.cache = new Map<string, [boolean, string, number, boolean, Date]>();
pineafan4edb7762022-06-26 19:21:04 +0100734 }
735
TheCodedProf633866f2023-02-03 17:06:00 -0500736 async updateUser(user: string, level: number) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500737 // console.log("Premium updateUser");
Skyler Greyda16adf2023-03-05 10:22:12 +0000738 if (!(await this.userExists(user))) await this.createUser(user, level);
TheCodedProf633866f2023-02-03 17:06:00 -0500739 await this.premium.updateOne({ user: user }, { $set: { level: level } }, { upsert: true });
740 }
741
742 async userExists(user: string): Promise<boolean> {
TheCodedProff8ef7942023-03-03 15:32:32 -0500743 // console.log("Premium userExists");
TheCodedProf633866f2023-02-03 17:06:00 -0500744 const entry = await this.premium.findOne({ user: user });
745 return entry ? true : false;
746 }
TheCodedProf633866f2023-02-03 17:06:00 -0500747 async createUser(user: string, level: number) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500748 // console.log("Premium createUser");
TheCodedProffaae5332023-03-01 18:16:05 -0500749 await this.premium.insertOne({ user: user, appliesTo: [], level: level }, collectionOptions);
TheCodedProf633866f2023-02-03 17:06:00 -0500750 }
751
TheCodedProfaa3fe992023-02-25 21:53:09 -0500752 async hasPremium(guild: string): Promise<[boolean, string, number, boolean] | null> {
TheCodedProff8ef7942023-03-03 15:32:32 -0500753 // console.log("Premium hasPremium");
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500754 // [Has premium, user giving premium, level, is mod: if given automatically]
755 const cached = this.cache.get(guild);
756 if (cached && cached[4].getTime() < Date.now()) return [cached[0], cached[1], cached[2], cached[3]];
TheCodedProf94ff6de2023-02-22 17:47:26 -0500757 const entries = await this.premium.find({}).toArray();
Skyler Greyda16adf2023-03-05 10:22:12 +0000758 const members = (await client.guilds.fetch(guild)).members.cache;
759 for (const { user } of entries) {
TheCodedProf94ff6de2023-02-22 17:47:26 -0500760 const member = members.get(user);
Skyler Greyda16adf2023-03-05 10:22:12 +0000761 if (member) {
762 //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 -0500763 const modPerms = //TODO: Create list in config for perms
Skyler Greyda16adf2023-03-05 10:22:12 +0000764 member.permissions.has("Administrator") ||
765 member.permissions.has("ManageChannels") ||
766 member.permissions.has("ManageRoles") ||
767 member.permissions.has("ManageEmojisAndStickers") ||
768 member.permissions.has("ManageWebhooks") ||
769 member.permissions.has("ManageGuild") ||
770 member.permissions.has("KickMembers") ||
771 member.permissions.has("BanMembers") ||
772 member.permissions.has("ManageEvents") ||
773 member.permissions.has("ManageMessages") ||
774 member.permissions.has("ManageThreads");
775 const entry = entries.find((e) => e.user === member.id);
776 if (entry && entry.level === 3 && modPerms) {
777 this.cache.set(guild, [
778 true,
779 member.id,
780 entry.level,
781 true,
782 new Date(Date.now() + this.cacheTimeout)
783 ]);
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500784 return [true, member.id, entry.level, true];
785 }
TheCodedProf94ff6de2023-02-22 17:47:26 -0500786 }
787 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100788 const entry = await this.premium.findOne({
TheCodedProf94ff6de2023-02-22 17:47:26 -0500789 appliesTo: {
790 $elemMatch: {
791 $eq: guild
792 }
793 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100794 });
Skyler Greyda16adf2023-03-05 10:22:12 +0000795 this.cache.set(guild, [
796 entry ? true : false,
797 entry?.user ?? "",
798 entry?.level ?? 0,
799 false,
800 new Date(Date.now() + this.cacheTimeout)
801 ]);
TheCodedProfaa3fe992023-02-25 21:53:09 -0500802 return entry ? [true, entry.user, entry.level, false] : null;
TheCodedProf267563a2023-01-21 17:00:57 -0500803 }
804
TheCodedProf633866f2023-02-03 17:06:00 -0500805 async fetchUser(user: string): Promise<PremiumSchema | null> {
TheCodedProff8ef7942023-03-03 15:32:32 -0500806 // console.log("Premium fetchUser");
TheCodedProf267563a2023-01-21 17:00:57 -0500807 const entry = await this.premium.findOne({ user: user });
TheCodedProf633866f2023-02-03 17:06:00 -0500808 if (!entry) return null;
809 return entry;
810 }
811
TheCodedProf94ff6de2023-02-22 17:47:26 -0500812 async checkAllPremium(member?: GuildMember) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500813 // console.log("Premium checkAllPremium");
TheCodedProf633866f2023-02-03 17:06:00 -0500814 const entries = await this.premium.find({}).toArray();
Skyler Greyda16adf2023-03-05 10:22:12 +0000815 if (member) {
816 const entry = entries.find((e) => e.user === member.id);
817 if (entry) {
TheCodedProf94ff6de2023-02-22 17:47:26 -0500818 const expiresAt = entry.expiresAt;
Skyler Greyda16adf2023-03-05 10:22:12 +0000819 if (expiresAt) expiresAt < Date.now() ? await this.premium.deleteOne({ user: member.id }) : null;
TheCodedProf94ff6de2023-02-22 17:47:26 -0500820 }
821 const roles = member.roles;
822 let level = 0;
823 if (roles.cache.has("1066468879309750313")) {
TheCodedProf633866f2023-02-03 17:06:00 -0500824 level = 99;
TheCodedProf94ff6de2023-02-22 17:47:26 -0500825 } else if (roles.cache.has("1066465491713003520")) {
TheCodedProf633866f2023-02-03 17:06:00 -0500826 level = 1;
TheCodedProf94ff6de2023-02-22 17:47:26 -0500827 } else if (roles.cache.has("1066439526496604194")) {
TheCodedProf633866f2023-02-03 17:06:00 -0500828 level = 2;
TheCodedProf94ff6de2023-02-22 17:47:26 -0500829 } else if (roles.cache.has("1066464134322978912")) {
TheCodedProf633866f2023-02-03 17:06:00 -0500830 level = 3;
831 }
TheCodedProf94ff6de2023-02-22 17:47:26 -0500832 await this.updateUser(member.id, level);
TheCodedProf633866f2023-02-03 17:06:00 -0500833 if (level > 0) {
Skyler Greyda16adf2023-03-05 10:22:12 +0000834 await this.premium.updateOne({ user: member.id }, { $unset: { expiresAt: "" } });
TheCodedProf633866f2023-02-03 17:06:00 -0500835 } else {
Skyler Greyda16adf2023-03-05 10:22:12 +0000836 await this.premium.updateOne(
837 { user: member.id },
838 { $set: { expiresAt: Date.now() + 1000 * 60 * 60 * 24 * 3 } }
839 );
TheCodedProf94ff6de2023-02-22 17:47:26 -0500840 }
841 } else {
Skyler Greyda16adf2023-03-05 10:22:12 +0000842 const members = await (await client.guilds.fetch("684492926528651336")).members.fetch();
843 for (const { roles, id } of members.values()) {
844 const entry = entries.find((e) => e.user === id);
845 if (entry) {
TheCodedProf94ff6de2023-02-22 17:47:26 -0500846 const expiresAt = entry.expiresAt;
Skyler Greyda16adf2023-03-05 10:22:12 +0000847 if (expiresAt) expiresAt < Date.now() ? await this.premium.deleteOne({ user: id }) : null;
TheCodedProf94ff6de2023-02-22 17:47:26 -0500848 }
849 let level: number = 0;
850 if (roles.cache.has("1066468879309750313")) {
851 level = 99;
852 } else if (roles.cache.has("1066465491713003520")) {
853 level = 1;
854 } else if (roles.cache.has("1066439526496604194")) {
855 level = 2;
856 } else if (roles.cache.has("1066464134322978912")) {
857 level = 3;
858 }
859 await this.updateUser(id, level);
860 if (level > 0) {
Skyler Greyda16adf2023-03-05 10:22:12 +0000861 await this.premium.updateOne({ user: id }, { $unset: { expiresAt: "" } });
TheCodedProf94ff6de2023-02-22 17:47:26 -0500862 } else {
Skyler Greyda16adf2023-03-05 10:22:12 +0000863 await this.premium.updateOne(
864 { user: id },
865 { $set: { expiresAt: Date.now() + 1000 * 60 * 60 * 24 * 3 } }
866 );
TheCodedProf94ff6de2023-02-22 17:47:26 -0500867 }
TheCodedProf633866f2023-02-03 17:06:00 -0500868 }
869 }
TheCodedProf267563a2023-01-21 17:00:57 -0500870 }
871
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500872 async addPremium(user: string, guild: string) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500873 // console.log("Premium addPremium");
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500874 const { level } = (await this.fetchUser(user))!;
875 this.cache.set(guild, [true, user, level, false, new Date(Date.now() + this.cacheTimeout)]);
TheCodedProf267563a2023-01-21 17:00:57 -0500876 return this.premium.updateOne({ user: user }, { $addToSet: { appliesTo: guild } }, { upsert: true });
pineafan4edb7762022-06-26 19:21:04 +0100877 }
TheCodedProffc420b72023-01-24 17:14:38 -0500878
TheCodedProf48865eb2023-03-05 15:25:25 -0500879 async removePremium(user: string, guild: string) {
TheCodedProff8ef7942023-03-03 15:32:32 -0500880 // console.log("Premium removePremium");
TheCodedProf9c51a7e2023-02-27 17:11:13 -0500881 this.cache.set(guild, [false, "", 0, false, new Date(Date.now() + this.cacheTimeout)]);
TheCodedProf48865eb2023-03-05 15:25:25 -0500882 return await this.premium.updateOne({ user: user }, { $pull: { appliesTo: guild } });
TheCodedProffc420b72023-01-24 17:14:38 -0500883 }
pineafan4edb7762022-06-26 19:21:04 +0100884}
885
pineafan1e462ab2023-03-07 21:34:06 +0000886// export class Plugins {}
pineafan6de4da52023-03-07 20:43:44 +0000887
pineafan6fb3e072022-05-20 19:27:23 +0100888export interface GuildConfig {
Skyler Grey75ea9172022-08-06 10:22:23 +0100889 id: string;
890 version: number;
PineaFan100df682023-01-02 13:26:08 +0000891 singleEventNotifications: Record<string, boolean>;
pineafan6fb3e072022-05-20 19:27:23 +0100892 filters: {
893 images: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100894 NSFW: boolean;
895 size: boolean;
896 };
897 malware: boolean;
pineafan6fb3e072022-05-20 19:27:23 +0100898 wordFilter: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100899 enabled: boolean;
pineafan6fb3e072022-05-20 19:27:23 +0100900 words: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100901 strict: string[];
902 loose: string[];
903 };
pineafan6fb3e072022-05-20 19:27:23 +0100904 allowed: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100905 users: string[];
906 roles: string[];
907 channels: string[];
908 };
909 };
pineafan6fb3e072022-05-20 19:27:23 +0100910 invite: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100911 enabled: boolean;
PineaFan538d3752023-01-12 21:48:23 +0000912 allowed: {
913 channels: string[];
914 roles: string[];
915 users: string[];
916 };
Skyler Grey75ea9172022-08-06 10:22:23 +0100917 };
pineafan6fb3e072022-05-20 19:27:23 +0100918 pings: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100919 mass: number;
920 everyone: boolean;
921 roles: boolean;
pineafan6fb3e072022-05-20 19:27:23 +0100922 allowed: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100923 roles: string[];
924 rolesToMention: string[];
925 users: string[];
926 channels: string[];
927 };
928 };
TheCodedProfad0b8202023-02-14 14:27:09 -0500929 clean: {
930 channels: string[];
931 allowed: {
TheCodedProff8ef7942023-03-03 15:32:32 -0500932 users: string[];
TheCodedProfad0b8202023-02-14 14:27:09 -0500933 roles: string[];
Skyler Greyda16adf2023-03-05 10:22:12 +0000934 };
935 };
Skyler Grey75ea9172022-08-06 10:22:23 +0100936 };
TheCodedProfbaee2c12023-02-18 16:11:06 -0500937 autoPublish: {
938 enabled: boolean;
939 channels: string[];
Skyler Greyda16adf2023-03-05 10:22:12 +0000940 };
pineafan6fb3e072022-05-20 19:27:23 +0100941 welcome: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100942 enabled: boolean;
Skyler Grey75ea9172022-08-06 10:22:23 +0100943 role: string | null;
944 ping: string | null;
945 channel: string | null;
946 message: string | null;
947 };
948 stats: Record<string, { name: string; enabled: boolean }>;
pineafan6fb3e072022-05-20 19:27:23 +0100949 logging: {
950 logs: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100951 enabled: boolean;
952 channel: string | null;
Skyler Greyad002172022-08-16 18:48:26 +0100953 toLog: string;
Skyler Grey75ea9172022-08-06 10:22:23 +0100954 };
pineafan6fb3e072022-05-20 19:27:23 +0100955 staff: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100956 channel: string | null;
957 };
pineafan73a7c4a2022-07-24 10:38:04 +0100958 attachments: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100959 channel: string | null;
960 saved: Record<string, string>;
961 };
962 };
pineafan6fb3e072022-05-20 19:27:23 +0100963 verify: {
PineaFandf4996f2023-01-01 14:20:06 +0000964 enabled: boolean;
Skyler Grey75ea9172022-08-06 10:22:23 +0100965 role: string | null;
966 };
pineafan6fb3e072022-05-20 19:27:23 +0100967 tickets: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100968 enabled: boolean;
969 category: string | null;
Skyler Greyad002172022-08-16 18:48:26 +0100970 types: string;
971 customTypes: string[] | null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100972 useCustom: boolean;
973 supportRole: string | null;
974 maxTickets: number;
975 };
pineafan6fb3e072022-05-20 19:27:23 +0100976 moderation: {
977 mute: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100978 timeout: boolean;
979 role: string | null;
980 text: string | null;
981 link: string | null;
982 };
pineafan6fb3e072022-05-20 19:27:23 +0100983 kick: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100984 text: string | null;
985 link: string | null;
986 };
pineafan6fb3e072022-05-20 19:27:23 +0100987 ban: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100988 text: string | null;
989 link: string | null;
990 };
pineafan6fb3e072022-05-20 19:27:23 +0100991 softban: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100992 text: string | null;
993 link: string | null;
994 };
pineafan6fb3e072022-05-20 19:27:23 +0100995 warn: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100996 text: string | null;
997 link: string | null;
998 };
pineafan6fb3e072022-05-20 19:27:23 +0100999 role: {
Skyler Grey75ea9172022-08-06 10:22:23 +01001000 role: string | null;
TheCodedProfd9636e82023-01-17 22:13:06 -05001001 text: null;
1002 link: null;
Skyler Grey75ea9172022-08-06 10:22:23 +01001003 };
PineaFane6ba7882023-01-18 20:41:16 +00001004 nick: {
1005 text: string | null;
1006 link: string | null;
Skyler Greyda16adf2023-03-05 10:22:12 +00001007 };
Skyler Grey75ea9172022-08-06 10:22:23 +01001008 };
pineafan6fb3e072022-05-20 19:27:23 +01001009 tracks: {
Skyler Grey75ea9172022-08-06 10:22:23 +01001010 name: string;
1011 retainPrevious: boolean;
1012 nullable: boolean;
1013 track: string[];
1014 manageableBy: string[];
1015 }[];
pineafan6fb3e072022-05-20 19:27:23 +01001016 roleMenu: {
Skyler Grey75ea9172022-08-06 10:22:23 +01001017 enabled: boolean;
1018 allowWebUI: boolean;
pineafan6fb3e072022-05-20 19:27:23 +01001019 options: {
TheCodedProf4a088b12023-06-06 15:29:59 -04001020 enabled?: boolean;
Skyler Grey75ea9172022-08-06 10:22:23 +01001021 name: string;
TheCodedProf4a088b12023-06-06 15:29:59 -04001022 description?: string;
Skyler Grey75ea9172022-08-06 10:22:23 +01001023 min: number;
1024 max: number;
pineafan6fb3e072022-05-20 19:27:23 +01001025 options: {
Skyler Grey75ea9172022-08-06 10:22:23 +01001026 name: string;
1027 description: string | null;
1028 role: string;
1029 }[];
1030 }[];
1031 };
1032 tags: Record<string, string>;
pineafan63fc5e22022-08-04 22:04:10 +01001033}
pineafan4edb7762022-06-26 19:21:04 +01001034
1035export interface HistorySchema {
Skyler Grey75ea9172022-08-06 10:22:23 +01001036 type: string;
1037 guild: string;
1038 user: string;
1039 moderator: string | null;
pineafan3a02ea32022-08-11 21:35:04 +01001040 reason: string | null;
Skyler Grey75ea9172022-08-06 10:22:23 +01001041 occurredAt: Date;
1042 before: string | null;
1043 after: string | null;
1044 amount: string | null;
pineafan4edb7762022-06-26 19:21:04 +01001045}
1046
TheCodedProfc016f9f2023-04-23 16:01:38 -04001047export type FlagColors = "red" | "yellow" | "green" | "blue" | "purple" | "gray";
1048
pineafan4edb7762022-06-26 19:21:04 +01001049export interface ModNoteSchema {
Skyler Grey75ea9172022-08-06 10:22:23 +01001050 guild: string;
1051 user: string;
pineafan3a02ea32022-08-11 21:35:04 +01001052 note: string | null;
TheCodedProfc016f9f2023-04-23 16:01:38 -04001053 flag: FlagColors | null;
pineafan4edb7762022-06-26 19:21:04 +01001054}
1055
pineafan73a7c4a2022-07-24 10:38:04 +01001056export interface PremiumSchema {
Skyler Grey75ea9172022-08-06 10:22:23 +01001057 user: string;
1058 level: number;
Skyler Grey75ea9172022-08-06 10:22:23 +01001059 appliesTo: string[];
TheCodedProf633866f2023-02-03 17:06:00 -05001060 expiresAt?: number;
Skyler Grey75ea9172022-08-06 10:22:23 +01001061}