TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 1 | import { ButtonStyle, CommandInteraction, ComponentType, GuildMember, Message, MessageComponentInteraction } from "discord.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 2 | import type Discord from "discord.js"; |
| 3 | import { Collection, MongoClient } from "mongodb"; |
pineafan | a2e39c7 | 2023-02-21 18:37:32 +0000 | [diff] [blame] | 4 | import config from "../config/main.js"; |
TheCodedProf | 633866f | 2023-02-03 17:06:00 -0500 | [diff] [blame] | 5 | import client from "../utils/client.js"; |
TheCodedProf | 088b1b2 | 2023-02-28 17:31:11 -0500 | [diff] [blame] | 6 | import * as crypto from "crypto"; |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 7 | import _ from "lodash"; |
| 8 | import defaultData from '../config/default.js'; |
TheCodedProf | 7527657 | 2023-03-04 13:49:16 -0500 | [diff] [blame] | 9 | |
TheCodedProf | faae533 | 2023-03-01 18:16:05 -0500 | [diff] [blame] | 10 | const username = encodeURIComponent(config.mongoOptions.username); |
| 11 | const password = encodeURIComponent(config.mongoOptions.password); |
Samuel Shuert | d66098b | 2023-03-04 14:05:26 -0500 | [diff] [blame] | 12 | |
TheCodedProf | 78b9033 | 2023-03-04 14:02:21 -0500 | [diff] [blame] | 13 | const mongoClient = new MongoClient(username ? `mongodb://${username}:${password}@${config.mongoOptions.host}?authMechanism=DEFAULT` : `mongodb://${config.mongoOptions.host}`, {authSource: config.mongoOptions.authSource}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 14 | await mongoClient.connect(); |
TheCodedProf | 8d577fa | 2023-03-01 13:06:40 -0500 | [diff] [blame] | 15 | const database = mongoClient.db(); |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 16 | |
TheCodedProf | 78b9033 | 2023-03-04 14:02:21 -0500 | [diff] [blame] | 17 | const collectionOptions = { authdb: config.mongoOptions.authSource, w: "majority" }; |
TheCodedProf | 75c51be | 2023-03-03 17:18:18 -0500 | [diff] [blame] | 18 | const getIV = () => crypto.randomBytes(16); |
TheCodedProf | faae533 | 2023-03-01 18:16:05 -0500 | [diff] [blame] | 19 | |
Samuel Shuert | cc63dee | 2023-03-03 18:54:29 -0500 | [diff] [blame] | 20 | |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 21 | export class Guilds { |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 22 | guilds: Collection<GuildConfig>; |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 23 | defaultData: GuildConfig; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 24 | |
| 25 | constructor() { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 26 | this.guilds = database.collection<GuildConfig>("guilds"); |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 27 | this.defaultData = defaultData; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 28 | } |
| 29 | |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 30 | async read(guild: string): Promise<GuildConfig> { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 31 | // console.log("Guild read") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 32 | const entry = await this.guilds.findOne({ id: guild }); |
TheCodedProf | 9f4cf9f | 2023-03-04 14:18:19 -0500 | [diff] [blame] | 33 | const data = _.cloneDeep(this.defaultData); |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 34 | return _.merge(data, entry ?? {}); |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 35 | } |
| 36 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 37 | async write(guild: string, set: object | null, unset: string[] | string = []) { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 38 | // console.log("Guild write") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 39 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 40 | const uo: Record<string, any> = {}; |
| 41 | if (!Array.isArray(unset)) unset = [unset]; |
| 42 | for (const key of unset) { |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 43 | uo[key] = null; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 44 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 45 | const out = { $set: {}, $unset: {} }; |
| 46 | if (set) out.$set = set; |
| 47 | if (unset.length) out.$unset = uo; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 48 | await this.guilds.updateOne({ id: guild }, out, { upsert: true }); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 49 | } |
| 50 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 51 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 52 | async append(guild: string, key: string, value: any) { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 53 | // console.log("Guild append") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 54 | if (Array.isArray(value)) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 55 | await this.guilds.updateOne( |
| 56 | { id: guild }, |
| 57 | { |
| 58 | $addToSet: { [key]: { $each: value } } |
| 59 | }, |
| 60 | { upsert: true } |
| 61 | ); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 62 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 63 | await this.guilds.updateOne( |
| 64 | { id: guild }, |
| 65 | { |
| 66 | $addToSet: { [key]: value } |
| 67 | }, |
| 68 | { upsert: true } |
| 69 | ); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 73 | async remove( |
| 74 | guild: string, |
| 75 | key: string, |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame] | 76 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 77 | value: any, |
| 78 | innerKey?: string | null |
| 79 | ) { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 80 | // console.log("Guild remove") |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 81 | if (innerKey) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 82 | await this.guilds.updateOne( |
| 83 | { id: guild }, |
| 84 | { |
| 85 | $pull: { [key]: { [innerKey]: { $eq: value } } } |
| 86 | }, |
| 87 | { upsert: true } |
| 88 | ); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 89 | } else if (Array.isArray(value)) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 90 | await this.guilds.updateOne( |
| 91 | { id: guild }, |
| 92 | { |
| 93 | $pullAll: { [key]: value } |
| 94 | }, |
| 95 | { upsert: true } |
| 96 | ); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 97 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 98 | await this.guilds.updateOne( |
| 99 | { id: guild }, |
| 100 | { |
| 101 | $pullAll: { [key]: [value] } |
| 102 | }, |
| 103 | { upsert: true } |
| 104 | ); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 105 | } |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 106 | } |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 107 | |
| 108 | async delete(guild: string) { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 109 | // console.log("Guild delete") |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 110 | await this.guilds.deleteOne({ id: guild }); |
| 111 | } |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 112 | } |
| 113 | |
TheCodedProf | cfe8e9a | 2023-02-26 17:28:09 -0500 | [diff] [blame] | 114 | interface TranscriptEmbed { |
| 115 | title?: string; |
| 116 | description?: string; |
| 117 | fields?: { |
| 118 | name: string; |
| 119 | value: string; |
| 120 | inline: boolean; |
| 121 | }[]; |
| 122 | footer?: { |
| 123 | text: string; |
| 124 | iconURL?: string; |
| 125 | }; |
TheCodedProf | faae533 | 2023-03-01 18:16:05 -0500 | [diff] [blame] | 126 | color?: number; |
| 127 | timestamp?: string; |
| 128 | author?: { |
| 129 | name: string; |
| 130 | iconURL?: string; |
| 131 | url?: string; |
| 132 | }; |
TheCodedProf | cfe8e9a | 2023-02-26 17:28:09 -0500 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | interface TranscriptComponent { |
| 136 | type: number; |
| 137 | style?: ButtonStyle; |
| 138 | label?: string; |
| 139 | description?: string; |
| 140 | placeholder?: string; |
| 141 | emojiURL?: string; |
| 142 | } |
| 143 | |
| 144 | interface TranscriptAuthor { |
| 145 | username: string; |
| 146 | discriminator: number; |
| 147 | nickname?: string; |
| 148 | id: string; |
| 149 | iconURL?: string; |
| 150 | topRole: { |
| 151 | color: number; |
| 152 | badgeURL?: string; |
TheCodedProf | 088b1b2 | 2023-02-28 17:31:11 -0500 | [diff] [blame] | 153 | }; |
| 154 | bot: boolean; |
TheCodedProf | cfe8e9a | 2023-02-26 17:28:09 -0500 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | interface TranscriptAttachment { |
| 158 | url: string; |
| 159 | filename: string; |
| 160 | size: number; |
| 161 | log?: string; |
| 162 | } |
| 163 | |
| 164 | interface TranscriptMessage { |
| 165 | id: string; |
| 166 | author: TranscriptAuthor; |
| 167 | content?: string; |
| 168 | embeds?: TranscriptEmbed[]; |
| 169 | components?: TranscriptComponent[][]; |
| 170 | editedTimestamp?: number; |
| 171 | createdTimestamp: number; |
| 172 | flags?: string[]; |
| 173 | attachments?: TranscriptAttachment[]; |
| 174 | stickerURLs?: string[]; |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 175 | referencedMessage?: string | [string, string, string]; // the message id, the channel id, the guild id |
TheCodedProf | cfe8e9a | 2023-02-26 17:28:09 -0500 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | interface TranscriptSchema { |
| 179 | code: string; |
| 180 | for: TranscriptAuthor; |
| 181 | type: "ticket" | "purge" |
| 182 | guild: string; |
| 183 | channel: string; |
| 184 | messages: TranscriptMessage[]; |
| 185 | createdTimestamp: number; |
| 186 | createdBy: TranscriptAuthor; |
| 187 | } |
| 188 | |
TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 189 | interface findDocSchema { channelID:string, messageID: string; transcript: string } |
| 190 | |
TheCodedProf | cfe8e9a | 2023-02-26 17:28:09 -0500 | [diff] [blame] | 191 | export class Transcript { |
| 192 | transcripts: Collection<TranscriptSchema>; |
TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 193 | messageToTranscript: Collection<findDocSchema>; |
TheCodedProf | cfe8e9a | 2023-02-26 17:28:09 -0500 | [diff] [blame] | 194 | |
| 195 | constructor() { |
| 196 | this.transcripts = database.collection<TranscriptSchema>("transcripts"); |
TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 197 | this.messageToTranscript = database.collection<findDocSchema>("messageToTranscript"); |
| 198 | } |
| 199 | |
| 200 | async upload(data: findDocSchema) { |
| 201 | // console.log("Transcript upload") |
| 202 | await this.messageToTranscript.insertOne(data); |
TheCodedProf | cfe8e9a | 2023-02-26 17:28:09 -0500 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | async create(transcript: Omit<TranscriptSchema, "code">) { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 206 | // console.log("Transcript create") |
TheCodedProf | cfe8e9a | 2023-02-26 17:28:09 -0500 | [diff] [blame] | 207 | let code; |
| 208 | do { |
TheCodedProf | 088b1b2 | 2023-02-28 17:31:11 -0500 | [diff] [blame] | 209 | code = crypto.randomBytes(64).toString("base64").replace(/=/g, "").replace(/\//g, "_").replace(/\+/g, "-"); |
TheCodedProf | cfe8e9a | 2023-02-26 17:28:09 -0500 | [diff] [blame] | 210 | } while (await this.transcripts.findOne({ code: code })); |
TheCodedProf | 75c51be | 2023-03-03 17:18:18 -0500 | [diff] [blame] | 211 | const key = crypto.randomBytes(32**2).toString("base64").replace(/=/g, "").replace(/\//g, "_").replace(/\+/g, "-").substring(0, 32); |
| 212 | const iv = getIV().toString("base64").replace(/=/g, "").replace(/\//g, "_").replace(/\+/g, "-"); |
| 213 | for(const message of transcript.messages) { |
| 214 | if(message.content) { |
| 215 | const encCipher = crypto.createCipheriv("AES-256-CBC", key, iv); |
| 216 | message.content = encCipher.update(message.content, "utf8", "base64") + encCipher.final("base64"); |
| 217 | } |
| 218 | } |
TheCodedProf | cfe8e9a | 2023-02-26 17:28:09 -0500 | [diff] [blame] | 219 | |
TheCodedProf | faae533 | 2023-03-01 18:16:05 -0500 | [diff] [blame] | 220 | const doc = await this.transcripts.insertOne(Object.assign(transcript, { code: code }), collectionOptions); |
TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 221 | if(doc.acknowledged) { |
| 222 | client.database.eventScheduler.schedule("deleteTranscript", (Date.now() + 1000 * 60 * 60 * 24 * 7).toString(), { guild: transcript.guild, code: code, iv: iv, key: key }); |
| 223 | return [code, key, iv]; |
| 224 | } |
TheCodedProf | 75c51be | 2023-03-03 17:18:18 -0500 | [diff] [blame] | 225 | else return [null, null, null]; |
TheCodedProf | cfe8e9a | 2023-02-26 17:28:09 -0500 | [diff] [blame] | 226 | } |
| 227 | |
TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 228 | async delete(code: string) { |
| 229 | // console.log("Transcript delete") |
| 230 | await this.transcripts.deleteOne({ code: code }); |
TheCodedProf | 75c51be | 2023-03-03 17:18:18 -0500 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | async deleteAll(guild: string) { |
| 234 | // console.log("Transcript delete") |
| 235 | const filteredDocs = await this.transcripts.find({ guild: guild }).toArray(); |
| 236 | for (const doc of filteredDocs) { |
| 237 | await this.transcripts.deleteOne({ code: doc.code }); |
| 238 | } |
TheCodedProf | cfe8e9a | 2023-02-26 17:28:09 -0500 | [diff] [blame] | 239 | } |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 240 | |
TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 241 | async readEncrypted(code: string) { |
| 242 | // console.log("Transcript read") |
| 243 | let doc: TranscriptSchema | null = await this.transcripts.findOne({ code: code }); |
| 244 | let findDoc: findDocSchema | null = null; |
| 245 | if(!doc) findDoc = (await this.messageToTranscript.findOne({ transcript: code })); |
| 246 | if(findDoc) { |
| 247 | const message = await ((client.channels.cache.get(findDoc.channelID)) as Discord.TextBasedChannel | null)?.messages.fetch(findDoc.messageID); |
| 248 | if(!message) return null; |
| 249 | const attachment = message.attachments.first(); |
| 250 | if(!attachment) return null; |
| 251 | const transcript = (await fetch(attachment.url)).body; |
| 252 | if(!transcript) return null; |
| 253 | const reader = transcript.getReader(); |
| 254 | let data: Uint8Array | null = null; |
| 255 | let allPacketsReceived = false; |
| 256 | while (!allPacketsReceived) { |
| 257 | const { value, done } = await reader.read(); |
| 258 | if (done) {allPacketsReceived = true; continue;} |
| 259 | if(!data) { |
| 260 | data = value; |
| 261 | } else { |
| 262 | data = new Uint8Array(Buffer.concat([data, value])); |
| 263 | } |
| 264 | } |
| 265 | if(!data) return null; |
| 266 | doc = JSON.parse(Buffer.from(data).toString()); |
| 267 | } |
| 268 | if(!doc) return null; |
| 269 | return doc; |
| 270 | } |
| 271 | |
| 272 | async read(code: string, key: string, iv: string) { |
| 273 | // console.log("Transcript read") |
| 274 | let doc: TranscriptSchema | null = await this.transcripts.findOne({ code: code }); |
| 275 | let findDoc: findDocSchema | null = null; |
| 276 | if(!doc) findDoc = (await this.messageToTranscript.findOne({ transcript: code })); |
| 277 | if(findDoc) { |
| 278 | const message = await ((client.channels.cache.get(findDoc.channelID)) as Discord.TextBasedChannel | null)?.messages.fetch(findDoc.messageID); |
| 279 | if(!message) return null; |
| 280 | const attachment = message.attachments.first(); |
| 281 | if(!attachment) return null; |
| 282 | const transcript = (await fetch(attachment.url)).body; |
| 283 | if(!transcript) return null; |
| 284 | const reader = transcript.getReader(); |
| 285 | let data: Uint8Array | null = null; |
| 286 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, no-constant-condition |
| 287 | while(true) { |
| 288 | const { value, done } = await reader.read(); |
| 289 | if (done) break; |
| 290 | if(!data) { |
| 291 | data = value; |
| 292 | } else { |
| 293 | data = new Uint8Array(Buffer.concat([data, value])); |
| 294 | } |
| 295 | } |
| 296 | if(!data) return null; |
| 297 | doc = JSON.parse(Buffer.from(data).toString()); |
| 298 | } |
| 299 | if(!doc) return null; |
| 300 | for(const message of doc.messages) { |
| 301 | if(message.content) { |
| 302 | const decCipher = crypto.createDecipheriv("AES-256-CBC", key, iv); |
| 303 | message.content = decCipher.update(message.content, "base64", "utf8") + decCipher.final("utf8"); |
| 304 | } |
| 305 | } |
| 306 | return doc; |
| 307 | } |
| 308 | |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 309 | async createTranscript(messages: Message[], interaction: MessageComponentInteraction | CommandInteraction, member: GuildMember) { |
| 310 | const interactionMember = await interaction.guild?.members.fetch(interaction.user.id) |
| 311 | const newOut: Omit<TranscriptSchema, "code"> = { |
| 312 | type: "ticket", |
| 313 | for: { |
| 314 | username: member!.user.username, |
| 315 | discriminator: parseInt(member!.user.discriminator), |
| 316 | id: member!.user.id, |
| 317 | topRole: { |
| 318 | color: member!.roles.highest.color |
TheCodedProf | 088b1b2 | 2023-02-28 17:31:11 -0500 | [diff] [blame] | 319 | }, |
| 320 | iconURL: member!.user.displayAvatarURL({ forceStatic: true}), |
| 321 | bot: member!.user.bot |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 322 | }, |
| 323 | guild: interaction.guild!.id, |
| 324 | channel: interaction.channel!.id, |
| 325 | messages: [], |
| 326 | createdTimestamp: Date.now(), |
| 327 | createdBy: { |
| 328 | username: interaction.user.username, |
| 329 | discriminator: parseInt(interaction.user.discriminator), |
| 330 | id: interaction.user.id, |
| 331 | topRole: { |
| 332 | color: interactionMember?.roles.highest.color ?? 0x000000 |
TheCodedProf | 088b1b2 | 2023-02-28 17:31:11 -0500 | [diff] [blame] | 333 | }, |
| 334 | iconURL: interaction.user.displayAvatarURL({ forceStatic: true}), |
| 335 | bot: interaction.user.bot |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 336 | } |
| 337 | } |
TheCodedProf | 088b1b2 | 2023-02-28 17:31:11 -0500 | [diff] [blame] | 338 | if(member.nickname) newOut.for.nickname = member.nickname; |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 339 | if(interactionMember?.roles.icon) newOut.createdBy.topRole.badgeURL = interactionMember.roles.icon.iconURL()!; |
| 340 | messages.reverse().forEach((message) => { |
| 341 | const msg: TranscriptMessage = { |
| 342 | id: message.id, |
| 343 | author: { |
| 344 | username: message.author.username, |
| 345 | discriminator: parseInt(message.author.discriminator), |
| 346 | id: message.author.id, |
| 347 | topRole: { |
| 348 | color: message.member!.roles.highest.color |
TheCodedProf | 088b1b2 | 2023-02-28 17:31:11 -0500 | [diff] [blame] | 349 | }, |
TheCodedProf | faae533 | 2023-03-01 18:16:05 -0500 | [diff] [blame] | 350 | iconURL: message.member!.user.displayAvatarURL({ forceStatic: true}), |
TheCodedProf | 088b1b2 | 2023-02-28 17:31:11 -0500 | [diff] [blame] | 351 | bot: message.author.bot |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 352 | }, |
| 353 | createdTimestamp: message.createdTimestamp |
| 354 | }; |
TheCodedProf | 088b1b2 | 2023-02-28 17:31:11 -0500 | [diff] [blame] | 355 | if(message.member?.nickname) msg.author.nickname = message.member.nickname; |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 356 | if (message.member!.roles.icon) msg.author.topRole.badgeURL = message.member!.roles.icon.iconURL()!; |
| 357 | if (message.content) msg.content = message.content; |
| 358 | if (message.embeds.length > 0) msg.embeds = message.embeds.map(embed => { |
| 359 | const obj: TranscriptEmbed = {}; |
| 360 | if (embed.title) obj.title = embed.title; |
| 361 | if (embed.description) obj.description = embed.description; |
| 362 | if (embed.fields.length > 0) obj.fields = embed.fields.map(field => { |
| 363 | return { |
| 364 | name: field.name, |
| 365 | value: field.value, |
| 366 | inline: field.inline ?? false |
| 367 | } |
| 368 | }); |
TheCodedProf | faae533 | 2023-03-01 18:16:05 -0500 | [diff] [blame] | 369 | if (embed.color) obj.color = embed.color; |
| 370 | if (embed.timestamp) obj.timestamp = embed.timestamp |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 371 | if (embed.footer) obj.footer = { |
| 372 | text: embed.footer.text, |
| 373 | }; |
| 374 | if (embed.footer?.iconURL) obj.footer!.iconURL = embed.footer.iconURL; |
TheCodedProf | faae533 | 2023-03-01 18:16:05 -0500 | [diff] [blame] | 375 | if (embed.author) obj.author = { |
| 376 | name: embed.author.name |
| 377 | }; |
| 378 | if (embed.author?.iconURL) obj.author!.iconURL = embed.author.iconURL; |
| 379 | if (embed.author?.url) obj.author!.url = embed.author.url; |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 380 | return obj; |
| 381 | }); |
| 382 | if (message.components.length > 0) msg.components = message.components.map(component => component.components.map(child => { |
| 383 | const obj: TranscriptComponent = { |
| 384 | type: child.type |
| 385 | } |
| 386 | if (child.type === ComponentType.Button) { |
| 387 | obj.style = child.style; |
| 388 | obj.label = child.label ?? ""; |
| 389 | } else if (child.type > 2) { |
| 390 | obj.placeholder = child.placeholder ?? ""; |
| 391 | } |
| 392 | return obj |
| 393 | })); |
| 394 | if (message.editedTimestamp) msg.editedTimestamp = message.editedTimestamp; |
| 395 | msg.flags = message.flags.toArray(); |
| 396 | |
| 397 | if (message.stickers.size > 0) msg.stickerURLs = message.stickers.map(sticker => sticker.url); |
| 398 | if (message.reference) msg.referencedMessage = [message.reference.guildId ?? "", message.reference.channelId, message.reference.messageId ?? ""]; |
| 399 | newOut.messages.push(msg); |
| 400 | }); |
| 401 | return newOut; |
| 402 | } |
| 403 | |
| 404 | toHumanReadable(transcript: Omit<TranscriptSchema, "code">): string { |
| 405 | let out = ""; |
| 406 | for (const message of transcript.messages) { |
| 407 | if (message.referencedMessage) { |
| 408 | if (Array.isArray(message.referencedMessage)) { |
| 409 | out += `> [Crosspost From] ${message.referencedMessage[0]} in ${message.referencedMessage[1]} in ${message.referencedMessage[2]}\n`; |
| 410 | } |
| 411 | else out += `> [Reply To] ${message.referencedMessage}\n`; |
| 412 | } |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 413 | out += `${message.author.nickname ?? message.author.username}#${message.author.discriminator} (${message.author.id}) (${message.id})`; |
| 414 | out += ` [${new Date(message.createdTimestamp).toISOString()}]`; |
| 415 | if (message.editedTimestamp) out += ` [Edited: ${new Date(message.editedTimestamp).toISOString()}]`; |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 416 | out += "\n"; |
| 417 | if (message.content) out += `[Content]\n${message.content}\n\n`; |
| 418 | if (message.embeds) { |
| 419 | for (const embed of message.embeds) { |
| 420 | out += `[Embed]\n`; |
| 421 | if (embed.title) out += `| Title: ${embed.title}\n`; |
| 422 | if (embed.description) out += `| Description: ${embed.description}\n`; |
| 423 | if (embed.fields) { |
| 424 | for (const field of embed.fields) { |
| 425 | out += `| Field: ${field.name} - ${field.value}\n`; |
| 426 | } |
| 427 | } |
| 428 | if (embed.footer) { |
| 429 | out += `|Footer: ${embed.footer.text}\n`; |
| 430 | } |
| 431 | out += "\n"; |
| 432 | } |
| 433 | } |
| 434 | if (message.components) { |
| 435 | for (const component of message.components) { |
| 436 | out += `[Component]\n`; |
| 437 | for (const button of component) { |
| 438 | out += `| Button: ${button.label ?? button.description}\n`; |
| 439 | } |
| 440 | out += "\n"; |
| 441 | } |
| 442 | } |
| 443 | if (message.attachments) { |
| 444 | for (const attachment of message.attachments) { |
| 445 | out += `[Attachment] ${attachment.filename} (${attachment.size} bytes) ${attachment.url}\n`; |
| 446 | } |
| 447 | } |
TheCodedProf | 088b1b2 | 2023-02-28 17:31:11 -0500 | [diff] [blame] | 448 | out += "\n\n" |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 449 | } |
| 450 | return out |
| 451 | } |
TheCodedProf | cfe8e9a | 2023-02-26 17:28:09 -0500 | [diff] [blame] | 452 | } |
| 453 | |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 454 | export class History { |
| 455 | histories: Collection<HistorySchema>; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 456 | |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 457 | constructor() { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 458 | this.histories = database.collection<HistorySchema>("history"); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 459 | } |
| 460 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 461 | async create( |
| 462 | type: string, |
| 463 | guild: string, |
| 464 | user: Discord.User, |
| 465 | moderator: Discord.User | null, |
| 466 | reason: string | null, |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 467 | before?: string | null, |
| 468 | after?: string | null, |
| 469 | amount?: string | null |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 470 | ) { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 471 | // console.log("History create"); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 472 | await this.histories.insertOne({ |
| 473 | type: type, |
| 474 | guild: guild, |
| 475 | user: user.id, |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 476 | moderator: moderator ? moderator.id : null, |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 477 | reason: reason, |
| 478 | occurredAt: new Date(), |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 479 | before: before ?? null, |
| 480 | after: after ?? null, |
| 481 | amount: amount ?? null |
TheCodedProf | faae533 | 2023-03-01 18:16:05 -0500 | [diff] [blame] | 482 | }, collectionOptions); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | async read(guild: string, user: string, year: number) { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 486 | // console.log("History read"); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 487 | const entry = (await this.histories |
| 488 | .find({ |
| 489 | guild: guild, |
| 490 | user: user, |
| 491 | occurredAt: { |
| 492 | $gte: new Date(year - 1, 11, 31, 23, 59, 59), |
| 493 | $lt: new Date(year + 1, 0, 1, 0, 0, 0) |
| 494 | } |
| 495 | }) |
| 496 | .toArray()) as HistorySchema[]; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 497 | return entry; |
| 498 | } |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 499 | |
| 500 | async delete(guild: string) { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 501 | // console.log("History delete"); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 502 | await this.histories.deleteMany({ guild: guild }); |
| 503 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 504 | } |
| 505 | |
TheCodedProf | b5e9d55 | 2023-01-29 15:43:26 -0500 | [diff] [blame] | 506 | interface ScanCacheSchema { |
| 507 | addedAt: Date; |
| 508 | hash: string; |
| 509 | data: boolean; |
| 510 | tags: string[]; |
| 511 | } |
| 512 | |
| 513 | export class ScanCache { |
| 514 | scanCache: Collection<ScanCacheSchema>; |
| 515 | |
| 516 | constructor() { |
| 517 | this.scanCache = database.collection<ScanCacheSchema>("scanCache"); |
| 518 | } |
| 519 | |
| 520 | async read(hash: string) { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 521 | // console.log("ScanCache read"); |
TheCodedProf | b5e9d55 | 2023-01-29 15:43:26 -0500 | [diff] [blame] | 522 | return await this.scanCache.findOne({ hash: hash }); |
| 523 | } |
| 524 | |
| 525 | async write(hash: string, data: boolean, tags?: string[]) { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 526 | // console.log("ScanCache write"); |
TheCodedProf | faae533 | 2023-03-01 18:16:05 -0500 | [diff] [blame] | 527 | await this.scanCache.insertOne({ hash: hash, data: data, tags: tags ?? [], addedAt: new Date() }, collectionOptions); |
TheCodedProf | b5e9d55 | 2023-01-29 15:43:26 -0500 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | async cleanup() { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 531 | // console.log("ScanCache cleanup"); |
TheCodedProf | b5e9d55 | 2023-01-29 15:43:26 -0500 | [diff] [blame] | 532 | await this.scanCache.deleteMany({ addedAt: { $lt: new Date(Date.now() - (1000 * 60 * 60 * 24 * 31)) }, hash: { $not$text: "http"} }); |
| 533 | } |
| 534 | } |
| 535 | |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 536 | export class PerformanceTest { |
| 537 | performanceData: Collection<PerformanceDataSchema>; |
| 538 | |
| 539 | constructor() { |
| 540 | this.performanceData = database.collection<PerformanceDataSchema>("performance"); |
| 541 | } |
| 542 | |
| 543 | async record(data: PerformanceDataSchema) { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 544 | // console.log("PerformanceTest record"); |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 545 | data.timestamp = new Date(); |
TheCodedProf | faae533 | 2023-03-01 18:16:05 -0500 | [diff] [blame] | 546 | await this.performanceData.insertOne(data, collectionOptions); |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 547 | } |
| 548 | async read() { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 549 | // console.log("PerformanceTest read"); |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 550 | return await this.performanceData.find({}).toArray(); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | export interface PerformanceDataSchema { |
| 555 | timestamp?: Date; |
| 556 | discord: number; |
| 557 | databaseRead: number; |
| 558 | resources: { |
| 559 | cpu: number; |
| 560 | memory: number; |
| 561 | temperature: number; |
| 562 | } |
| 563 | } |
| 564 | |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 565 | export class ModNotes { |
| 566 | modNotes: Collection<ModNoteSchema>; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 567 | |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 568 | constructor() { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 569 | this.modNotes = database.collection<ModNoteSchema>("modNotes"); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | async create(guild: string, user: string, note: string | null) { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 573 | // console.log("ModNotes create"); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 574 | await this.modNotes.updateOne({ guild: guild, user: user }, { $set: { note: note } }, { upsert: true }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | async read(guild: string, user: string) { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 578 | // console.log("ModNotes read"); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 579 | const entry = await this.modNotes.findOne({ guild: guild, user: user }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 580 | return entry?.note ?? null; |
| 581 | } |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 582 | |
| 583 | async delete(guild: string) { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 584 | // console.log("ModNotes delete"); |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 585 | await this.modNotes.deleteMany({ guild: guild }); |
| 586 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 587 | } |
| 588 | |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 589 | export class Premium { |
| 590 | premium: Collection<PremiumSchema>; |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 591 | cache: Map<string, [boolean, string, number, boolean, Date]>; // Date indicates the time one hour after it was created |
| 592 | cacheTimeout = 1000 * 60 * 60; // 1 hour |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 593 | |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 594 | constructor() { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 595 | this.premium = database.collection<PremiumSchema>("premium"); |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 596 | this.cache = new Map<string, [boolean, string, number, boolean, Date]>(); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 597 | } |
| 598 | |
TheCodedProf | 633866f | 2023-02-03 17:06:00 -0500 | [diff] [blame] | 599 | async updateUser(user: string, level: number) { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 600 | // console.log("Premium updateUser"); |
TheCodedProf | 633866f | 2023-02-03 17:06:00 -0500 | [diff] [blame] | 601 | if(!(await this.userExists(user))) await this.createUser(user, level); |
| 602 | await this.premium.updateOne({ user: user }, { $set: { level: level } }, { upsert: true }); |
| 603 | } |
| 604 | |
| 605 | async userExists(user: string): Promise<boolean> { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 606 | // console.log("Premium userExists"); |
TheCodedProf | 633866f | 2023-02-03 17:06:00 -0500 | [diff] [blame] | 607 | const entry = await this.premium.findOne({ user: user }); |
| 608 | return entry ? true : false; |
| 609 | } |
TheCodedProf | 633866f | 2023-02-03 17:06:00 -0500 | [diff] [blame] | 610 | async createUser(user: string, level: number) { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 611 | // console.log("Premium createUser"); |
TheCodedProf | faae533 | 2023-03-01 18:16:05 -0500 | [diff] [blame] | 612 | await this.premium.insertOne({ user: user, appliesTo: [], level: level }, collectionOptions); |
TheCodedProf | 633866f | 2023-02-03 17:06:00 -0500 | [diff] [blame] | 613 | } |
| 614 | |
TheCodedProf | aa3fe99 | 2023-02-25 21:53:09 -0500 | [diff] [blame] | 615 | async hasPremium(guild: string): Promise<[boolean, string, number, boolean] | null> { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 616 | // console.log("Premium hasPremium"); |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 617 | // [Has premium, user giving premium, level, is mod: if given automatically] |
| 618 | const cached = this.cache.get(guild); |
| 619 | if (cached && cached[4].getTime() < Date.now()) return [cached[0], cached[1], cached[2], cached[3]]; |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 620 | const entries = await this.premium.find({}).toArray(); |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 621 | const members = (await client.guilds.fetch(guild)).members.cache |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 622 | for(const {user} of entries) { |
| 623 | const member = members.get(user); |
TheCodedProf | aa3fe99 | 2023-02-25 21:53:09 -0500 | [diff] [blame] | 624 | if(member) { //TODO: Notify user if they've given premium to a server that has since gotten premium via a mod. |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 625 | const modPerms = //TODO: Create list in config for perms |
| 626 | member.permissions.has("Administrator") || |
| 627 | member.permissions.has("ManageChannels") || |
| 628 | member.permissions.has("ManageRoles") || |
| 629 | member.permissions.has("ManageEmojisAndStickers") || |
| 630 | member.permissions.has("ManageWebhooks") || |
| 631 | member.permissions.has("ManageGuild") || |
| 632 | member.permissions.has("KickMembers") || |
| 633 | member.permissions.has("BanMembers") || |
| 634 | member.permissions.has("ManageEvents") || |
| 635 | member.permissions.has("ManageMessages") || |
| 636 | member.permissions.has("ManageThreads") |
| 637 | const entry = entries.find(e => e.user === member.id); |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 638 | if(entry && (entry.level === 3) && modPerms) { |
| 639 | this.cache.set(guild, [true, member.id, entry.level, true, new Date(Date.now() + this.cacheTimeout)]); |
| 640 | return [true, member.id, entry.level, true]; |
| 641 | } |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 642 | } |
| 643 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 644 | const entry = await this.premium.findOne({ |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 645 | appliesTo: { |
| 646 | $elemMatch: { |
| 647 | $eq: guild |
| 648 | } |
| 649 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 650 | }); |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 651 | this.cache.set(guild, [entry ? true : false, entry?.user ?? "", entry?.level ?? 0, false, new Date(Date.now() + this.cacheTimeout)]); |
TheCodedProf | aa3fe99 | 2023-02-25 21:53:09 -0500 | [diff] [blame] | 652 | return entry ? [true, entry.user, entry.level, false] : null; |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 653 | } |
| 654 | |
TheCodedProf | 633866f | 2023-02-03 17:06:00 -0500 | [diff] [blame] | 655 | async fetchUser(user: string): Promise<PremiumSchema | null> { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 656 | // console.log("Premium fetchUser"); |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 657 | const entry = await this.premium.findOne({ user: user }); |
TheCodedProf | 633866f | 2023-02-03 17:06:00 -0500 | [diff] [blame] | 658 | if (!entry) return null; |
| 659 | return entry; |
| 660 | } |
| 661 | |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 662 | async checkAllPremium(member?: GuildMember) { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 663 | // console.log("Premium checkAllPremium"); |
TheCodedProf | 633866f | 2023-02-03 17:06:00 -0500 | [diff] [blame] | 664 | const entries = await this.premium.find({}).toArray(); |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 665 | if(member) { |
| 666 | const entry = entries.find(e => e.user === member.id); |
| 667 | if(entry) { |
| 668 | const expiresAt = entry.expiresAt; |
| 669 | if(expiresAt) expiresAt < Date.now() ? await this.premium.deleteOne({user: member.id}) : null; |
| 670 | } |
| 671 | const roles = member.roles; |
| 672 | let level = 0; |
| 673 | if (roles.cache.has("1066468879309750313")) { |
TheCodedProf | 633866f | 2023-02-03 17:06:00 -0500 | [diff] [blame] | 674 | level = 99; |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 675 | } else if (roles.cache.has("1066465491713003520")) { |
TheCodedProf | 633866f | 2023-02-03 17:06:00 -0500 | [diff] [blame] | 676 | level = 1; |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 677 | } else if (roles.cache.has("1066439526496604194")) { |
TheCodedProf | 633866f | 2023-02-03 17:06:00 -0500 | [diff] [blame] | 678 | level = 2; |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 679 | } else if (roles.cache.has("1066464134322978912")) { |
TheCodedProf | 633866f | 2023-02-03 17:06:00 -0500 | [diff] [blame] | 680 | level = 3; |
| 681 | } |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 682 | await this.updateUser(member.id, level); |
TheCodedProf | 633866f | 2023-02-03 17:06:00 -0500 | [diff] [blame] | 683 | if (level > 0) { |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 684 | await this.premium.updateOne({ user: member.id }, {$unset: { expiresAt: ""}}) |
TheCodedProf | 633866f | 2023-02-03 17:06:00 -0500 | [diff] [blame] | 685 | } else { |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 686 | await this.premium.updateOne({ user: member.id }, {$set: { expiresAt: (Date.now() + (1000*60*60*24*3)) }}) |
| 687 | } |
| 688 | } else { |
| 689 | const members = await (await client.guilds.fetch('684492926528651336')).members.fetch(); |
| 690 | for(const {roles, id} of members.values()) { |
| 691 | const entry = entries.find(e => e.user === id); |
| 692 | if(entry) { |
| 693 | const expiresAt = entry.expiresAt; |
| 694 | if(expiresAt) expiresAt < Date.now() ? await this.premium.deleteOne({user: id}) : null; |
| 695 | } |
| 696 | let level: number = 0; |
| 697 | if (roles.cache.has("1066468879309750313")) { |
| 698 | level = 99; |
| 699 | } else if (roles.cache.has("1066465491713003520")) { |
| 700 | level = 1; |
| 701 | } else if (roles.cache.has("1066439526496604194")) { |
| 702 | level = 2; |
| 703 | } else if (roles.cache.has("1066464134322978912")) { |
| 704 | level = 3; |
| 705 | } |
| 706 | await this.updateUser(id, level); |
| 707 | if (level > 0) { |
| 708 | await this.premium.updateOne({ user: id }, {$unset: { expiresAt: ""}}) |
| 709 | } else { |
| 710 | await this.premium.updateOne({ user: id }, {$set: { expiresAt: (Date.now() + (1000*60*60*24*3)) }}) |
| 711 | } |
TheCodedProf | 633866f | 2023-02-03 17:06:00 -0500 | [diff] [blame] | 712 | } |
| 713 | } |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 714 | } |
| 715 | |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 716 | async addPremium(user: string, guild: string) { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 717 | // console.log("Premium addPremium"); |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 718 | const { level } = (await this.fetchUser(user))!; |
| 719 | this.cache.set(guild, [true, user, level, false, new Date(Date.now() + this.cacheTimeout)]); |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 720 | return this.premium.updateOne({ user: user }, { $addToSet: { appliesTo: guild } }, { upsert: true }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 721 | } |
TheCodedProf | fc420b7 | 2023-01-24 17:14:38 -0500 | [diff] [blame] | 722 | |
| 723 | removePremium(user: string, guild: string) { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 724 | // console.log("Premium removePremium"); |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 725 | this.cache.set(guild, [false, "", 0, false, new Date(Date.now() + this.cacheTimeout)]); |
TheCodedProf | fc420b7 | 2023-01-24 17:14:38 -0500 | [diff] [blame] | 726 | return this.premium.updateOne({ user: user }, { $pull: { appliesTo: guild } }); |
| 727 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 728 | } |
| 729 | |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 730 | export interface GuildConfig { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 731 | id: string; |
| 732 | version: number; |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 733 | singleEventNotifications: Record<string, boolean>; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 734 | filters: { |
| 735 | images: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 736 | NSFW: boolean; |
| 737 | size: boolean; |
| 738 | }; |
| 739 | malware: boolean; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 740 | wordFilter: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 741 | enabled: boolean; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 742 | words: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 743 | strict: string[]; |
| 744 | loose: string[]; |
| 745 | }; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 746 | allowed: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 747 | users: string[]; |
| 748 | roles: string[]; |
| 749 | channels: string[]; |
| 750 | }; |
| 751 | }; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 752 | invite: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 753 | enabled: boolean; |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 754 | allowed: { |
| 755 | channels: string[]; |
| 756 | roles: string[]; |
| 757 | users: string[]; |
| 758 | }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 759 | }; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 760 | pings: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 761 | mass: number; |
| 762 | everyone: boolean; |
| 763 | roles: boolean; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 764 | allowed: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 765 | roles: string[]; |
| 766 | rolesToMention: string[]; |
| 767 | users: string[]; |
| 768 | channels: string[]; |
| 769 | }; |
| 770 | }; |
TheCodedProf | ad0b820 | 2023-02-14 14:27:09 -0500 | [diff] [blame] | 771 | clean: { |
| 772 | channels: string[]; |
| 773 | allowed: { |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 774 | users: string[]; |
TheCodedProf | ad0b820 | 2023-02-14 14:27:09 -0500 | [diff] [blame] | 775 | roles: string[]; |
| 776 | } |
| 777 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 778 | }; |
TheCodedProf | baee2c1 | 2023-02-18 16:11:06 -0500 | [diff] [blame] | 779 | autoPublish: { |
| 780 | enabled: boolean; |
| 781 | channels: string[]; |
| 782 | } |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 783 | welcome: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 784 | enabled: boolean; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 785 | role: string | null; |
| 786 | ping: string | null; |
| 787 | channel: string | null; |
| 788 | message: string | null; |
| 789 | }; |
| 790 | stats: Record<string, { name: string; enabled: boolean }>; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 791 | logging: { |
| 792 | logs: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 793 | enabled: boolean; |
| 794 | channel: string | null; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 795 | toLog: string; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 796 | }; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 797 | staff: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 798 | channel: string | null; |
| 799 | }; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 800 | attachments: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 801 | channel: string | null; |
| 802 | saved: Record<string, string>; |
| 803 | }; |
| 804 | }; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 805 | verify: { |
PineaFan | df4996f | 2023-01-01 14:20:06 +0000 | [diff] [blame] | 806 | enabled: boolean; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 807 | role: string | null; |
| 808 | }; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 809 | tickets: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 810 | enabled: boolean; |
| 811 | category: string | null; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 812 | types: string; |
| 813 | customTypes: string[] | null; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 814 | useCustom: boolean; |
| 815 | supportRole: string | null; |
| 816 | maxTickets: number; |
| 817 | }; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 818 | moderation: { |
| 819 | mute: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 820 | timeout: boolean; |
| 821 | role: string | null; |
| 822 | text: string | null; |
| 823 | link: string | null; |
| 824 | }; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 825 | kick: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 826 | text: string | null; |
| 827 | link: string | null; |
| 828 | }; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 829 | ban: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 830 | text: string | null; |
| 831 | link: string | null; |
| 832 | }; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 833 | softban: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 834 | text: string | null; |
| 835 | link: string | null; |
| 836 | }; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 837 | warn: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 838 | text: string | null; |
| 839 | link: string | null; |
| 840 | }; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 841 | role: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 842 | role: string | null; |
TheCodedProf | d9636e8 | 2023-01-17 22:13:06 -0500 | [diff] [blame] | 843 | text: null; |
| 844 | link: null; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 845 | }; |
PineaFan | e6ba788 | 2023-01-18 20:41:16 +0000 | [diff] [blame] | 846 | nick: { |
| 847 | text: string | null; |
| 848 | link: string | null; |
| 849 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 850 | }; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 851 | tracks: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 852 | name: string; |
| 853 | retainPrevious: boolean; |
| 854 | nullable: boolean; |
| 855 | track: string[]; |
| 856 | manageableBy: string[]; |
| 857 | }[]; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 858 | roleMenu: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 859 | enabled: boolean; |
| 860 | allowWebUI: boolean; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 861 | options: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 862 | name: string; |
| 863 | description: string; |
| 864 | min: number; |
| 865 | max: number; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 866 | options: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 867 | name: string; |
| 868 | description: string | null; |
| 869 | role: string; |
| 870 | }[]; |
| 871 | }[]; |
| 872 | }; |
| 873 | tags: Record<string, string>; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 874 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 875 | |
| 876 | export interface HistorySchema { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 877 | type: string; |
| 878 | guild: string; |
| 879 | user: string; |
| 880 | moderator: string | null; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 881 | reason: string | null; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 882 | occurredAt: Date; |
| 883 | before: string | null; |
| 884 | after: string | null; |
| 885 | amount: string | null; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | export interface ModNoteSchema { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 889 | guild: string; |
| 890 | user: string; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 891 | note: string | null; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 892 | } |
| 893 | |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 894 | export interface PremiumSchema { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 895 | user: string; |
| 896 | level: number; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 897 | appliesTo: string[]; |
TheCodedProf | 633866f | 2023-02-03 17:06:00 -0500 | [diff] [blame] | 898 | expiresAt?: number; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 899 | } |