huge changes once again
diff --git a/src/utils/confirmationMessage.ts b/src/utils/confirmationMessage.ts
index dbcd52f..7eaa369 100644
--- a/src/utils/confirmationMessage.ts
+++ b/src/utils/confirmationMessage.ts
@@ -1,5 +1,5 @@
import Discord, { CommandInteraction, MessageActionRow, Message } from "discord.js";
-import generateEmojiEmbed from "./generateEmojiEmbed.js"
+import EmojiEmbed from "./generateEmojiEmbed.js"
import getEmojiByName from "./getEmojiByName.js";
class confirmationMessage {
@@ -58,7 +58,7 @@
while (true) {
let object = {
embeds: [
- new generateEmojiEmbed()
+ new EmojiEmbed()
.setEmoji(this.emoji)
.setTitle(this.title)
.setDescription(this.description)
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
diff --git a/src/utils/dualCollector.ts b/src/utils/dualCollector.ts
index 902822a..67cef5c 100644
--- a/src/utils/dualCollector.ts
+++ b/src/utils/dualCollector.ts
@@ -1,6 +1,6 @@
import Discord from 'discord.js';
import client from './client.js';
-import generateEmojiEmbed from "./generateEmojiEmbed.js";
+import EmojiEmbed from "./generateEmojiEmbed.js";
export default async function (m, interactionFilter, messageFilter) {
let out;
diff --git a/src/utils/eventScheduler.ts b/src/utils/eventScheduler.ts
new file mode 100644
index 0000000..9210883
--- /dev/null
+++ b/src/utils/eventScheduler.ts
@@ -0,0 +1,37 @@
+import { EventSchedulerDatabase } from './database';
+import client from './client.js';
+
+class EventScheduler {
+ database: any;
+ next: {timestamp: Date, data: object, responded: boolean} | {};
+
+ constructor() {
+ this.database = EventSchedulerDatabase;
+ this.next = {};
+ }
+
+ async create(timestamp: Date, data: object) {
+ await this.database.create(timestamp, data);
+ if (this.next === {}) {
+ this.next = this.next = await this.getNext();
+ return
+ }
+ if (timestamp.getTime() < (this.next as {timestamp: Date}).timestamp.getTime()) {
+ this.next = {timestamp: timestamp, data: data, responded: false};
+ }
+ }
+
+ async getNext() {
+ let entry = await this.database.getNext();
+ if (entry) {
+ this.next = entry;
+ }
+ return this.next;
+ }
+
+ async delete(timestamp: Date, data: object) {
+ await this.database.delete(timestamp, data);
+ } // TODO: add a loop
+}
+
+export default EventScheduler;
\ No newline at end of file
diff --git a/src/utils/log.ts b/src/utils/log.ts
index 19eb2b6..55f8f4c 100644
--- a/src/utils/log.ts
+++ b/src/utils/log.ts
@@ -53,7 +53,7 @@
}
async log(log: any): Promise<void> {
- let config = await client.database.read(log.hidden.guild);
+ let config = await client.database.guilds.read(log.hidden.guild);
if (!config.logging.logs.enabled) return;
if (!(log.meta.calculateType == true)) {
if(!toHexArray(config.logging.logs.toLog).includes(log.meta.calculateType)) return console.log('Not logging this type of event');
diff --git a/src/utils/memory.ts b/src/utils/memory.ts
index 7e21fa9..a06705b 100644
--- a/src/utils/memory.ts
+++ b/src/utils/memory.ts
@@ -16,7 +16,7 @@
async readGuildInfo(guild: string): Promise<object> {
if (!this.memory[guild]) {
- let guildData = await client.database.read(guild);
+ let guildData = await client.database.guilds.read(guild);
this.memory[guild] = {
lastUpdated: Date.now(),
filters: guildData.filters,
diff --git a/src/utils/singleNotify.ts b/src/utils/singleNotify.ts
index a983478..051ec4d 100644
--- a/src/utils/singleNotify.ts
+++ b/src/utils/singleNotify.ts
@@ -1,5 +1,5 @@
import client from './client.js';
-import generateEmojiEmbed from "./generateEmojiEmbed.js";
+import EmojiEmbed from "./generateEmojiEmbed.js";
let severities = {
"Critical": "Danger",
@@ -8,14 +8,14 @@
}
export default async function(type: string, guild: string, message: string, severity: string) {
- let data = await client.database.read(guild);
+ let data = await client.database.guilds.read(guild);
if (data.singleEventNotifications[type]) return;
data.singleEventNotifications[type] = true;
- client.database.write(guild, data);
+ client.database.guilds.write(guild, data);
try {
let channel = await client.channels.fetch(data.logging.staff.channel);
if (!channel) return;
- await channel.send({embeds: [new generateEmojiEmbed()
+ await channel.send({embeds: [new EmojiEmbed()
.setTitle(`${severity} notification`)
.setDescription(message)
.setStatus(severities[severity])