diff --git a/src/structure/client/wrappers/GuildWrapper.js b/src/structure/client/wrappers/GuildWrapper.js index 10a8913..b06e1c3 100644 --- a/src/structure/client/wrappers/GuildWrapper.js +++ b/src/structure/client/wrappers/GuildWrapper.js @@ -114,7 +114,7 @@ class GuildWrapper { const result = await this.client.storageManager.mongodb.webhooks.findOne({ feature, guild: this.id }); if (!result) return null; if (!this.me.permissions.has('MANAGE_WEBHOOKS')) throw new Error('Missing MANAGE_WEBHOOKS'); - const hooks = await this.guild.fetchWebhooks(); + const hooks = await this.fetchWebhooks(); const hook = hooks.get(result.hookID); if (!hook) return null; // const hook = new WebhookClient(result.hookID, result.token, { @@ -125,6 +125,10 @@ class GuildWrapper { } + fetchWebhooks() { + return this.guild.fetchWebhooks(); + } + get defaultConfig() { return { ...this.client.defaultConfig('GUILD') }; } diff --git a/src/structure/components/commands/administration/Import.js b/src/structure/components/commands/administration/Import.js index fb258f3..e6326f2 100644 --- a/src/structure/components/commands/administration/Import.js +++ b/src/structure/components/commands/administration/Import.js @@ -49,7 +49,7 @@ class ImportCommand extends SlashCommand { database: dbs[version], // Default to v3 version }); - + await migrator.connect(); let imported = null; try { @@ -58,7 +58,18 @@ class ImportCommand extends SlashCommand { return { index: 'COMMAND_IMPORT_ERROR', params: { message: err.message }, emoji: 'failure' }; } - await guild.updateSettings(imported); + const { webhook } = imported; + if (webhook) { + if (typeof webhook === 'string') { + const hooks = await guild.fetchWebhooks(); + const hook = hooks.get(webhook); + if(hook) await guild.updateWebhook('messages', hook); + } else if(version === '3') { + this.client.storageManager.mongodb.webhooks.updateOne({ feature: 'messages', guild: guild.id }, webhook); + } + } + + await guild.updateSettings(imported.settings); return { index: 'COMMAND_IMPORT_SUCCESS', emoji: 'success' }; } diff --git a/src/utilities/SettingsMigrator.js b/src/utilities/SettingsMigrator.js index cbfccda..907f27e 100644 --- a/src/utilities/SettingsMigrator.js +++ b/src/utilities/SettingsMigrator.js @@ -30,9 +30,10 @@ class SettingsMigrator { this.logger.debug(`Attempting settings migration for ${this.guild}`); + const { version } = this._config; let idIdentifier = null, collection = null; - if (this._config.version === '2') { + if (version === '2') { collection = 'discord_guilds'; idIdentifier = 'guild_id'; } else { @@ -41,15 +42,20 @@ class SettingsMigrator { } const filter = { [idIdentifier]: this.guild }; - console.log(filter); - const result = await this.mongo.findOne(collection, filter); - if (!result) return Promise.reject(new Error('No old settings found')); + const settings = await this.mongo.findOne(collection, filter); + if (!settings) return Promise.reject(new Error('No old settings found')); - const { _version } = result; + const { _version } = settings; if (!_version) return Promise.reject(new Error('Unable to determine configuration version')); + let webhook = null; + if (version === '3') + webhook = this.mongo.findOne('webhooks', { guild: this.guild, feature: 'messageLog' }); + else if (version === '2') + ({ webhook } = settings.chatlogs); + this.logger.info(`Settings migration for ${this.guild}`); - return this.cleanUp(this[_version](result)); + return { settings: this.cleanUp(this[_version](settings)), webhook }; }