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