changed guild data structure

This commit is contained in:
Erik 2022-06-18 14:01:23 +03:00
parent 40e37fd106
commit 3786cc2b40
Signed by: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB

View File

@ -3,6 +3,7 @@ const { Guild } = require("discord.js");
const { PollReactions } = require("../../../constants/Constants.js"); const { PollReactions } = require("../../../constants/Constants.js");
const { FilterUtil } = require("../../../utilities/index.js"); const { FilterUtil } = require("../../../utilities/index.js");
const MemberWrapper = require("./MemberWrapper.js"); const MemberWrapper = require("./MemberWrapper.js");
const configVersion = '3.slash.2';
class GuildWrapper { 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) { async settings(forceFetch = false) {
if (this._settings && !forceFetch) return this._settings; 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(this._settings instanceof Promise) this._settings = await this._settings || null;
if(!settings) this._settings = { guildId: this.id, ...this.defaultConfig }; if(!settings) this._settings = { guildId: this.id, ...this.defaultConfig };
else this._settings = { ...this.defaultConfig, ...settings }; //eslint-disable-line prefer-object-spread else this._settings = { ...this.defaultConfig, ...settings }; //eslint-disable-line prefer-object-spread
return this._settings; 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) { async updateSettings(settings) {
if (!this._settings) await this.settings(); if (!this._settings) await this.settings();
try { await this.updateData({ settings });
await this.client.storageManager.mongodb.guilds.updateOne({
guildId: this.id
}, { _version: '3.slash', ...settings });
this._settings = { this._settings = {
...this._settings, ...this._settings,
...settings ...settings
}; };
this._storageLog(`Database update: Settings (guild:${this.id})`);
} catch (error) {
this._storageError(error);
}
} }
async permissions() { async permissions() {