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