From 0e160ae77477f0986a02746e84158329299f438f Mon Sep 17 00:00:00 2001 From: IRONM00N <64110067+IRONM00N@users.noreply.github.com> Date: Sat, 8 Jan 2022 12:25:35 -0500 Subject: add shared db --- src/lib/extensions/discord-akairo/BushClient.ts | 76 +++++++++++++++------- .../extensions/discord-akairo/BushClientUtil.ts | 42 ++++++++++++ 2 files changed, 95 insertions(+), 23 deletions(-) (limited to 'src/lib/extensions') diff --git a/src/lib/extensions/discord-akairo/BushClient.ts b/src/lib/extensions/discord-akairo/BushClient.ts index 8cbc6fe..65b60eb 100644 --- a/src/lib/extensions/discord-akairo/BushClient.ts +++ b/src/lib/extensions/discord-akairo/BushClient.ts @@ -42,7 +42,7 @@ import { import EventEmitter from 'events'; import path from 'path'; import readline from 'readline'; -import type { Sequelize as SequelizeType } from 'sequelize'; +import type { Options as SequelizeOptions, Sequelize as SequelizeType } from 'sequelize'; import { fileURLToPath } from 'url'; import UpdateCacheTask from '../../../tasks/updateCache.js'; import UpdateStatsTask from '../../../tasks/updateStats.js'; @@ -52,6 +52,7 @@ import { Guild as GuildModel } from '../../models/Guild.js'; import { Level } from '../../models/Level.js'; import { ModLog } from '../../models/ModLog.js'; import { Reminder } from '../../models/Reminder.js'; +import { Shared } from '../../models/Shared.js'; import { Stat } from '../../models/Stat.js'; import { StickyRole } from '../../models/StickyRole.js'; import { AllowedMentions } from '../../utils/AllowedMentions.js'; @@ -152,9 +153,14 @@ export class BushClient extends AkairoClient extends AkairoClient; this.config = config; + this.util = new BushClientUtil(this); + + /* handlers */ this.listenerHandler = new BushListenerHandler(this, { directory: path.join(__dirname, '..', '..', '..', 'listeners'), automateCategories: true @@ -250,9 +259,9 @@ export class BushClient extends AkairoClient extends AkairoClient this.logger.debug(sql) : false, timezone: 'America/New_York' + }; + this.instanceDB = new Sequelize({ + ...sharedDBOptions, + database: this.config.isDevelopment ? 'bushbot-dev' : this.config.isBeta ? 'bushbot-beta' : 'bushbot' + }); + this.sharedDB = new Sequelize({ + ...sharedDBOptions, + database: 'bushbot-shared' }); - // global objects + // eslint-disable-next-line @typescript-eslint/no-unused-vars + /* global objects */ global.client = this; global.util = this.util; } @@ -321,7 +339,7 @@ export class BushClient extends AkairoClient extends AkairoClient>.`, false); + } catch (e) { + await this.console.error( + 'startup', + `Failed to connect to <> with error:\n${util.inspect(e, { colors: true, depth: 1 })}`, + false + ); + process.exit(2); + } try { - await this.db.authenticate(); - Global.initModel(this.db); - GuildModel.initModel(this.db, this); - ModLog.initModel(this.db); - ActivePunishment.initModel(this.db); - Level.initModel(this.db); - StickyRole.initModel(this.db); - Stat.initModel(this.db); - Reminder.initModel(this.db); - await this.db.sync({ alter: true }); // Sync all tables to fix everything if updated - await this.console.success('startup', `Successfully connected to <>.`, false); + await this.sharedDB.authenticate(); + Stat.initModel(this.sharedDB); + Global.initModel(this.sharedDB); + Shared.initModel(this.sharedDB); + await this.sharedDB.sync({ alter: true }); // Sync all tables to fix everything if updated + await this.console.success('startup', `Successfully connected to <>.`, false); } catch (e) { await this.console.error( 'startup', - `Failed to connect to <> with error:\n${util.inspect(e, { colors: true, depth: 1 })}`, + `Failed to connect to <> with error:\n${util.inspect(e, { colors: true, depth: 1 })}`, false ); process.exit(2); @@ -416,7 +447,6 @@ export class BushClient extends AkairoClient>.`, false); this.stats.commandsUsed = await UpdateStatsTask.init(); @@ -443,7 +473,7 @@ export class BushClient extends AkairoClient(key: K): SharedCache[K]; + public getShared(key?: keyof SharedCache) { + return key ? client.cache.shared[key] : client.cache.shared; + } + /** * Add or remove an element from an array stored in the Globals database. * @param action Either `add` or `remove` an element. @@ -462,6 +470,25 @@ export class BushClientUtil extends ClientUtil { return await row.save().catch((e) => this.handleError('insertOrRemoveFromGlobal', e)); } + /** + * Add or remove an element from an array stored in the Shared database. + * @param action Either `add` or `remove` an element. + * @param key The key of the element in the shared cache to update. + * @param value The value to add/remove from the array. + */ + public async insertOrRemoveFromShared( + action: 'add' | 'remove', + key: K, + value: typeof client['cache']['shared'][K][0] + ): Promise { + const row = (await Shared.findByPk(0)) ?? (await Shared.create()); + const oldValue: any[] = row[key]; + const newValue = this.addOrRemoveFromArray(action, oldValue, value); + row[key] = newValue; + client.cache.shared[key] = newValue; + return await row.save().catch((e) => this.handleError('insertOrRemoveFromShared', e)); + } + /** * Updates an element in the Globals database. * @param key The key in the global cache to update. @@ -478,6 +505,21 @@ export class BushClientUtil extends ClientUtil { return await row.save().catch((e) => this.handleError('setGlobal', e)); } + /** + * Updates an element in the Shared database. + * @param key The key in the shared cache to update. + * @param value The value to set the key to. + */ + public async setShared( + key: K, + value: typeof client['cache']['shared'][K] + ): Promise { + const row = (await Shared.findByPk(0)) ?? (await Shared.create()); + row[key] = value; + client.cache.shared[key] = value; + return await row.save().catch((e) => this.handleError('setShared', e)); + } + /** * Add or remove an item from an array. All duplicates will be removed. * @param action Either `add` or `remove` an element. -- cgit