From e0531157a745ac6178d55cc159b5932fa36ff20f Mon Sep 17 00:00:00 2001 From: IRONM00N <64110067+IRONM00N@users.noreply.github.com> Date: Sat, 8 Jan 2022 20:08:16 -0500 Subject: database migrations - use jsonb over text + JSON.parse, refactor bad word & automod - store word in shared db closes: #53 --- src/lib/models/Global.ts | 9 +++--- src/lib/models/Guild.ts | 42 ++++++++++++++-------------- src/lib/models/ModLog.ts | 5 ++-- src/lib/models/Shared.ts | 42 ++++++++++++++++++++++------ src/lib/models/Stat.ts | 13 +++++++-- src/lib/models/StickyRole.ts | 3 +- src/lib/models/__helpers.ts | 65 -------------------------------------------- 7 files changed, 73 insertions(+), 106 deletions(-) delete mode 100644 src/lib/models/__helpers.ts (limited to 'src/lib/models') diff --git a/src/lib/models/Global.ts b/src/lib/models/Global.ts index 1deb090..8e467f6 100644 --- a/src/lib/models/Global.ts +++ b/src/lib/models/Global.ts @@ -1,7 +1,6 @@ import { type Snowflake } from 'discord.js'; import { type Sequelize } from 'sequelize'; import { BaseModel } from './BaseModel.js'; -import { jsonArray } from './__helpers.js'; const { DataTypes } = (await import('sequelize')).default; export interface GlobalModel { @@ -54,10 +53,10 @@ export class Global extends BaseModel i /** * Custom automod phases */ - public declare autoModPhases: BadWords; + public declare autoModPhases: BadWordDetails[]; /** * The features enabled in a guild @@ -149,24 +148,27 @@ export class Guild extends BaseModel i { id: { type: DataTypes.STRING, primaryKey: true }, prefix: { type: DataTypes.TEXT, allowNull: false, defaultValue: client.config.prefix }, - autoPublishChannels: jsonArray('autoPublishChannels'), - blacklistedChannels: jsonArray('blacklistedChannels'), - blacklistedUsers: jsonArray('blacklistedChannels'), + autoPublishChannels: { type: DataTypes.JSONB, allowNull: false, defaultValue: [] }, + blacklistedChannels: { type: DataTypes.JSONB, allowNull: false, defaultValue: [] }, + blacklistedUsers: { type: DataTypes.JSONB, allowNull: false, defaultValue: [] }, welcomeChannel: { type: DataTypes.STRING, allowNull: true }, muteRole: { type: DataTypes.STRING, allowNull: true }, punishmentEnding: { type: DataTypes.TEXT, allowNull: true }, - disabledCommands: jsonArray('disabledCommands'), - lockdownChannels: jsonArray('lockdownChannels'), - autoModPhases: jsonObject('autoModPhases'), - enabledFeatures: jsonArray( - 'enabledFeatures', - Object.keys(guildFeaturesObj).filter((key) => guildFeaturesObj[key as keyof typeof guildFeaturesObj].default) - ), - joinRoles: jsonArray('joinRoles'), - logChannels: jsonObject('logChannels'), - bypassChannelBlacklist: jsonArray('bypassChannelBlacklist'), - noXpChannels: jsonArray('noXpChannels'), - levelRoles: jsonObject('levelRoles'), + disabledCommands: { type: DataTypes.JSONB, allowNull: false, defaultValue: [] }, + lockdownChannels: { type: DataTypes.JSONB, allowNull: false, defaultValue: [] }, + autoModPhases: { type: DataTypes.JSONB, allowNull: false, defaultValue: [] }, + enabledFeatures: { + type: DataTypes.JSONB, + allowNull: false, + defaultValue: Object.keys(guildFeaturesObj).filter( + (key) => guildFeaturesObj[key as keyof typeof guildFeaturesObj].default + ) + }, + joinRoles: { type: DataTypes.JSONB, allowNull: false, defaultValue: [] }, + logChannels: { type: DataTypes.JSONB, allowNull: false, defaultValue: {} }, + bypassChannelBlacklist: { type: DataTypes.JSONB, allowNull: false, defaultValue: [] }, + noXpChannels: { type: DataTypes.JSONB, allowNull: false, defaultValue: [] }, + levelRoles: { type: DataTypes.JSONB, allowNull: false, defaultValue: {} }, levelUpChannel: { type: DataTypes.STRING, allowNull: true } }, { sequelize } diff --git a/src/lib/models/ModLog.ts b/src/lib/models/ModLog.ts index 7ddd64c..8d6a08c 100644 --- a/src/lib/models/ModLog.ts +++ b/src/lib/models/ModLog.ts @@ -2,7 +2,6 @@ import { type Snowflake } from 'discord.js'; import { nanoid } from 'nanoid'; import { type Sequelize } from 'sequelize'; import { BaseModel } from './BaseModel.js'; -import { jsonBoolean } from './__helpers.js'; const { DataTypes } = (await import('sequelize')).default; export enum ModLogType { @@ -116,8 +115,8 @@ export class ModLog extends BaseModel implements SharedModel { @@ -24,14 +31,28 @@ export class Shared extends BaseModel impl Stat.init( { environment: { type: DataTypes.STRING, primaryKey: true }, - commandsUsed: jsonBigint('commandsUsed') + commandsUsed: { + type: DataTypes.TEXT, + get: function (): bigint { + return BigInt(this.getDataValue('commandsUsed')); + }, + set: function (val: bigint) { + return this.setDataValue('commandsUsed', `${val}`); + }, + allowNull: false, + defaultValue: `${0n}` + } }, { sequelize } ); diff --git a/src/lib/models/StickyRole.ts b/src/lib/models/StickyRole.ts index bbe81ae..4beb4cf 100644 --- a/src/lib/models/StickyRole.ts +++ b/src/lib/models/StickyRole.ts @@ -1,7 +1,6 @@ import { type Snowflake } from 'discord.js'; import { type Sequelize } from 'sequelize'; import { BaseModel } from './BaseModel.js'; -import { jsonArray } from './__helpers.js'; const { DataTypes } = (await import('sequelize')).default; export interface StickyRoleModel { @@ -47,7 +46,7 @@ export class StickyRole extends BaseModel { - return jsonParseGet.call(this, key); - }, - set: function (val: Record) { - return jsonParseSet.call(this, key, val); - }, - allowNull: false, - defaultValue: '{}' - }; -} - -export function jsonArray(key: string, defaultValue: string[] = []): any { - return { - type: DataTypes.TEXT, - get: function (): string[] { - return jsonParseGet.call(this, key); - }, - set: function (val: string[]) { - return jsonParseSet.call(this, key, val); - }, - allowNull: false, - defaultValue: JSON.stringify(defaultValue) - }; -} - -export function jsonBoolean(key: string, defaultVal = false): any { - return { - type: DataTypes.STRING, - get: function (): boolean { - return jsonParseGet.call(this, key); - }, - set: function (val: boolean) { - return jsonParseSet.call(this, key, val); - }, - allowNull: false, - defaultValue: `${defaultVal}` - }; -} - -export function jsonBigint(key: string, defaultVal = 0n): any { - return { - type: DataTypes.TEXT, - get: function (): bigint { - return BigInt(this.getDataValue(key)); - }, - set: function (val: bigint) { - return this.setDataValue(key, `${val}`); - }, - allowNull: false, - defaultValue: `${defaultVal}` - }; -} -- cgit