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