From 6eb42974bdd4da4f9a6d77c8fde4c19f9f0a351b Mon Sep 17 00:00:00 2001 From: IRONM00N <64110067+IRONM00N@users.noreply.github.com> Date: Mon, 21 Jun 2021 15:33:36 -0400 Subject: fix(db): made it work now --- src/lib/extensions/BushClient.ts | 3 +- src/lib/extensions/BushClientUtil.ts | 20 +++++++++++- src/lib/extensions/BushCommand.ts | 4 +-- src/lib/extensions/BushInteractionMessage.ts | 2 +- src/lib/models/Global.ts | 46 +++++++++++++++++++++++++--- src/lib/models/Guild.ts | 16 ++++++++-- src/lib/models/Level.ts | 2 +- src/lib/models/StickyRole.ts | 10 ++++-- src/lib/utils/BushCache.ts | 4 +++ 9 files changed, 92 insertions(+), 15 deletions(-) (limited to 'src/lib') diff --git a/src/lib/extensions/BushClient.ts b/src/lib/extensions/BushClient.ts index ad22cfe..d0758d4 100644 --- a/src/lib/extensions/BushClient.ts +++ b/src/lib/extensions/BushClient.ts @@ -36,6 +36,7 @@ export class BushClient extends AkairoClient { public db: Sequelize; public logger: BushLogger; public constants = BushConstants; + public cache = BushCache; constructor(config: BotConfig) { super( { @@ -164,7 +165,7 @@ export class BushClient extends AkairoClient { Models.Ban.initModel(this.db); Models.Level.initModel(this.db); Models.StickyRole.initModel(this.db); - await this.db.sync(); // Sync all tables to fix everything if updated + await this.db.sync({ alter: true }); // Sync all tables to fix everything if updated this.console.success('Startup', `Successfully connected to <>.`, false); } catch (error) { this.console.error('Startup', `Failed to connect to <> with error:\n` + error?.stack, false); diff --git a/src/lib/extensions/BushClientUtil.ts b/src/lib/extensions/BushClientUtil.ts index 6687cb0..4d428ee 100644 --- a/src/lib/extensions/BushClientUtil.ts +++ b/src/lib/extensions/BushClientUtil.ts @@ -31,6 +31,8 @@ import { } from 'discord.js'; import got from 'got'; import { promisify } from 'util'; +import { Global } from '../models/Global'; +import { BushCache } from '../utils/BushCache'; import { BushClient } from './BushClient'; import { BushMessage } from './BushMessage'; @@ -233,7 +235,7 @@ export class BushClientUtil extends ClientUtil { /** Commonly Used Emojis */ public emojis = { success: '<:checkmark:837109864101707807>', - warn: '<:warn:848726900876247050> ', + warn: '<:warn:848726900876247050>', error: '<:error:837123021016924261>', successFull: '<:checkmark_full:850118767576088646>', warnFull: '<:warn_full:850118767391539312>', @@ -481,4 +483,20 @@ export class BushClientUtil extends ClientUtil { array[l - 1] = `${conjunction} ${array[l - 1]}`; return array.join(', '); } + + public async insertOrRemoveFromGlobal(action: 'add' | 'remove', key: keyof typeof BushCache, value: any) { + const environment = this.client.config.dev ? 'development' : 'production'; + let row = await Global.findByPk(environment); + const oldValue: any[] = row[key]; + let newValue: any[]; + if (action === 'add') { + if (!oldValue.includes(action)) oldValue.push(value); + newValue = oldValue; + } else { + newValue = oldValue.filter((ae) => ae !== value); + } + row[key] = newValue; + this.client.cache[key] = newValue; + return await row.save().catch((e) => this.client.logger.error('insertOrRemoveFromGlobal', e)); + } } diff --git a/src/lib/extensions/BushCommand.ts b/src/lib/extensions/BushCommand.ts index 8358c46..bc6ff68 100644 --- a/src/lib/extensions/BushCommand.ts +++ b/src/lib/extensions/BushCommand.ts @@ -4,7 +4,7 @@ import { Command, CommandOptions } from 'discord-akairo'; import { Snowflake } from 'discord.js'; import { BushClient } from './BushClient'; import { BushCommandHandler } from './BushCommandHandler'; -import { BushInteractionMessage } from './BushInteractionMessage'; +import { BushSlashMessage } from './BushInteractionMessage'; import { BushMessage } from './BushMessage'; export interface BushCommandOptions extends CommandOptions { @@ -37,7 +37,7 @@ export class BushCommand extends Command { } public exec(message: BushMessage, args: any): any; - public exec(message: BushMessage | BushInteractionMessage, args: any): any { + public exec(message: BushMessage | BushSlashMessage, args: any): any { super.exec(message, args); } } diff --git a/src/lib/extensions/BushInteractionMessage.ts b/src/lib/extensions/BushInteractionMessage.ts index 9bdc291..ade11ea 100644 --- a/src/lib/extensions/BushInteractionMessage.ts +++ b/src/lib/extensions/BushInteractionMessage.ts @@ -2,7 +2,7 @@ import { AkairoMessage } from 'discord-akairo'; import { CommandInteraction } from 'discord.js'; import { BushClient } from './BushClient'; -export class BushInteractionMessage extends AkairoMessage { +export class BushSlashMessage extends AkairoMessage { public constructor( client: BushClient, interaction: CommandInteraction, diff --git a/src/lib/models/Global.ts b/src/lib/models/Global.ts index 65f51c4..abe0ab3 100644 --- a/src/lib/models/Global.ts +++ b/src/lib/models/Global.ts @@ -3,6 +3,7 @@ import { DataTypes, Optional, Sequelize } from 'sequelize'; import { BaseModel } from './BaseModel'; export interface GlobalModel { + environment: 'production' | 'development'; superUsers: Snowflake[]; disabledCommands: string[]; blacklistedUsers: Snowflake[]; @@ -15,6 +16,7 @@ export type GlobalModelCreationAttributes = Optional< >; export class Global extends BaseModel implements GlobalModel { + environment: 'production' | 'development'; superUsers: Snowflake[]; disabledCommands: string[]; blacklistedUsers: Snowflake[]; @@ -23,24 +25,58 @@ export class Global extends BaseModel i defaultValue: client.config.prefix }, autoPublishChannels: { - type: DataTypes.ARRAY(DataTypes.STRING), + type: DataTypes.STRING, + get: function () { + return JSON.parse(this.getDataValue('autoPublishChannels') as unknown as string); + }, + set: function (val: Snowflake[]) { + return this.setDataValue('autoPublishChannels', JSON.stringify(val) as unknown as Snowflake[]); + }, allowNull: true }, blacklistedChannels: { - type: DataTypes.ARRAY(DataTypes.STRING), + type: DataTypes.STRING, + get: function () { + return JSON.parse(this.getDataValue('blacklistedChannels') as unknown as string); + }, + set: function (val: Snowflake[]) { + return this.setDataValue('blacklistedChannels', JSON.stringify(val) as unknown as Snowflake[]); + }, allowNull: true } }, diff --git a/src/lib/models/Level.ts b/src/lib/models/Level.ts index 6113627..426ec1a 100644 --- a/src/lib/models/Level.ts +++ b/src/lib/models/Level.ts @@ -32,7 +32,7 @@ export class Level extends BaseModel { defaultValue: 0 } }, - { sequelize } + { sequelize: sequelize } ); } static convertXpToLevel(xp: number): number { diff --git a/src/lib/models/StickyRole.ts b/src/lib/models/StickyRole.ts index 597d7c5..a3928e7 100644 --- a/src/lib/models/StickyRole.ts +++ b/src/lib/models/StickyRole.ts @@ -30,8 +30,14 @@ export class StickyRole extends BaseModel(); + public static disabledCommands = new Array(); + public static blacklistedChannels = new Array(); + public static blacklistedGuilds = new Array(); + public static blacklistedUsers = new Array(); } -- cgit