v2 is no more

This commit is contained in:
Erik 2023-01-29 19:36:28 +02:00
parent d73d1b4798
commit 63a8c79ec0
Signed by untrusted user: Navy.gif
GPG Key ID: 2532FBBB61C65A68

View File

@ -5,8 +5,6 @@ const { FilterUtil, SettingsMigrator, InfractionMigrator } = require("../../../u
const MemberWrapper = require("./MemberWrapper.js");
const configVersion = '3.slash.2';
const { MONGODB_V2_HOST } = process.env;
class GuildWrapper {
constructor(client, guild) {
@ -31,7 +29,7 @@ class GuildWrapper {
const now = Date.now();
const id = `${type}:${user}:${now}`;
const data = { ...opts, user, id, guild: this.id, type, time: duration * 1000, created: now };
if(duration) await this.createCallback(data);
if (duration) await this.createCallback(data);
}
async createReminder({ time, user, channel, reminder }) {
@ -52,20 +50,20 @@ class GuildWrapper {
async createCallback(data, update = true) {
const handler = this[`_${data.type}`];//.bind(this);
if (!handler) throw new Error(`Invalid callback type`);
const now = Date.now();
const time = data.created + data.time;
const diff = time - now;
if (diff < 5000) return handler.bind(this)(data);
const cb = { timeout: setTimeout(handler.bind(this), diff, data), data };
this.callbacks.set(data.id, cb);
if(update) await this.client.mongodb.callbacks.updateOne({ id: data.id, guild: this.id }, data);
if (update) await this.client.mongodb.callbacks.updateOne({ id: data.id, guild: this.id }, data);
}
async removeCallback(id) {
const cb = this.callbacks.get(id);
if(cb) clearTimeout(cb.timeout);
if (cb) clearTimeout(cb.timeout);
this.callbacks.delete(id);
await this.client.mongodb.callbacks.deleteOne({ guild: this.id, id });
}
@ -81,8 +79,8 @@ class GuildWrapper {
const result = {};
for (const emoji of reactionEmojis) {
let reaction = reactions.resolve(emoji);
if(!reaction) continue;
if(reaction.partial) reaction = await reaction.fetch();
if (!reaction) continue;
if (reaction.partial) reaction = await reaction.fetch();
result[emoji] = reaction.count - 1;
}
@ -93,7 +91,7 @@ class GuildWrapper {
}
}
await this.removeCallback(id);
if(startedIn) await startedIn.send(this.format('COMMAND_POLL_NOTIFY_STARTER', { user, channel }));
if (startedIn) await startedIn.send(this.format('COMMAND_POLL_NOTIFY_STARTER', { user, channel }));
}
async _reminder({ reminder, user, channel, id }) {
@ -104,7 +102,8 @@ class GuildWrapper {
title: this.format('GENERAL_REMINDER_TITLE'),
description: reminder,
color: EmbedDefaultColor
}] });
}]
});
await this.removeCallback(id);
}
@ -128,7 +127,7 @@ class GuildWrapper {
}
async fetchData() {
if(this._data) return this._data;
if (this._data) return this._data;
const data = await this.client.mongodb.guilds.findOne({ guildId: this.id });
if (!data) {
this._data = {};
@ -155,16 +154,20 @@ class GuildWrapper {
const data = await this.fetchData();
// eslint-disable-next-line prefer-const
let { settings, _imported } = data;
const {
settings,
// _imported
} = data;
const { defaultConfig } = this;
if (!settings && !_imported?.settings && !_imported?.modlogs && process.env.NODE_ENV === 'staging') {
if (this._importPromise) settings = await this._importPromise;
else {
this._importPromise = this._attemptDataImport();
settings = await this._importPromise;
}
}
// V2 db server is no more, leaving this here if needed again
// if (!settings && !_imported?.settings && !_imported?.modlogs && process.env.NODE_ENV === 'staging') {
// if (this._importPromise) settings = await this._importPromise;
// else {
// this._importPromise = this._attemptDataImport();
// settings = await this._importPromise;
// }
// }
if (settings) {
// Ensure new settings properties are propagated to existing configs
@ -173,7 +176,7 @@ class GuildWrapper {
if (!(key in defaultConfig)) continue;
defaultConfig[key] = { ...defaultConfig[key], ...settings[key] };
}
}
this._settings = defaultConfig;
@ -205,7 +208,7 @@ class GuildWrapper {
const perms = await this.client.storageManager.mongodb.permissions.findOne({ guildId: this.id });
if (!perms) this._permissions = { guildId: this.id };
else this._permissions = perms;
return this._permissions;
}
@ -221,7 +224,7 @@ class GuildWrapper {
async _attemptDataImport() {
const migratorOptions = {
host: MONGODB_V2_HOST,
// host: MONGODB_V2_HOST,
database: 'galacticbot',
version: '2'
};
@ -264,7 +267,7 @@ class GuildWrapper {
if (webhook) {
const hooks = await this.fetchWebhooks().catch(() => null);
const hook = hooks?.get(webhook);
if (hook)
if (hook)
await this.updateWebhook('messages', hook);
}
@ -360,7 +363,7 @@ class GuildWrapper {
async userWrapper() {
if (this._userWrapper) return this._userWrapper;
this._userWrapper = await this.client.getUserWrapper(this.author.id);
return this._userWrapper;
}
@ -369,7 +372,7 @@ class GuildWrapper {
const member = await this.resolveMember(user);
if (!member) return Promise.reject(new Error('No member found'));
if (this.memberWrappers.has(member.id)) return this.memberWrappers.get(member.id);
const wrapper = new MemberWrapper(this.client, member);
this.memberWrappers.set(wrapper.id, wrapper);
return wrapper;