aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorIRONM00N <64110067+IRONM00N@users.noreply.github.com>2021-06-21 15:33:36 -0400
committerIRONM00N <64110067+IRONM00N@users.noreply.github.com>2021-06-21 15:33:36 -0400
commit6eb42974bdd4da4f9a6d77c8fde4c19f9f0a351b (patch)
tree6b0490f7f17d5d663f0f764589328e8acb79dd22 /src/lib
parent5c3da90f441c321f55ae735d6002f4da91f2481e (diff)
downloadtanzanite-6eb42974bdd4da4f9a6d77c8fde4c19f9f0a351b.tar.gz
tanzanite-6eb42974bdd4da4f9a6d77c8fde4c19f9f0a351b.tar.bz2
tanzanite-6eb42974bdd4da4f9a6d77c8fde4c19f9f0a351b.zip
fix(db): made it work now
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/extensions/BushClient.ts3
-rw-r--r--src/lib/extensions/BushClientUtil.ts20
-rw-r--r--src/lib/extensions/BushCommand.ts4
-rw-r--r--src/lib/extensions/BushInteractionMessage.ts2
-rw-r--r--src/lib/models/Global.ts46
-rw-r--r--src/lib/models/Guild.ts16
-rw-r--r--src/lib/models/Level.ts2
-rw-r--r--src/lib/models/StickyRole.ts10
-rw-r--r--src/lib/utils/BushCache.ts4
9 files changed, 92 insertions, 15 deletions
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 <<database>>.`, false);
} catch (error) {
this.console.error('Startup', `Failed to connect to <<database>> 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<GlobalModel, GlobalModelCreationAttributes> implements GlobalModel {
+ environment: 'production' | 'development';
superUsers: Snowflake[];
disabledCommands: string[];
blacklistedUsers: Snowflake[];
@@ -23,24 +25,58 @@ export class Global extends BaseModel<GlobalModel, GlobalModelCreationAttributes
static initModel(sequelize: Sequelize): void {
Global.init(
{
+ environment: {
+ type: DataTypes.STRING,
+ primaryKey: true
+ },
superUsers: {
- type: DataTypes.ARRAY(DataTypes.STRING),
+ type: DataTypes.STRING,
+ get: function () {
+ return JSON.parse(this.getDataValue('superUsers') as unknown as string);
+ },
+ set: function (val: Snowflake[]) {
+ return this.setDataValue('superUsers', JSON.stringify(val) as unknown as Snowflake[]);
+ },
allowNull: true
},
disabledCommands: {
- type: DataTypes.ARRAY(DataTypes.STRING),
+ type: DataTypes.STRING,
+ get: function () {
+ return JSON.parse(this.getDataValue('disabledCommands') as unknown as string);
+ },
+ set: function (val: Snowflake[]) {
+ return this.setDataValue('disabledCommands', JSON.stringify(val) as unknown as string[]);
+ },
allowNull: true
},
blacklistedUsers: {
- type: DataTypes.ARRAY(DataTypes.STRING),
+ type: DataTypes.STRING,
+ get: function () {
+ return JSON.parse(this.getDataValue('blacklistedUsers') as unknown as string);
+ },
+ set: function (val: Snowflake[]) {
+ return this.setDataValue('blacklistedUsers', JSON.stringify(val) as unknown as Snowflake[]);
+ },
allowNull: true
},
blacklistedGuilds: {
- type: DataTypes.ARRAY(DataTypes.STRING),
+ type: DataTypes.STRING,
+ get: function () {
+ return JSON.parse(this.getDataValue('blacklistedGuilds') as unknown as string);
+ },
+ set: function (val: Snowflake[]) {
+ return this.setDataValue('blacklistedGuilds', 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/Guild.ts b/src/lib/models/Guild.ts
index bc93951..c4ae53e 100644
--- a/src/lib/models/Guild.ts
+++ b/src/lib/models/Guild.ts
@@ -30,11 +30,23 @@ export class Guild extends BaseModel<GuildModel, GuildModelCreationAttributes> 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<LevelModel, LevelModelCreationAttributes> {
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<StickyRoleModel, StickyRoleModelCreati
},
roles: {
- type: DataTypes.ARRAY(DataTypes.STRING),
- allowNull: false
+ type: DataTypes.STRING,
+ get: function () {
+ return JSON.parse(this.getDataValue('roles') as unknown as string);
+ },
+ set: function (val: Snowflake[]) {
+ return this.setDataValue('roles', JSON.stringify(val) as unknown as Snowflake[]);
+ },
+ allowNull: true
}
},
{ sequelize }
diff --git a/src/lib/utils/BushCache.ts b/src/lib/utils/BushCache.ts
index 915fcb1..947b15d 100644
--- a/src/lib/utils/BushCache.ts
+++ b/src/lib/utils/BushCache.ts
@@ -2,4 +2,8 @@ import { Snowflake } from 'discord.js';
export class BushCache {
public static superUsers = new Array<Snowflake>();
+ public static disabledCommands = new Array<string>();
+ public static blacklistedChannels = new Array<Snowflake>();
+ public static blacklistedGuilds = new Array<Snowflake>();
+ public static blacklistedUsers = new Array<Snowflake>();
}