huge changes once again
diff --git a/src/utils/database.ts b/src/utils/database.ts
index 4e37652..66d7e67 100644
--- a/src/utils/database.ts
+++ b/src/utils/database.ts
@@ -1,5 +1,12 @@
-import { Collection, Db, MongoClient } from 'mongodb';
+import Discord from 'discord.js';
+import { Collection, MongoClient } from 'mongodb';
import structuredClone from '@ungap/structured-clone';
+import config from '../config/main.json' assert {type: 'json'};
+
+
+const mongoClient = new MongoClient(config.mongoUrl);
+await mongoClient.connect()
+const database = mongoClient.db("Nucleus");
export const Entry = data => {
@@ -19,21 +26,11 @@
}
-export default class Database {
- mongoClient: MongoClient;
- database: Db;
+export class Guilds {
guilds: Collection<GuildConfig>;
defaultData: GuildConfig;
-
- constructor(url) {
- this.mongoClient = new MongoClient(url);
- }
-
- async connect() {
- await this.mongoClient.connect()
- this.database = this.mongoClient.db("Nucleus");
- this.guilds = this.database.collection<GuildConfig>("guilds");
- await this.guilds.createIndex({ id: "text" }, { unique: true });
+ async setup() {
+ this.guilds = database.collection<GuildConfig>("guilds");
this.defaultData = (await import("../config/default.json", { assert: { type: "json" }})).default as unknown as GuildConfig;
return this;
}
@@ -79,6 +76,85 @@
}
}
+
+export class History {
+ histories: Collection<HistorySchema>;
+ defaultData: GuildConfig;
+
+ async setup() {
+ this.histories = database.collection<HistorySchema>("history");
+ return this;
+ }
+
+ async create(type: string, guild: string, user: Discord.User, moderator: Discord.User | null, reason: string | null, before?: null, after?: null, amount?: null) {
+ await this.histories.insertOne({
+ type: type,
+ guild: guild,
+ user: user.id,
+ moderator: moderator.id,
+ reason: reason,
+ occurredAt: new Date(),
+ before: before,
+ after: after,
+ amount: amount
+ });
+ }
+
+ async read(guild: string, user: string, year: number) {
+ let entry = (await this.histories.find({
+ guild: guild,
+ user: user,
+ occurredAt: {
+ $gte: new Date(year - 1, 11, 31, 23, 59, 59),
+ $lt: new Date(year + 1, 0, 1, 0, 0, 0)
+ }
+ }).toArray()) as HistorySchema[];
+ return entry;
+ }
+}
+
+export class ModNotes {
+ modNotes: Collection<ModNoteSchema>;
+ defaultData: GuildConfig;
+
+ async setup() {
+ this.modNotes = database.collection<ModNoteSchema>("modNotes");
+ return this;
+ }
+
+ async create(guild: string, user: string, note: string | null) {
+ await this.modNotes.updateOne({ guild: guild, user: user }, { $set: { note: note }}, { upsert: true });
+ }
+
+ async read(guild: string, user: string) {
+ let entry = await this.modNotes.findOne({ guild: guild, user: user });
+ return entry?.note ?? null;
+ }
+}
+
+export class EventSchedulerDatabase {
+ events: Collection<EventSchedulerSchema>;
+ defaultData: GuildConfig;
+
+ async setup() {
+ this.events = database.collection<EventSchedulerSchema>("eventScheduler");
+ return this;
+ }
+
+ async create(timestamp: Date, data: object) {
+ await this.events.insertOne({ timestamp: timestamp, data: data});
+ }
+
+ async getNext() {
+ let entry = await this.events.findOne({ timestamp: { $lte: new Date() }});
+ return entry;
+ }
+
+ async remove(timestamp: Date, data: object) {
+ await this.events.deleteOne({ timestamp: timestamp, data: data});
+ }
+}
+
export interface GuildConfig {
id: string,
version: number,
@@ -205,10 +281,33 @@
max: number,
options: {
name: string,
- description: string,
+ description: string | null,
role: string
}[]
}[]
}
tags: {}
-};
\ No newline at end of file
+};
+
+export interface HistorySchema {
+ type: string,
+ guild: string,
+ user: string,
+ moderator: string | null,
+ reason: string,
+ occurredAt: Date,
+ before: string | null,
+ after: string | null,
+ amount: string | null
+}
+
+export interface ModNoteSchema {
+ guild: string,
+ user: string,
+ note: string
+}
+
+export interface EventSchedulerSchema {
+ timestamp: Date,
+ data: object
+}
\ No newline at end of file