pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import type Discord from "discord.js"; |
| 2 | import { Collection, MongoClient } from "mongodb"; |
| 3 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 4 | // @ts-ignore |
| 5 | import structuredClone from "@ungap/structured-clone"; |
| 6 | import config from "../config/main.json" assert {type: "json"}; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 7 | |
| 8 | |
| 9 | const mongoClient = new MongoClient(config.mongoUrl); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 10 | await mongoClient.connect(); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 11 | const database = mongoClient.db("Nucleus"); |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 12 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 13 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 14 | export const Entry = (data: any) => { |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 15 | data = data ?? {}; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 16 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 17 | data.getKey = (key: any) => data[key]; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 18 | return { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 19 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 20 | get(target: Record<string, any>, prop: string, receiver: any) { |
| 21 | let dataToReturn = data[prop]; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 22 | if (dataToReturn === null ) return Reflect.get(target, prop, receiver); |
| 23 | if (typeof dataToReturn === "object" && !Array.isArray(dataToReturn)) dataToReturn = new Proxy( |
| 24 | Reflect.get(target, prop, receiver), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 25 | Entry(dataToReturn) |
| 26 | ); |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 27 | return dataToReturn ?? Reflect.get(target, prop, receiver); |
| 28 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 29 | }; |
| 30 | }; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 31 | |
| 32 | |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 33 | export class Guilds { |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 34 | guilds: Collection<GuildConfig>; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 35 | defaultData: GuildConfig | null; |
| 36 | |
| 37 | constructor() { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 38 | this.guilds = database.collection<GuildConfig>("guilds"); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 39 | this.defaultData = null; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 40 | return this; |
| 41 | } |
| 42 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 43 | async setup() { |
| 44 | this.defaultData = (await import("../config/default.json", { assert: { type: "json" }})).default as unknown as GuildConfig; |
| 45 | } |
| 46 | |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 47 | async read(guild: string) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 48 | const entry = await this.guilds.findOne({ id: guild }); |
| 49 | return new Proxy(structuredClone(this.defaultData), Entry(entry)) as unknown as GuildConfig; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 50 | } |
| 51 | |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 52 | async write(guild: string, set: object | null, unset: string[] | string = []) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 53 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 54 | const uo: Record<string, any> = {}; |
| 55 | if (!Array.isArray(unset)) unset = [unset]; |
| 56 | for (const key of unset) { |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 57 | uo[key] = null; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 58 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 59 | const out = {$set: {}, $unset: {}}; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 60 | if (set) out["$set"] = set; |
| 61 | if (unset.length) out["$unset"] = uo; |
| 62 | await this.guilds.updateOne({ id: guild }, out, { upsert: true }); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 63 | } |
| 64 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 65 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 66 | async append(guild: string, key: string, value: any) { |
| 67 | if (Array.isArray(value)) { |
| 68 | await this.guilds.updateOne({ id: guild }, { |
| 69 | $addToSet: { [key]: { $each: value } } |
| 70 | }, { upsert: true }); |
| 71 | } else { |
| 72 | await this.guilds.updateOne({ id: guild }, { |
| 73 | $addToSet: { [key]: value } |
| 74 | }, { upsert: true }); |
| 75 | } |
| 76 | } |
| 77 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 78 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 79 | async remove(guild: string, key: string, value: any, innerKey?: string | null) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 80 | console.log(Array.isArray(value)); |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 81 | if (innerKey) { |
| 82 | await this.guilds.updateOne({ id: guild }, { |
| 83 | $pull: { [key]: { [innerKey]: { $eq: value } } } |
| 84 | }, { upsert: true }); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 85 | } else if (Array.isArray(value)) { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 86 | await this.guilds.updateOne({ id: guild }, { |
| 87 | $pullAll: { [key]: value } |
| 88 | }, { upsert: true }); |
| 89 | } else { |
| 90 | await this.guilds.updateOne({ id: guild }, { |
| 91 | $pullAll: { [key]: [value] } |
| 92 | }, { upsert: true }); |
| 93 | } |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 94 | } |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 95 | |
| 96 | async delete(guild: string) { |
| 97 | await this.guilds.deleteOne({ id: guild }); |
| 98 | } |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 99 | } |
| 100 | |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 101 | |
| 102 | export class History { |
| 103 | histories: Collection<HistorySchema>; |
| 104 | defaultData: GuildConfig; |
| 105 | |
| 106 | async setup() { |
| 107 | this.histories = database.collection<HistorySchema>("history"); |
| 108 | return this; |
| 109 | } |
| 110 | |
| 111 | async create(type: string, guild: string, user: Discord.User, moderator: Discord.User | null, reason: string | null, before?: null, after?: null, amount?: null) { |
| 112 | await this.histories.insertOne({ |
| 113 | type: type, |
| 114 | guild: guild, |
| 115 | user: user.id, |
| 116 | moderator: moderator.id, |
| 117 | reason: reason, |
| 118 | occurredAt: new Date(), |
| 119 | before: before, |
| 120 | after: after, |
| 121 | amount: amount |
| 122 | }); |
| 123 | } |
| 124 | |
| 125 | async read(guild: string, user: string, year: number) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 126 | const entry = (await this.histories.find({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 127 | guild: guild, |
| 128 | user: user, |
| 129 | occurredAt: { |
| 130 | $gte: new Date(year - 1, 11, 31, 23, 59, 59), |
| 131 | $lt: new Date(year + 1, 0, 1, 0, 0, 0) |
| 132 | } |
| 133 | }).toArray()) as HistorySchema[]; |
| 134 | return entry; |
| 135 | } |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 136 | |
| 137 | async delete(guild: string) { |
| 138 | await this.histories.deleteMany({ guild: guild }); |
| 139 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | export class ModNotes { |
| 143 | modNotes: Collection<ModNoteSchema>; |
| 144 | defaultData: GuildConfig; |
| 145 | |
| 146 | async setup() { |
| 147 | this.modNotes = database.collection<ModNoteSchema>("modNotes"); |
| 148 | return this; |
| 149 | } |
| 150 | |
| 151 | async create(guild: string, user: string, note: string | null) { |
| 152 | await this.modNotes.updateOne({ guild: guild, user: user }, { $set: { note: note }}, { upsert: true }); |
| 153 | } |
| 154 | |
| 155 | async read(guild: string, user: string) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 156 | const entry = await this.modNotes.findOne({ guild: guild, user: user }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 157 | return entry?.note ?? null; |
| 158 | } |
| 159 | } |
| 160 | |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 161 | export class Premium { |
| 162 | premium: Collection<PremiumSchema>; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 163 | |
| 164 | async setup() { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 165 | this.premium = database.collection<PremiumSchema>("premium"); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 166 | return this; |
| 167 | } |
| 168 | |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 169 | async hasPremium(guild: string) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 170 | const entry = await this.premium.findOne({ appliesTo: { $in: [guild] } }); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 171 | return entry !== null; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 175 | export interface GuildConfig { |
| 176 | id: string, |
| 177 | version: number, |
| 178 | singleEventNotifications: { |
| 179 | statsChannelDeleted: boolean |
| 180 | } |
| 181 | filters: { |
| 182 | images: { |
| 183 | NSFW: boolean, |
| 184 | size: boolean |
| 185 | }, |
| 186 | malware: boolean, |
| 187 | wordFilter: { |
| 188 | enabled: boolean, |
| 189 | words: { |
| 190 | strict: string[], |
| 191 | loose: string[] |
| 192 | }, |
| 193 | allowed: { |
| 194 | users: string[], |
| 195 | roles: string[], |
| 196 | channels: string[] |
| 197 | } |
| 198 | }, |
| 199 | invite: { |
| 200 | enabled: boolean, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 201 | channels: string[] |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 202 | }, |
| 203 | pings: { |
| 204 | mass: number, |
| 205 | everyone: boolean, |
| 206 | roles: boolean, |
| 207 | allowed: { |
| 208 | roles: string[], |
| 209 | rolesToMention: string[], |
| 210 | users: string[], |
| 211 | channels: string[] |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | welcome: { |
| 216 | enabled: boolean, |
| 217 | verificationRequired: { |
| 218 | message: boolean, |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 219 | role: string | null |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 220 | }, |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 221 | role: string | null, |
| 222 | ping: string | null, |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 223 | channel: string | null, |
| 224 | message: string | null, |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 225 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 226 | stats: Record<string, {name: string, enabled: boolean}> |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 227 | logging: { |
| 228 | logs: { |
| 229 | enabled: boolean, |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 230 | channel: string | null, |
| 231 | toLog: string | null, |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 232 | }, |
| 233 | staff: { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 234 | channel: string | null, |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 235 | }, |
| 236 | attachments: { |
| 237 | channel: string | null, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 238 | saved: Record<string, string> |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | verify: { |
| 242 | enabled: boolean, |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 243 | role: string | null, |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 244 | } |
| 245 | tickets: { |
| 246 | enabled: boolean, |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 247 | category: string | null, |
| 248 | types: string | null, |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 249 | customTypes: string[], |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 250 | useCustom: boolean, |
| 251 | supportRole: string | null, |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 252 | maxTickets: number |
| 253 | } |
| 254 | moderation: { |
| 255 | mute: { |
| 256 | timeout: boolean, |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 257 | role: string | null, |
| 258 | text: string | null, |
| 259 | link: string | null |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 260 | }, |
| 261 | kick: { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 262 | text: string | null, |
| 263 | link: string | null |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 264 | }, |
| 265 | ban: { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 266 | text: string | null, |
| 267 | link: string | null |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 268 | }, |
| 269 | softban: { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 270 | text: string | null, |
| 271 | link: string | null |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 272 | }, |
| 273 | warn: { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 274 | text: string | null, |
| 275 | link: string | null |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 276 | }, |
| 277 | role: { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 278 | role: string | null, |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | tracks: { |
| 282 | name: string, |
| 283 | retainPrevious: boolean, |
| 284 | nullable: boolean, |
| 285 | track: string[], |
| 286 | manageableBy: string[] |
| 287 | }[] |
| 288 | roleMenu: { |
| 289 | enabled: boolean, |
| 290 | allowWebUI: boolean, |
| 291 | options: { |
| 292 | name: string, |
| 293 | description: string, |
| 294 | min: number, |
| 295 | max: number, |
| 296 | options: { |
| 297 | name: string, |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 298 | description: string | null, |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 299 | role: string |
| 300 | }[] |
| 301 | }[] |
| 302 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 303 | tags: Record<string, string> |
| 304 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 305 | |
| 306 | export interface HistorySchema { |
| 307 | type: string, |
| 308 | guild: string, |
| 309 | user: string, |
| 310 | moderator: string | null, |
| 311 | reason: string, |
| 312 | occurredAt: Date, |
| 313 | before: string | null, |
| 314 | after: string | null, |
| 315 | amount: string | null |
| 316 | } |
| 317 | |
| 318 | export interface ModNoteSchema { |
| 319 | guild: string, |
| 320 | user: string, |
| 321 | note: string |
| 322 | } |
| 323 | |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 324 | export interface PremiumSchema { |
| 325 | user: string, |
| 326 | level: number, |
| 327 | expires: Date, |
| 328 | appliesTo: string[] |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 329 | } |