pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 1 | import { Collection, Db, MongoClient } from 'mongodb'; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 2 | import structuredClone from '@ungap/structured-clone'; |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 3 | |
| 4 | |
| 5 | export const Entry = data => { |
| 6 | data = data ?? {}; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 7 | data.getKey = key => data[key] |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 8 | return { |
| 9 | get(target, prop, receiver) { |
| 10 | let dataToReturn = data[prop] |
| 11 | if (dataToReturn === null ) return Reflect.get(target, prop, receiver); |
| 12 | if (typeof dataToReturn === "object" && !Array.isArray(dataToReturn)) dataToReturn = new Proxy( |
| 13 | Reflect.get(target, prop, receiver), |
| 14 | Entry(dataToReturn), |
| 15 | ) |
| 16 | return dataToReturn ?? Reflect.get(target, prop, receiver); |
| 17 | } |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | |
| 22 | export default class Database { |
| 23 | mongoClient: MongoClient; |
| 24 | database: Db; |
| 25 | guilds: Collection<GuildConfig>; |
| 26 | defaultData: GuildConfig; |
| 27 | |
| 28 | constructor(url) { |
| 29 | this.mongoClient = new MongoClient(url); |
| 30 | } |
| 31 | |
| 32 | async connect() { |
| 33 | await this.mongoClient.connect() |
| 34 | this.database = this.mongoClient.db("Nucleus"); |
| 35 | this.guilds = this.database.collection<GuildConfig>("guilds"); |
| 36 | await this.guilds.createIndex({ id: "text" }, { unique: true }); |
| 37 | this.defaultData = (await import("../config/default.json", { assert: { type: "json" }})).default as unknown as GuildConfig; |
| 38 | return this; |
| 39 | } |
| 40 | |
| 41 | async read(guild: string) { |
| 42 | let entry = await this.guilds.findOne({ id: guild }); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 43 | return new Proxy(structuredClone(this.defaultData), Entry(entry)) as unknown as GuildConfig |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 44 | } |
| 45 | |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 46 | async write(guild: string, set: object = {}, unset: string[] = []) { |
| 47 | let uo = {} |
| 48 | for (let key of unset) { |
| 49 | uo[key] = ""; |
| 50 | } |
| 51 | await this.guilds.updateOne({ id: guild }, { |
| 52 | $unset: uo, |
| 53 | $set: set |
| 54 | }, { upsert: true }); |
| 55 | } |
| 56 | |
| 57 | async append(guild: string, key: string, value: any) { |
| 58 | if (Array.isArray(value)) { |
| 59 | await this.guilds.updateOne({ id: guild }, { |
| 60 | $addToSet: { [key]: { $each: value } } |
| 61 | }, { upsert: true }); |
| 62 | } else { |
| 63 | await this.guilds.updateOne({ id: guild }, { |
| 64 | $addToSet: { [key]: value } |
| 65 | }, { upsert: true }); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | async remove(guild: string, key: string, value: any) { |
| 70 | if (Array.isArray(value)) { |
| 71 | await this.guilds.updateOne({ id: guild }, { |
| 72 | $pullAll: { [key]: value } |
| 73 | }, { upsert: true }); |
| 74 | } else { |
| 75 | await this.guilds.updateOne({ id: guild }, { |
| 76 | $pullAll: { [key]: [value] } |
| 77 | }, { upsert: true }); |
| 78 | } |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | export interface GuildConfig { |
| 83 | id: string, |
| 84 | version: number, |
| 85 | singleEventNotifications: { |
| 86 | statsChannelDeleted: boolean |
| 87 | } |
| 88 | filters: { |
| 89 | images: { |
| 90 | NSFW: boolean, |
| 91 | size: boolean |
| 92 | }, |
| 93 | malware: boolean, |
| 94 | wordFilter: { |
| 95 | enabled: boolean, |
| 96 | words: { |
| 97 | strict: string[], |
| 98 | loose: string[] |
| 99 | }, |
| 100 | allowed: { |
| 101 | users: string[], |
| 102 | roles: string[], |
| 103 | channels: string[] |
| 104 | } |
| 105 | }, |
| 106 | invite: { |
| 107 | enabled: boolean, |
| 108 | allowed: { |
| 109 | users: string[], |
| 110 | channels: string[], |
| 111 | roles: string[] |
| 112 | } |
| 113 | }, |
| 114 | pings: { |
| 115 | mass: number, |
| 116 | everyone: boolean, |
| 117 | roles: boolean, |
| 118 | allowed: { |
| 119 | roles: string[], |
| 120 | rolesToMention: string[], |
| 121 | users: string[], |
| 122 | channels: string[] |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | welcome: { |
| 127 | enabled: boolean, |
| 128 | verificationRequired: { |
| 129 | message: boolean, |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 130 | role: string | null |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 131 | }, |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 132 | welcomeRole: string | null, |
| 133 | channel: string | null, |
| 134 | message: string | null, |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 135 | } |
| 136 | stats: { |
| 137 | enabled: boolean, |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 138 | channel: string | null, |
| 139 | text: string | null, |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 140 | }[] |
| 141 | logging: { |
| 142 | logs: { |
| 143 | enabled: boolean, |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 144 | channel: string | null, |
| 145 | toLog: string | null, |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 146 | }, |
| 147 | staff: { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 148 | channel: string | null, |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | verify: { |
| 152 | enabled: boolean, |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 153 | role: string | null, |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 154 | } |
| 155 | tickets: { |
| 156 | enabled: boolean, |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 157 | category: string | null, |
| 158 | types: string | null, |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 159 | customTypes: string[], |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 160 | useCustom: boolean, |
| 161 | supportRole: string | null, |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 162 | maxTickets: number |
| 163 | } |
| 164 | moderation: { |
| 165 | mute: { |
| 166 | timeout: boolean, |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 167 | role: string | null, |
| 168 | text: string | null, |
| 169 | link: string | null |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 170 | }, |
| 171 | kick: { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 172 | text: string | null, |
| 173 | link: string | null |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 174 | }, |
| 175 | ban: { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 176 | text: string | null, |
| 177 | link: string | null |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 178 | }, |
| 179 | softban: { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 180 | text: string | null, |
| 181 | link: string | null |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 182 | }, |
| 183 | warn: { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 184 | text: string | null, |
| 185 | link: string | null |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 186 | }, |
| 187 | role: { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 188 | role: string | null, |
pineafan | 6fb3e07 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | tracks: { |
| 192 | name: string, |
| 193 | retainPrevious: boolean, |
| 194 | nullable: boolean, |
| 195 | track: string[], |
| 196 | manageableBy: string[] |
| 197 | }[] |
| 198 | roleMenu: { |
| 199 | enabled: boolean, |
| 200 | allowWebUI: boolean, |
| 201 | options: { |
| 202 | name: string, |
| 203 | description: string, |
| 204 | min: number, |
| 205 | max: number, |
| 206 | options: { |
| 207 | name: string, |
| 208 | description: string, |
| 209 | role: string |
| 210 | }[] |
| 211 | }[] |
| 212 | } |
| 213 | tags: {} |
| 214 | }; |