const { connect } = require("mongoose").set('strictQuery', true); const color = require("../functions/colorCodes") const fs = require('fs'); const path = require('path'); module.exports = class DatabaseHandler { constructor(connectionString) { this.cache = new Map(); this.connectionString = connectionString; this.models = {}; this.initializeModels(); } initializeModels() { const modelsPath = path.join(__dirname, '../models'); const modelFiles = fs.readdirSync(modelsPath).filter(file => file.endsWith('.js')); for (const file of modelFiles) { const modelName = path.parse(file).name; this.models[modelName] = require(path.join(modelsPath, file)); } } cacheSweeper(client) { setInterval(() => { const guilds = this.cache.values(); for (const g of guilds) { if (!client?.servers?.has(g?.id)) { this.cache.delete(g?.id); } } }, 60 * 60 * 1000); } guildSweeper(client) { setInterval(async () => { const guilds = await this.getAll(); let i = 0; for (const g of guilds) { setTimeout(async () => { i++; if (!client?.servers?.has(g?.id)) { this.cache.delete(g?.id) this.deleteGuild(g?.id); } }, i * 600) } }, 180 * 180 * 1000); } async connectToDatabase() { await connect(this.connectionString, { useNewUrlParser: true, }).catch((err) => { console.log(color("%", `%4[Mongoose]%7 :: ${err}`)); }).then(() => console.log( color("%", "%6[Mongoose]%7 :: Connected to MongoDB"), )); } async fetchGuild(guildId, createIfNotFound = false) { const fetched = await this.models.guilds.findOne({ id: guildId }); if (fetched) return fetched; if (!fetched && createIfNotFound) { await this.models.guilds.create({ id: guildId, language: 'en_EN', botJoined: Date.now() / 1000 | 0, }); return this.models.guilds.findOne({ id: guildId }); } return null; } async getGuild(guildId, createIfNotFound = true, force = false) { if (force) return this.fetchGuild(guildId, createIfNotFound); if (this.cache.has(guildId)) { return this.cache.get(guildId); } const fetched = await this.fetchGuild(guildId, createIfNotFound); if (fetched) { this.cache.set(guildId, fetched?.toObject() ?? fetched); return this.cache.get(guildId); } return null; } async deleteGuild(guildId, onlyCache = false) { if (this.cache.has(guildId)) this.cache.delete(guildId); return !onlyCache ? this.models.guilds.deleteMany({ id: guildId }) : true; } async updateGuild(guildId, data = {}, createIfNotFound = false) { let oldData = await this.getGuild(guildId, createIfNotFound); if (oldData) { if (oldData?._doc) oldData = oldData?._doc; data = { ...oldData, ...data }; this.cache.set(guildId, data); return this.models.guilds.updateOne({ id: guildId, }, data); } return null; } async getAll() { return this.models.guilds.find(); } };