diff --git a/src/structure/client/wrappers/GuildWrapper.js b/src/structure/client/wrappers/GuildWrapper.js index 05df641..cb6433a 100644 --- a/src/structure/client/wrappers/GuildWrapper.js +++ b/src/structure/client/wrappers/GuildWrapper.js @@ -1,10 +1,12 @@ const { default: Collection } = require("@discordjs/collection"); const { Guild } = require("discord.js"); const { PollReactions, EmbedDefaultColor } = require("../../../constants/Constants.js"); -const { FilterUtil } = require("../../../utilities/index.js"); +const { FilterUtil, SettingsMigrator, InfractionMigrator } = require("../../../utilities/index.js"); const MemberWrapper = require("./MemberWrapper.js"); const configVersion = '3.slash.2'; +const { MONGODB_V2_HOST } = process.env; + class GuildWrapper { constructor(client, guild) { @@ -152,9 +154,14 @@ class GuildWrapper { if (this._settings && !forceFetch) return this._settings; const data = await this.fetchData(); - const { settings } = data; + // eslint-disable-next-line prefer-const + let { settings, _imported } = data; const { defaultConfig } = this; + if (!settings && !_imported?.settings && !_imported?.modlogs) { + settings = await this._attemptDataImport(); + } + if (settings) { // Ensure new settings properties are propagated to existing configs const keys = Object.keys(settings); @@ -208,6 +215,58 @@ class GuildWrapper { return true; } + async _attemptDataImport() { + const migratorOptions = { + host: MONGODB_V2_HOST, + database: 'galacticbot', + version: '2' + }; + + const settingsMigrator = new SettingsMigrator(this.client, this, migratorOptions); + const modlogsMigrator = new InfractionMigrator(this.client, this, migratorOptions); + + await settingsMigrator.connect(); + await modlogsMigrator.connect(); + + let importedSettings = null; + let importedModlogs = null; + + try { + importedSettings = await settingsMigrator.migrate(); + importedModlogs = await modlogsMigrator.migrate(); + importedModlogs.sort((a, b) => a.case - b.case); + } catch (err) { + await settingsMigrator.end(); + await modlogsMigrator.end(); + if (!err.message.includes('No old')) this.client.logger.error(err.stack); + return null; + } + await settingsMigrator.end(); + await modlogsMigrator.end(); + + await this.client.mongodb.infractions.insertMany(importedModlogs); + this._data.caseId = importedModlogs[importedModlogs.length - 1].case; + await this.updateData({ + caseId: this._data.caseId, premium: importedSettings.premium, + _imported: { settings: true, modlogs: true } + }); + + const { webhook, permissions, settings } = importedSettings; + if (webhook) { + const hooks = await this.fetchWebhooks(); + const hook = hooks.get(webhook); + if (hook) + await this.updateWebhook('messages', hook); + } + + if (permissions) await this.client.storageManager.mongodb.permissions.updateOne({ guildId: this.id }, permissions); + await this.updateSettings(settings); + + + return settings; + + } + /** * Update a webhook entry in the database * diff --git a/src/structure/storage/providers/Mongodb.js b/src/structure/storage/providers/Mongodb.js index cbda31e..c0ab2c1 100644 --- a/src/structure/storage/providers/Mongodb.js +++ b/src/structure/storage/providers/Mongodb.js @@ -10,8 +10,9 @@ class MongoDBProvider extends Provider { config }); - const { MONGODB_HOST, MONGODB_USERNAME, MONGODB_PASSWORD } = process.env; - const { database } = this.config; + const { MONGODB_HOST, MONGODB_USERNAME, MONGODB_PASSWORD, MONGODB_DATABASE } = process.env; + const database = MONGODB_DATABASE; + // const { database } = this.config; const auth = MONGODB_USERNAME ? `${MONGODB_USERNAME}:${MONGODB_PASSWORD}@`:''; this.URI = `mongodb://${auth}${MONGODB_HOST}/${database}?authSource=${database}`; diff --git a/src/utilities/InfractionMigrator.js b/src/utilities/InfractionMigrator.js index dba3c20..96d8069 100644 --- a/src/utilities/InfractionMigrator.js +++ b/src/utilities/InfractionMigrator.js @@ -54,7 +54,7 @@ class InfractionMigrator { const filter = { [idIdentifier]: this.guild }; const infractions = await this.mongo.find(collection, filter); - if (!infractions.length) return Promise.reject(new Error('No infractions found')); + if (!infractions.length) return Promise.reject(new Error('No old infractions found')); const translated = this[version](infractions); return translated;