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