automatic import for gtest deployment

This commit is contained in:
Erik 2022-08-31 22:56:05 +03:00
parent b2252329e8
commit d4ab0ff66d
Signed by untrusted user: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB
3 changed files with 65 additions and 5 deletions

View File

@ -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
*

View File

@ -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}`;

View File

@ -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;