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