diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-01-08 20:08:16 -0500 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-01-08 20:08:16 -0500 |
commit | e0531157a745ac6178d55cc159b5932fa36ff20f (patch) | |
tree | a8de340d0c48269619e9bfde536932eb08556593 /src/lib/models | |
parent | 0e160ae77477f0986a02746e84158329299f438f (diff) | |
download | tanzanite-e0531157a745ac6178d55cc159b5932fa36ff20f.tar.gz tanzanite-e0531157a745ac6178d55cc159b5932fa36ff20f.tar.bz2 tanzanite-e0531157a745ac6178d55cc159b5932fa36ff20f.zip |
database migrations - use jsonb over text + JSON.parse, refactor bad word & automod - store word in shared db
closes: #53
Diffstat (limited to 'src/lib/models')
-rw-r--r-- | src/lib/models/Global.ts | 9 | ||||
-rw-r--r-- | src/lib/models/Guild.ts | 42 | ||||
-rw-r--r-- | src/lib/models/ModLog.ts | 5 | ||||
-rw-r--r-- | src/lib/models/Shared.ts | 42 | ||||
-rw-r--r-- | src/lib/models/Stat.ts | 13 | ||||
-rw-r--r-- | src/lib/models/StickyRole.ts | 3 | ||||
-rw-r--r-- | src/lib/models/__helpers.ts | 65 |
7 files changed, 73 insertions, 106 deletions
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<GlobalModel, GlobalModelCreationAttributes Global.init( { environment: { type: DataTypes.STRING, primaryKey: true }, - disabledCommands: jsonArray('disabledCommands'), - blacklistedUsers: jsonArray('blacklistedUsers'), - blacklistedGuilds: jsonArray('blacklistedGuilds'), - blacklistedChannels: jsonArray('blacklistedChannels') + disabledCommands: { type: DataTypes.JSONB, allowNull: false, defaultValue: [] }, + blacklistedUsers: { type: DataTypes.JSONB, allowNull: false, defaultValue: [] }, + blacklistedGuilds: { type: DataTypes.JSONB, allowNull: false, defaultValue: [] }, + blacklistedChannels: { type: DataTypes.JSONB, allowNull: false, defaultValue: [] } }, { sequelize } ); diff --git a/src/lib/models/Guild.ts b/src/lib/models/Guild.ts index 80bd119..34f6747 100644 --- a/src/lib/models/Guild.ts +++ b/src/lib/models/Guild.ts @@ -1,10 +1,9 @@ import { ExcludeEnum, type Snowflake } from 'discord.js'; import { ChannelTypes } from 'discord.js/typings/enums'; import { type Sequelize } from 'sequelize'; -import { type BadWords } from '../common/AutoMod.js'; +import { BadWordDetails } from '../common/AutoMod.js'; import { type BushClient } from '../extensions/discord-akairo/BushClient.js'; import { BaseModel } from './BaseModel.js'; -import { jsonArray, jsonObject } from './__helpers.js'; const { DataTypes } = (await import('sequelize')).default; export interface GuildModel { @@ -18,7 +17,7 @@ export interface GuildModel { punishmentEnding: string; disabledCommands: string[]; lockdownChannels: Snowflake[]; - autoModPhases: BadWords; + autoModPhases: BadWordDetails[]; enabledFeatures: GuildFeatures[]; joinRoles: Snowflake[]; logChannels: LogChannelDB; @@ -39,7 +38,7 @@ export interface GuildModelCreationAttributes { punishmentEnding?: string; disabledCommands?: string[]; lockdownChannels?: Snowflake[]; - autoModPhases?: BadWords; + autoModPhases?: BadWordDetails[]; enabledFeatures?: GuildFeatures[]; joinRoles?: Snowflake[]; logChannels?: LogChannelDB; @@ -103,7 +102,7 @@ export class Guild extends BaseModel<GuildModel, GuildModelCreationAttributes> 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<GuildModel, GuildModelCreationAttributes> 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<ModLogModel, ModLogModelCreationAttributes reason: { type: DataTypes.TEXT, allowNull: true }, guild: { type: DataTypes.STRING, references: { model: 'Guilds', key: 'id' } }, evidence: { type: DataTypes.TEXT, allowNull: true }, - pseudo: jsonBoolean('pseudo'), - hidden: jsonBoolean('hidden') + pseudo: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false }, + hidden: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false } }, { sequelize } ); diff --git a/src/lib/models/Shared.ts b/src/lib/models/Shared.ts index dd7682b..a240ef9 100644 --- a/src/lib/models/Shared.ts +++ b/src/lib/models/Shared.ts @@ -1,18 +1,25 @@ -import { type Sequelize } from 'sequelize'; +import { Snowflake } from 'discord.js'; +import type { Sequelize } from 'sequelize'; +import type { BadWords } from '../common/AutoMod.js'; import { BaseModel } from './BaseModel.js'; -import { jsonArray } from './__helpers.js'; const { DataTypes } = (await import('sequelize')).default; export interface SharedModel { primaryKey: 0; - superUsers: string[]; + superUsers: Snowflake[]; + privilegedUsers: Snowflake[]; + badLinksSecret: string[]; badLinks: string[]; + badWords: BadWords; } export interface SharedModelCreationAttributes { primaryKey?: 0; - superUsers?: string[]; + superUsers?: Snowflake[]; + privilegedUsers?: Snowflake[]; + badLinksSecret?: string[]; badLinks?: string[]; + badWords?: BadWords; } export class Shared extends BaseModel<SharedModel, SharedModelCreationAttributes> implements SharedModel { @@ -24,15 +31,29 @@ export class Shared extends BaseModel<SharedModel, SharedModelCreationAttributes /** * Trusted users. */ - public declare superUsers: string[]; + public declare superUsers: Snowflake[]; - //todo /** - * Bad links. + * Users that have all permissions that devs have except eval. + */ + public declare privilegedUsers: Snowflake[]; + + /** + * Non-public bad links. + */ + public declare badLinksSecret: string[]; + + /** + * Public Bad links. */ public declare badLinks: string[]; /** + * Bad words. + */ + public declare badWords: BadWords; + + /** * Initializes the model. * @param sequelize The sequelize instance. */ @@ -40,8 +61,11 @@ export class Shared extends BaseModel<SharedModel, SharedModelCreationAttributes Shared.init( { primaryKey: { type: DataTypes.INTEGER, primaryKey: true, validate: { min: 0, max: 0 } }, - superUsers: jsonArray('superUsers'), - badLinks: jsonArray('badLinks') + superUsers: { type: DataTypes.JSONB, allowNull: false, defaultValue: [] }, + privilegedUsers: { type: DataTypes.JSONB, allowNull: false, defaultValue: [] }, + badLinksSecret: { type: DataTypes.JSONB, allowNull: false, defaultValue: [] }, + badLinks: { type: DataTypes.JSONB, allowNull: false, defaultValue: [] }, + badWords: { type: DataTypes.JSONB, allowNull: false, defaultValue: {} } }, { sequelize, freezeTableName: true } ); diff --git a/src/lib/models/Stat.ts b/src/lib/models/Stat.ts index 06ee21f..8f90724 100644 --- a/src/lib/models/Stat.ts +++ b/src/lib/models/Stat.ts @@ -1,6 +1,5 @@ import { type Sequelize } from 'sequelize'; import { BaseModel } from './BaseModel.js'; -import { jsonBigint } from './__helpers.js'; const { DataTypes } = (await import('sequelize')).default; type Environment = 'production' | 'development' | 'beta'; @@ -34,7 +33,17 @@ export class Stat extends BaseModel<StatModel, StatModelCreationAttributes> 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', <any>`${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<StickyRoleModel, StickyRoleModelCreati { user: { type: DataTypes.STRING, allowNull: false }, guild: { type: DataTypes.STRING, allowNull: false }, - roles: jsonArray('roles'), + roles: { type: DataTypes.JSONB, allowNull: false, defaultValue: [] }, nickname: { type: DataTypes.STRING, allowNull: true } }, { sequelize } diff --git a/src/lib/models/__helpers.ts b/src/lib/models/__helpers.ts deleted file mode 100644 index bbfe328..0000000 --- a/src/lib/models/__helpers.ts +++ /dev/null @@ -1,65 +0,0 @@ -import { type Model } from 'sequelize'; -const { DataTypes } = (await import('sequelize')).default; - -export function jsonParseGet(this: Model, key: string): any { - return JSON.parse(this.getDataValue(key)); -} -export function jsonParseSet(this: Model, key: string, value: any): any { - return this.setDataValue(key, JSON.stringify(value)); -} - -export function jsonObject(key: string): any { - return { - type: DataTypes.TEXT, - get: function (): Record<string, unknown> { - return jsonParseGet.call(this, key); - }, - set: function (val: Record<string, unknown>) { - 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}` - }; -} |