blob: cda63d642c2eea70a84661a161ac61c2356c5b5d [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import type Discord from "discord.js";
2import { Collection, MongoClient } from "mongodb";
Skyler Grey75ea9172022-08-06 10:22:23 +01003// @ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01004import structuredClone from "@ungap/structured-clone";
Skyler Grey75ea9172022-08-06 10:22:23 +01005import config from "../config/main.json" assert { type: "json" };
pineafan4edb7762022-06-26 19:21:04 +01006
7const mongoClient = new MongoClient(config.mongoUrl);
pineafan63fc5e22022-08-04 22:04:10 +01008await mongoClient.connect();
pineafan4edb7762022-06-26 19:21:04 +01009const database = mongoClient.db("Nucleus");
pineafan6fb3e072022-05-20 19:27:23 +010010
pineafan63fc5e22022-08-04 22:04:10 +010011// eslint-disable-next-line @typescript-eslint/no-explicit-any
12export const Entry = (data: any) => {
pineafan6fb3e072022-05-20 19:27:23 +010013 data = data ?? {};
pineafan63fc5e22022-08-04 22:04:10 +010014 // eslint-disable-next-line @typescript-eslint/no-explicit-any
15 data.getKey = (key: any) => data[key];
pineafan6fb3e072022-05-20 19:27:23 +010016 return {
pineafan63fc5e22022-08-04 22:04:10 +010017 // eslint-disable-next-line @typescript-eslint/no-explicit-any
18 get(target: Record<string, any>, prop: string, receiver: any) {
19 let dataToReturn = data[prop];
Skyler Grey11236ba2022-08-08 21:13:33 +010020 if (dataToReturn === null) return Reflect.get(target, prop, receiver);
21 if (typeof dataToReturn === "object" && !Array.isArray(dataToReturn))
22 dataToReturn = new Proxy(Reflect.get(target, prop, receiver), Entry(dataToReturn));
pineafan6fb3e072022-05-20 19:27:23 +010023 return dataToReturn ?? Reflect.get(target, prop, receiver);
24 }
pineafan63fc5e22022-08-04 22:04:10 +010025 };
26};
pineafan6fb3e072022-05-20 19:27:23 +010027
pineafan4edb7762022-06-26 19:21:04 +010028export class Guilds {
pineafan6fb3e072022-05-20 19:27:23 +010029 guilds: Collection<GuildConfig>;
pineafan63fc5e22022-08-04 22:04:10 +010030 defaultData: GuildConfig | null;
31
32 constructor() {
pineafan4edb7762022-06-26 19:21:04 +010033 this.guilds = database.collection<GuildConfig>("guilds");
pineafan63fc5e22022-08-04 22:04:10 +010034 this.defaultData = null;
pineafan6fb3e072022-05-20 19:27:23 +010035 }
36
pineafan63fc5e22022-08-04 22:04:10 +010037 async setup() {
Skyler Grey11236ba2022-08-08 21:13:33 +010038 this.defaultData = (await import("../config/default.json", { assert: { type: "json" } }))
39 .default as unknown as GuildConfig;
40 return this;
pineafan63fc5e22022-08-04 22:04:10 +010041 }
42
pineafan6fb3e072022-05-20 19:27:23 +010043 async read(guild: string) {
pineafan63fc5e22022-08-04 22:04:10 +010044 const entry = await this.guilds.findOne({ id: guild });
Skyler Grey11236ba2022-08-08 21:13:33 +010045 return new Proxy(structuredClone(this.defaultData), Entry(entry)) as unknown as GuildConfig;
pineafan6fb3e072022-05-20 19:27:23 +010046 }
47
Skyler Grey11236ba2022-08-08 21:13:33 +010048 async write(guild: string, set: object | null, unset: string[] | string = []) {
pineafan63fc5e22022-08-04 22:04:10 +010049 // eslint-disable-next-line @typescript-eslint/no-explicit-any
50 const uo: Record<string, any> = {};
51 if (!Array.isArray(unset)) unset = [unset];
52 for (const key of unset) {
pineafan0bc04162022-07-25 17:22:26 +010053 uo[key] = null;
pineafan6702cef2022-06-13 17:52:37 +010054 }
Skyler Grey75ea9172022-08-06 10:22:23 +010055 const out = { $set: {}, $unset: {} };
56 if (set) out.$set = set;
57 if (unset.length) out.$unset = uo;
pineafan0bc04162022-07-25 17:22:26 +010058 await this.guilds.updateOne({ id: guild }, out, { upsert: true });
pineafan6702cef2022-06-13 17:52:37 +010059 }
60
pineafan63fc5e22022-08-04 22:04:10 +010061 // eslint-disable-next-line @typescript-eslint/no-explicit-any
pineafan6702cef2022-06-13 17:52:37 +010062 async append(guild: string, key: string, value: any) {
63 if (Array.isArray(value)) {
Skyler Grey75ea9172022-08-06 10:22:23 +010064 await this.guilds.updateOne(
65 { id: guild },
66 {
67 $addToSet: { [key]: { $each: value } }
68 },
69 { upsert: true }
70 );
pineafan6702cef2022-06-13 17:52:37 +010071 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +010072 await this.guilds.updateOne(
73 { id: guild },
74 {
75 $addToSet: { [key]: value }
76 },
77 { upsert: true }
78 );
pineafan6702cef2022-06-13 17:52:37 +010079 }
80 }
81
Skyler Grey75ea9172022-08-06 10:22:23 +010082 async remove(
83 guild: string,
84 key: string,
Skyler Greyc634e2b2022-08-06 17:50:48 +010085 // eslint-disable-next-line @typescript-eslint/no-explicit-any
Skyler Grey75ea9172022-08-06 10:22:23 +010086 value: any,
87 innerKey?: string | null
88 ) {
pineafan63fc5e22022-08-04 22:04:10 +010089 console.log(Array.isArray(value));
pineafan02ba0232022-07-24 22:16:15 +010090 if (innerKey) {
Skyler Grey75ea9172022-08-06 10:22:23 +010091 await this.guilds.updateOne(
92 { id: guild },
93 {
94 $pull: { [key]: { [innerKey]: { $eq: value } } }
95 },
96 { upsert: true }
97 );
pineafan0bc04162022-07-25 17:22:26 +010098 } else if (Array.isArray(value)) {
Skyler Grey75ea9172022-08-06 10:22:23 +010099 await this.guilds.updateOne(
100 { id: guild },
101 {
102 $pullAll: { [key]: value }
103 },
104 { upsert: true }
105 );
pineafan6702cef2022-06-13 17:52:37 +0100106 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100107 await this.guilds.updateOne(
108 { id: guild },
109 {
110 $pullAll: { [key]: [value] }
111 },
112 { upsert: true }
113 );
pineafan6702cef2022-06-13 17:52:37 +0100114 }
pineafan6fb3e072022-05-20 19:27:23 +0100115 }
pineafane23c4ec2022-07-27 21:56:27 +0100116
117 async delete(guild: string) {
118 await this.guilds.deleteOne({ id: guild });
119 }
pineafan6fb3e072022-05-20 19:27:23 +0100120}
121
pineafan4edb7762022-06-26 19:21:04 +0100122export class History {
123 histories: Collection<HistorySchema>;
pineafan4edb7762022-06-26 19:21:04 +0100124
pineafan3a02ea32022-08-11 21:35:04 +0100125 constructor() {
pineafan4edb7762022-06-26 19:21:04 +0100126 this.histories = database.collection<HistorySchema>("history");
pineafan4edb7762022-06-26 19:21:04 +0100127 }
128
Skyler Grey75ea9172022-08-06 10:22:23 +0100129 async create(
130 type: string,
131 guild: string,
132 user: Discord.User,
133 moderator: Discord.User | null,
134 reason: string | null,
pineafan3a02ea32022-08-11 21:35:04 +0100135 before?: string | null,
136 after?: string | null,
137 amount?: string | null
Skyler Grey75ea9172022-08-06 10:22:23 +0100138 ) {
pineafan4edb7762022-06-26 19:21:04 +0100139 await this.histories.insertOne({
140 type: type,
141 guild: guild,
142 user: user.id,
pineafan3a02ea32022-08-11 21:35:04 +0100143 moderator: moderator ? moderator.id : null,
pineafan4edb7762022-06-26 19:21:04 +0100144 reason: reason,
145 occurredAt: new Date(),
pineafan3a02ea32022-08-11 21:35:04 +0100146 before: before ?? null,
147 after: after ?? null,
148 amount: amount ?? null
pineafan4edb7762022-06-26 19:21:04 +0100149 });
150 }
151
152 async read(guild: string, user: string, year: number) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100153 const entry = (await this.histories
154 .find({
155 guild: guild,
156 user: user,
157 occurredAt: {
158 $gte: new Date(year - 1, 11, 31, 23, 59, 59),
159 $lt: new Date(year + 1, 0, 1, 0, 0, 0)
160 }
161 })
162 .toArray()) as HistorySchema[];
pineafan4edb7762022-06-26 19:21:04 +0100163 return entry;
164 }
pineafane23c4ec2022-07-27 21:56:27 +0100165
166 async delete(guild: string) {
167 await this.histories.deleteMany({ guild: guild });
168 }
pineafan4edb7762022-06-26 19:21:04 +0100169}
170
171export class ModNotes {
172 modNotes: Collection<ModNoteSchema>;
pineafan4edb7762022-06-26 19:21:04 +0100173
pineafan3a02ea32022-08-11 21:35:04 +0100174 constructor() {
pineafan4edb7762022-06-26 19:21:04 +0100175 this.modNotes = database.collection<ModNoteSchema>("modNotes");
pineafan4edb7762022-06-26 19:21:04 +0100176 }
177
178 async create(guild: string, user: string, note: string | null) {
Skyler Grey11236ba2022-08-08 21:13:33 +0100179 await this.modNotes.updateOne({ guild: guild, user: user }, { $set: { note: note } }, { upsert: true });
pineafan4edb7762022-06-26 19:21:04 +0100180 }
181
182 async read(guild: string, user: string) {
pineafan63fc5e22022-08-04 22:04:10 +0100183 const entry = await this.modNotes.findOne({ guild: guild, user: user });
pineafan4edb7762022-06-26 19:21:04 +0100184 return entry?.note ?? null;
185 }
186}
187
pineafan73a7c4a2022-07-24 10:38:04 +0100188export class Premium {
189 premium: Collection<PremiumSchema>;
pineafan4edb7762022-06-26 19:21:04 +0100190
pineafan3a02ea32022-08-11 21:35:04 +0100191 constructor() {
pineafan73a7c4a2022-07-24 10:38:04 +0100192 this.premium = database.collection<PremiumSchema>("premium");
pineafan4edb7762022-06-26 19:21:04 +0100193 }
194
pineafan73a7c4a2022-07-24 10:38:04 +0100195 async hasPremium(guild: string) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100196 const entry = await this.premium.findOne({
197 appliesTo: { $in: [guild] }
198 });
pineafane23c4ec2022-07-27 21:56:27 +0100199 return entry !== null;
pineafan4edb7762022-06-26 19:21:04 +0100200 }
201}
202
pineafan6fb3e072022-05-20 19:27:23 +0100203export interface GuildConfig {
Skyler Grey75ea9172022-08-06 10:22:23 +0100204 id: string;
205 version: number;
pineafan6fb3e072022-05-20 19:27:23 +0100206 singleEventNotifications: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100207 statsChannelDeleted: boolean;
208 };
pineafan6fb3e072022-05-20 19:27:23 +0100209 filters: {
210 images: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100211 NSFW: boolean;
212 size: boolean;
213 };
214 malware: boolean;
pineafan6fb3e072022-05-20 19:27:23 +0100215 wordFilter: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100216 enabled: boolean;
pineafan6fb3e072022-05-20 19:27:23 +0100217 words: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100218 strict: string[];
219 loose: string[];
220 };
pineafan6fb3e072022-05-20 19:27:23 +0100221 allowed: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100222 users: string[];
223 roles: string[];
224 channels: string[];
225 };
226 };
pineafan6fb3e072022-05-20 19:27:23 +0100227 invite: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100228 enabled: boolean;
229 channels: string[];
230 };
pineafan6fb3e072022-05-20 19:27:23 +0100231 pings: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100232 mass: number;
233 everyone: boolean;
234 roles: boolean;
pineafan6fb3e072022-05-20 19:27:23 +0100235 allowed: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100236 roles: string[];
237 rolesToMention: string[];
238 users: string[];
239 channels: string[];
240 };
241 };
242 };
pineafan6fb3e072022-05-20 19:27:23 +0100243 welcome: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100244 enabled: boolean;
pineafan6fb3e072022-05-20 19:27:23 +0100245 verificationRequired: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100246 message: boolean;
247 role: string | null;
248 };
249 role: string | null;
250 ping: string | null;
251 channel: string | null;
252 message: string | null;
253 };
254 stats: Record<string, { name: string; enabled: boolean }>;
pineafan6fb3e072022-05-20 19:27:23 +0100255 logging: {
256 logs: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100257 enabled: boolean;
258 channel: string | null;
259 toLog: string | null;
260 };
pineafan6fb3e072022-05-20 19:27:23 +0100261 staff: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100262 channel: string | null;
263 };
pineafan73a7c4a2022-07-24 10:38:04 +0100264 attachments: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100265 channel: string | null;
266 saved: Record<string, string>;
267 };
268 };
pineafan6fb3e072022-05-20 19:27:23 +0100269 verify: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100270 enabled: boolean;
271 role: string | null;
272 };
pineafan6fb3e072022-05-20 19:27:23 +0100273 tickets: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100274 enabled: boolean;
275 category: string | null;
276 types: string | null;
277 customTypes: string[];
278 useCustom: boolean;
279 supportRole: string | null;
280 maxTickets: number;
281 };
pineafan6fb3e072022-05-20 19:27:23 +0100282 moderation: {
283 mute: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100284 timeout: boolean;
285 role: string | null;
286 text: string | null;
287 link: string | null;
288 };
pineafan6fb3e072022-05-20 19:27:23 +0100289 kick: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100290 text: string | null;
291 link: string | null;
292 };
pineafan6fb3e072022-05-20 19:27:23 +0100293 ban: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100294 text: string | null;
295 link: string | null;
296 };
pineafan6fb3e072022-05-20 19:27:23 +0100297 softban: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100298 text: string | null;
299 link: string | null;
300 };
pineafan6fb3e072022-05-20 19:27:23 +0100301 warn: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100302 text: string | null;
303 link: string | null;
304 };
pineafan6fb3e072022-05-20 19:27:23 +0100305 role: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100306 role: string | null;
307 };
308 };
pineafan6fb3e072022-05-20 19:27:23 +0100309 tracks: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100310 name: string;
311 retainPrevious: boolean;
312 nullable: boolean;
313 track: string[];
314 manageableBy: string[];
315 }[];
pineafan6fb3e072022-05-20 19:27:23 +0100316 roleMenu: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100317 enabled: boolean;
318 allowWebUI: boolean;
pineafan6fb3e072022-05-20 19:27:23 +0100319 options: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100320 name: string;
321 description: string;
322 min: number;
323 max: number;
pineafan6fb3e072022-05-20 19:27:23 +0100324 options: {
Skyler Grey75ea9172022-08-06 10:22:23 +0100325 name: string;
326 description: string | null;
327 role: string;
328 }[];
329 }[];
330 };
331 tags: Record<string, string>;
pineafan63fc5e22022-08-04 22:04:10 +0100332}
pineafan4edb7762022-06-26 19:21:04 +0100333
334export interface HistorySchema {
Skyler Grey75ea9172022-08-06 10:22:23 +0100335 type: string;
336 guild: string;
337 user: string;
338 moderator: string | null;
pineafan3a02ea32022-08-11 21:35:04 +0100339 reason: string | null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100340 occurredAt: Date;
341 before: string | null;
342 after: string | null;
343 amount: string | null;
pineafan4edb7762022-06-26 19:21:04 +0100344}
345
346export interface ModNoteSchema {
Skyler Grey75ea9172022-08-06 10:22:23 +0100347 guild: string;
348 user: string;
pineafan3a02ea32022-08-11 21:35:04 +0100349 note: string | null;
pineafan4edb7762022-06-26 19:21:04 +0100350}
351
pineafan73a7c4a2022-07-24 10:38:04 +0100352export interface PremiumSchema {
Skyler Grey75ea9172022-08-06 10:22:23 +0100353 user: string;
354 level: number;
355 expires: Date;
356 appliesTo: string[];
357}