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