diff --git a/src/structure/client/wrappers/GuildWrapper.js b/src/structure/client/wrappers/GuildWrapper.js index 0ca9ee5..302952b 100644 --- a/src/structure/client/wrappers/GuildWrapper.js +++ b/src/structure/client/wrappers/GuildWrapper.js @@ -3,6 +3,7 @@ const { Guild } = require("discord.js"); const { PollReactions } = require("../../../constants/Constants.js"); const { FilterUtil } = require("../../../utilities/index.js"); const MemberWrapper = require("./MemberWrapper.js"); +const configVersion = '3.slash.2'; class GuildWrapper { @@ -124,31 +125,56 @@ class GuildWrapper { } + async fetchData() { + if(this._data) return this._data; + const data = await this.client.mongodb.guilds.findOne({ guildId: this.id }); + if (!data) return null; + if (data._version === '3.slash') { + const keys = Object.keys(this.defaultConfig); + const settings = {}; + for (const key of keys) { + settings[key] = data[key]; + delete data[key]; + } + data.settings = settings; + data._version = configVersion;//'3.slash.2'; + await this.client.mongodb.guilds.deleteOne({ guildId: this.id }); + await this.client.mongodb.guilds.updateOne({ guildId: this.id }, data); + } + this._data = data; + return data; + } + async settings(forceFetch = false) { if (this._settings && !forceFetch) return this._settings; - const settings = await this.client.storageManager.mongodb.guilds.findOne({ guildId: this.id }); + const data = await this.fetchData() || {}; + const { settings } = data; + // const settings = await this.client.storageManager.mongodb.guilds.findOne({ guildId: this.id }); // if(this._settings instanceof Promise) this._settings = await this._settings || null; if(!settings) this._settings = { guildId: this.id, ...this.defaultConfig }; else this._settings = { ...this.defaultConfig, ...settings }; //eslint-disable-line prefer-object-spread return this._settings; } - // TODO Move settings to a settings object insteadd of being in a mess with everything else in the data object + async updateData(data) { + try { + await this.client.mongodb.guilds.updateOne({ guildId: this.id }, { _version: configVersion, ...data }); + this._data = { ...this._data, ...data }; + this._storageLog(`Database update: Settings (guild:${this.id})`); + } catch (err) { + this._storageError(err); + } + } + + // TODO Move settings to a settings object instead of being in a mess with everything else in the data object async updateSettings(settings) { if (!this._settings) await this.settings(); - try { - await this.client.storageManager.mongodb.guilds.updateOne({ - guildId: this.id - }, { _version: '3.slash', ...settings }); - this._settings = { - ...this._settings, - ...settings - }; - this._storageLog(`Database update: Settings (guild:${this.id})`); - } catch (error) { - this._storageError(error); - } + await this.updateData({ settings }); + this._settings = { + ...this._settings, + ...settings + }; } async permissions() {