diff options
Diffstat (limited to 'src/lib/models')
-rw-r--r-- | src/lib/models/ActivePunishment.ts | 136 | ||||
-rw-r--r-- | src/lib/models/BaseModel.ts | 23 | ||||
-rw-r--r-- | src/lib/models/Global.ts | 88 | ||||
-rw-r--r-- | src/lib/models/Guild.ts | 430 | ||||
-rw-r--r-- | src/lib/models/Level.ts | 70 | ||||
-rw-r--r-- | src/lib/models/ModLog.ts | 188 | ||||
-rw-r--r-- | src/lib/models/Stat.ts | 52 | ||||
-rw-r--r-- | src/lib/models/StickyRole.ts | 68 | ||||
-rw-r--r-- | src/lib/models/__helpers.ts | 45 |
9 files changed, 322 insertions, 778 deletions
diff --git a/src/lib/models/ActivePunishment.ts b/src/lib/models/ActivePunishment.ts index 12b0cab..0ad6cd8 100644 --- a/src/lib/models/ActivePunishment.ts +++ b/src/lib/models/ActivePunishment.ts @@ -19,6 +19,7 @@ export interface ActivePunishmentModel { expires: Date | null; modlog: string; } + export interface ActivePunishmentModelCreationAttributes { id?: string; type: ActivePunishmentType; @@ -29,123 +30,44 @@ export interface ActivePunishmentModelCreationAttributes { modlog: string; } -const NEVER_USED = 'This should never be executed'; - -export class ActivePunishment - extends BaseModel<ActivePunishmentModel, ActivePunishmentModelCreationAttributes> - implements ActivePunishmentModel -{ - /** - * The ID of this punishment (no real use just for a primary key) - */ - public get id(): string { - throw new Error(NEVER_USED); - } - public set id(_: string) { - throw new Error(NEVER_USED); - } +// declaration merging so that the fields don't override Sequelize's getters +export interface ActivePunishment { + /** The ID of this punishment (no real use just for a primary key) */ + id: string; - /** - * The type of punishment. - */ - public get type(): ActivePunishmentType { - throw new Error(NEVER_USED); - } - public set type(_: ActivePunishmentType) { - throw new Error(NEVER_USED); - } + /** The type of punishment. */ + type: ActivePunishmentType; - /** - * The user who is punished. - */ - public get user(): Snowflake { - throw new Error(NEVER_USED); - } - public set user(_: Snowflake) { - throw new Error(NEVER_USED); - } + /** The user who is punished. */ + user: Snowflake; - /** - * The guild they are punished in. - */ - public get guild(): Snowflake { - throw new Error(NEVER_USED); - } - public set guild(_: Snowflake) { - throw new Error(NEVER_USED); - } + /** The guild they are punished in. */ + guild: Snowflake; - /** - * Additional info about the punishment if applicable. The channel id for channel blocks and role for punishment roles. - */ - public get extraInfo(): Snowflake { - throw new Error(NEVER_USED); - } - public set extraInfo(_: Snowflake) { - throw new Error(NEVER_USED); - } + /** Additional info about the punishment if applicable. The channel id for channel blocks and role for punishment roles. */ + extraInfo: Snowflake; - /** - * The date when this punishment expires (optional). - */ - public get expires(): Date | null { - throw new Error(NEVER_USED); - } - public set expires(_: Date | null) { - throw new Error(NEVER_USED); - } + /** The date when this punishment expires (optional). */ + expires: Date | null; - /** - * The reference to the modlog entry. - */ - public get modlog(): string { - throw new Error(NEVER_USED); - } - public set modlog(_: string) { - throw new Error(NEVER_USED); - } + /** The reference to the modlog entry. */ + modlog: string; +} +export class ActivePunishment + extends BaseModel<ActivePunishmentModel, ActivePunishmentModelCreationAttributes> + implements ActivePunishmentModel +{ public static initModel(sequelize: Sequelize): void { ActivePunishment.init( { - id: { - type: DataTypes.STRING, - primaryKey: true, - allowNull: false, - defaultValue: nanoid - }, - type: { - type: DataTypes.STRING, - allowNull: false - }, - user: { - type: DataTypes.STRING, - allowNull: false - }, - guild: { - type: DataTypes.STRING, - allowNull: false, - references: { - model: 'Guilds', - key: 'id' - } - }, - extraInfo: { - type: DataTypes.STRING, - allowNull: true - }, - expires: { - type: DataTypes.DATE, - allowNull: true - }, - modlog: { - type: DataTypes.STRING, - allowNull: true, - references: { - model: 'ModLogs', - key: 'id' - } - } + id: { type: DataTypes.STRING, primaryKey: true, allowNull: false, defaultValue: nanoid }, + type: { type: DataTypes.STRING, allowNull: false }, + user: { type: DataTypes.STRING, allowNull: false }, + guild: { type: DataTypes.STRING, allowNull: false, references: { model: 'Guilds', key: 'id' } }, + extraInfo: { type: DataTypes.STRING, allowNull: true }, + expires: { type: DataTypes.DATE, allowNull: true }, + modlog: { type: DataTypes.STRING, allowNull: true, references: { model: 'ModLogs', key: 'id' } } }, { sequelize } ); diff --git a/src/lib/models/BaseModel.ts b/src/lib/models/BaseModel.ts index 555bf7d..d9377d1 100644 --- a/src/lib/models/BaseModel.ts +++ b/src/lib/models/BaseModel.ts @@ -1,18 +1,13 @@ import { Model } from 'sequelize'; -import { NEVER_USED } from './__helpers'; -export abstract class BaseModel<A, B> extends Model<A, B> { - /** - * The date when the row was created. - */ - public get createdAt(): Date { - throw new Error(NEVER_USED); - } +// declaration merging so that the fields don't override Sequelize's getters +// eslint-disable-next-line @typescript-eslint/no-unused-vars +export interface BaseModel<A, B> { + /** The date when the row was created. */ + readonly createdAt: Date; - /** - * The date when the row was last updated. - */ - public get updatedAt(): Date { - throw new Error(NEVER_USED); - } + /** The date when the row was last updated. */ + readonly updatedAt: Date; } + +export abstract class BaseModel<A, B> extends Model<A, B> {} diff --git a/src/lib/models/Global.ts b/src/lib/models/Global.ts index 17bd570..c5c680d 100644 --- a/src/lib/models/Global.ts +++ b/src/lib/models/Global.ts @@ -1,7 +1,7 @@ import { Snowflake } from 'discord.js'; import { DataTypes, Sequelize } from 'sequelize'; import { BaseModel } from './BaseModel'; -import { jsonArrayInit, NEVER_USED } from './__helpers'; +import { jsonArray } from './__helpers'; export interface GlobalModel { environment: 'production' | 'development' | 'beta'; @@ -21,79 +21,37 @@ export interface GlobalModelCreationAttributes { blacklistedChannels?: Snowflake[]; } -export class Global extends BaseModel<GlobalModel, GlobalModelCreationAttributes> implements GlobalModel { - /** - * The bot's environment. - */ - public get environment(): 'production' | 'development' | 'beta' { - throw new Error(NEVER_USED); - } - public set environment(_: 'production' | 'development' | 'beta') { - throw new Error(NEVER_USED); - } +// declaration merging so that the fields don't override Sequelize's getters +export interface Global { + /** The bot's environment. */ + environment: 'production' | 'development' | 'beta'; - /** - * Trusted users. - */ - public get superUsers(): Snowflake[] { - throw new Error(NEVER_USED); - } - public set superUsers(_: Snowflake[]) { - throw new Error(NEVER_USED); - } + /** Trusted users. */ + superUsers: Snowflake[]; - /** - * Globally disabled commands. - */ - public get disabledCommands(): string[] { - throw new Error(NEVER_USED); - } - public set disabledCommands(_: string[]) { - throw new Error(NEVER_USED); - } + /** Globally disabled commands. */ + disabledCommands: string[]; - /** - * Globally blacklisted users. - */ - public get blacklistedUsers(): Snowflake[] { - throw new Error(NEVER_USED); - } - public set blacklistedUsers(_: Snowflake[]) { - throw new Error(NEVER_USED); - } + /** Globally blacklisted users. */ + blacklistedUsers: Snowflake[]; - /** - * Guilds blacklisted from using the bot. - */ - public get blacklistedGuilds(): Snowflake[] { - throw new Error(NEVER_USED); - } - public set blacklistedGuilds(_: Snowflake[]) { - throw new Error(NEVER_USED); - } + /** Guilds blacklisted from using the bot. */ + blacklistedGuilds: Snowflake[]; - /** - * Channels where the bot is prevented from running. - */ - public get blacklistedChannels(): Snowflake[] { - throw new Error(NEVER_USED); - } - public set blacklistedChannels(_: Snowflake[]) { - throw new Error(NEVER_USED); - } + /** Channels where the bot is prevented from running. */ + blacklistedChannels: Snowflake[]; +} +export class Global extends BaseModel<GlobalModel, GlobalModelCreationAttributes> implements GlobalModel { public static initModel(sequelize: Sequelize): void { Global.init( { - environment: { - type: DataTypes.STRING, - primaryKey: true - }, - superUsers: jsonArrayInit('superUsers'), - disabledCommands: jsonArrayInit('disabledCommands'), - blacklistedUsers: jsonArrayInit('blacklistedUsers'), - blacklistedGuilds: jsonArrayInit('blacklistedGuilds'), - blacklistedChannels: jsonArrayInit('blacklistedChannels') + environment: { type: DataTypes.STRING, primaryKey: true }, + superUsers: jsonArray('superUsers'), + disabledCommands: jsonArray('disabledCommands'), + blacklistedUsers: jsonArray('blacklistedUsers'), + blacklistedGuilds: jsonArray('blacklistedGuilds'), + blacklistedChannels: jsonArray('blacklistedChannels') }, { sequelize } ); diff --git a/src/lib/models/Guild.ts b/src/lib/models/Guild.ts index 7b51e61..1fadde9 100644 --- a/src/lib/models/Guild.ts +++ b/src/lib/models/Guild.ts @@ -3,9 +3,136 @@ import { DataTypes, Sequelize } from 'sequelize'; import { BadWords } from '../common/AutoMod'; import { BushClient } from '../extensions/discord-akairo/BushClient'; import { BaseModel } from './BaseModel'; -import { jsonArrayInit, jsonParseGet, jsonParseSet, NEVER_USED } from './__helpers'; +import { jsonArray, jsonObject } from './__helpers'; -interface GuildSetting { +export interface GuildModel { + id: Snowflake; + prefix: string; + autoPublishChannels: Snowflake[]; + blacklistedChannels: Snowflake[]; + blacklistedUsers: Snowflake[]; + welcomeChannel: Snowflake; + muteRole: Snowflake; + punishmentEnding: string; + disabledCommands: string[]; + lockdownChannels: Snowflake[]; + autoModPhases: BadWords; + enabledFeatures: GuildFeatures[]; + joinRoles: Snowflake[]; + logChannels: LogChannelDB; + bypassChannelBlacklist: Snowflake[]; + noXpChannels: Snowflake[]; + levelRoles: { [level: number]: Snowflake }; + levelUpChannel: Snowflake; +} + +export interface GuildModelCreationAttributes { + id: Snowflake; + prefix?: string; + autoPublishChannels?: Snowflake[]; + blacklistedChannels?: Snowflake[]; + blacklistedUsers?: Snowflake[]; + welcomeChannel?: Snowflake; + muteRole?: Snowflake; + punishmentEnding?: string; + disabledCommands?: string[]; + lockdownChannels?: Snowflake[]; + autoModPhases?: BadWords; + enabledFeatures?: GuildFeatures[]; + joinRoles?: Snowflake[]; + logChannels?: LogChannelDB; + bypassChannelBlacklist?: Snowflake[]; + noXpChannels?: Snowflake[]; + levelRoles?: { [level: number]: Snowflake }; + levelUpChannel?: Snowflake; +} + +// declaration merging so that the fields don't override Sequelize's getters +export interface Guild { + /** The ID of the guild */ + id: Snowflake; + + /** The bot's prefix for the guild */ + prefix: string; + + /** Channels that will have their messages automatically published */ + autoPublishChannels: Snowflake[]; + + /** Channels where the bot won't respond in. */ + blacklistedChannels: Snowflake[]; + + /** Users that the bot ignores in this guild */ + blacklistedUsers: Snowflake[]; + + /** The channels where the welcome messages are sent */ + welcomeChannel: Snowflake; + + /** The role given out when muting someone */ + muteRole: Snowflake; + + /** The message that gets sent after someone gets a punishment dm */ + punishmentEnding: string; + + /** Guild specific disabled commands */ + disabledCommands: string[]; + + /** Channels that should get locked down when the lockdown command gets used. */ + lockdownChannels: Snowflake[]; + + /** Custom automod phases */ + autoModPhases: BadWords; + + /** The features enabled in a guild */ + enabledFeatures: GuildFeatures[]; + + /** The roles to assign to a user if they are not assigned sticky roles */ + joinRoles: Snowflake[]; + + /** The channels where logging messages will be sent. */ + logChannels: LogChannelDB; + + /** These users will be able to use commands in channels blacklisted */ + bypassChannelBlacklist: Snowflake[]; + + /** Channels where users will not earn xp for leveling. */ + noXpChannels: Snowflake[]; + + /** What roles get given to users when they reach certain levels. */ + levelRoles: { [level: number]: Snowflake }; + + /** The channel to send level up messages in instead of last channel. */ + levelUpChannel: Snowflake; +} + +export class Guild extends BaseModel<GuildModel, GuildModelCreationAttributes> implements GuildModel { + public static initModel(sequelize: Sequelize, client: BushClient): void { + Guild.init( + { + 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'), + 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'), + joinRoles: jsonArray('joinRoles'), + logChannels: jsonObject('logChannels'), + bypassChannelBlacklist: jsonArray('bypassChannelBlacklist'), + noXpChannels: jsonArray('noXpChannels'), + levelRoles: jsonObject('levelRoles'), + levelUpChannel: { type: DataTypes.STRING, allowNull: true } + }, + { sequelize: sequelize } + ); + } +} + +export interface GuildSetting { name: string; description: string; type: 'string' | 'custom' | 'channel' | 'role' | 'user' | 'channel-array' | 'role-array' | 'user-array'; @@ -182,302 +309,3 @@ type LogChannelDB = { [x in keyof typeof guildLogsObj]?: Snowflake }; export type GuildFeatures = keyof typeof guildFeaturesObj; export const guildFeaturesArr: GuildFeatures[] = Object.keys(guildFeaturesObj) as GuildFeatures[]; - -export interface GuildModel { - id: Snowflake; - prefix: string; - autoPublishChannels: Snowflake[]; - blacklistedChannels: Snowflake[]; - blacklistedUsers: Snowflake[]; - welcomeChannel: Snowflake; - muteRole: Snowflake; - punishmentEnding: string; - disabledCommands: string[]; - lockdownChannels: Snowflake[]; - autoModPhases: BadWords; - enabledFeatures: GuildFeatures[]; - joinRoles: Snowflake[]; - logChannels: LogChannelDB; - bypassChannelBlacklist: Snowflake[]; - noXpChannels: Snowflake[]; - levelRoles: { [level: number]: Snowflake }; - levelUpChannel: Snowflake; -} - -export interface GuildModelCreationAttributes { - id: Snowflake; - prefix?: string; - autoPublishChannels?: Snowflake[]; - blacklistedChannels?: Snowflake[]; - blacklistedUsers?: Snowflake[]; - welcomeChannel?: Snowflake; - muteRole?: Snowflake; - punishmentEnding?: string; - disabledCommands?: string[]; - lockdownChannels?: Snowflake[]; - autoModPhases?: BadWords; - enabledFeatures?: GuildFeatures[]; - joinRoles?: Snowflake[]; - logChannels?: LogChannelDB; - bypassChannelBlacklist?: Snowflake[]; - noXpChannels?: Snowflake[]; - levelRoles?: { [level: number]: Snowflake }; - levelUpChannel?: Snowflake; -} - -export class Guild extends BaseModel<GuildModel, GuildModelCreationAttributes> implements GuildModel { - /** - * The ID of the guild - */ - public get id(): Snowflake { - throw new Error(NEVER_USED); - } - public set id(_: Snowflake) { - throw new Error(NEVER_USED); - } - - /** - * The bot's prefix for the guild - */ - public get prefix(): string { - throw new Error(NEVER_USED); - } - public set prefix(_: string) { - throw new Error(NEVER_USED); - } - - /** - * Channels that will have their messages automatically published - */ - public get autoPublishChannels(): Snowflake[] { - throw new Error(NEVER_USED); - } - public set autoPublishChannels(_: Snowflake[]) { - throw new Error(NEVER_USED); - } - - /** - * Channels where the bot won't respond in. - */ - public get blacklistedChannels(): Snowflake[] { - throw new Error(NEVER_USED); - } - public set blacklistedChannels(_: Snowflake[]) { - throw new Error(NEVER_USED); - } - - /** - * Users that the bot ignores in this guild - */ - public get blacklistedUsers(): Snowflake[] { - throw new Error(NEVER_USED); - } - public set blacklistedUsers(_: Snowflake[]) { - throw new Error(NEVER_USED); - } - - /** - * The channels where the welcome messages are sent - */ - public get welcomeChannel(): Snowflake { - throw new Error(NEVER_USED); - } - public set welcomeChannel(_: Snowflake) { - throw new Error(NEVER_USED); - } - - /** - * The role given out when muting someone - */ - public get muteRole(): Snowflake { - throw new Error(NEVER_USED); - } - public set muteRole(_: Snowflake) { - throw new Error(NEVER_USED); - } - - /** - * The message that gets sent after someone gets a punishment dm - */ - public get punishmentEnding(): string { - throw new Error(NEVER_USED); - } - public set punishmentEnding(_: string) { - throw new Error(NEVER_USED); - } - - /** - * Guild specific disabled commands - */ - public get disabledCommands(): string[] { - throw new Error(NEVER_USED); - } - public set disabledCommands(_: string[]) { - throw new Error(NEVER_USED); - } - - /** - * Channels that should get locked down when the lockdown command gets used. - */ - public get lockdownChannels(): Snowflake[] { - throw new Error(NEVER_USED); - } - public set lockdownChannels(_: Snowflake[]) { - throw new Error(NEVER_USED); - } - - /** - * Custom automod phases - */ - public get autoModPhases(): BadWords { - throw new Error(NEVER_USED); - } - public set autoModPhases(_: BadWords) { - throw new Error(NEVER_USED); - } - - /** - * The features enabled in a guild - */ - public get enabledFeatures(): GuildFeatures[] { - throw new Error(NEVER_USED); - } - public set enabledFeatures(_: GuildFeatures[]) { - throw new Error(NEVER_USED); - } - - /** - * The roles to assign to a user if they are not assigned sticky roles - */ - public get joinRoles(): Snowflake[] { - throw new Error(NEVER_USED); - } - public set joinRoles(_: Snowflake[]) { - throw new Error(NEVER_USED); - } - - /** - * The channels where logging messages will be sent. - */ - public get logChannels(): LogChannelDB { - throw new Error(NEVER_USED); - } - public set logChannels(_: LogChannelDB) { - throw new Error(NEVER_USED); - } - - /** - * These users will be able to use commands in channels blacklisted - */ - public get bypassChannelBlacklist(): Snowflake[] { - throw new Error(NEVER_USED); - } - public set bypassChannelBlacklist(_: Snowflake[]) { - throw new Error(NEVER_USED); - } - - /** - * Channels where users will not earn xp for leveling. - */ - public get noXpChannels(): Snowflake[] { - throw new Error(NEVER_USED); - } - public set noXpChannels(_: Snowflake[]) { - throw new Error(NEVER_USED); - } - - /** - * What roles get given to users when they reach certain levels. - */ - public get levelRoles(): { [level: number]: Snowflake } { - throw new Error(NEVER_USED); - } - public set levelRoles(_: { [level: number]: Snowflake }) { - throw new Error(NEVER_USED); - } - - /** - * The channel to send level up messages in instead of last channel. - */ - public get levelUpChannel(): Snowflake { - throw new Error(NEVER_USED); - } - public set levelUpChannel(_: Snowflake) { - throw new Error(NEVER_USED); - } - - public static initModel(sequelize: Sequelize, client: BushClient): void { - Guild.init( - { - id: { - type: DataTypes.STRING, - primaryKey: true - }, - prefix: { - type: DataTypes.TEXT, - allowNull: false, - defaultValue: client.config.prefix - }, - autoPublishChannels: jsonArrayInit('autoPublishChannels'), - blacklistedChannels: jsonArrayInit('blacklistedChannels'), - blacklistedUsers: jsonArrayInit('blacklistedChannels'), - welcomeChannel: { - type: DataTypes.STRING, - allowNull: true - }, - muteRole: { - type: DataTypes.STRING, - allowNull: true - }, - punishmentEnding: { - type: DataTypes.TEXT, - allowNull: true - }, - disabledCommands: jsonArrayInit('disabledCommands'), - lockdownChannels: jsonArrayInit('lockdownChannels'), - autoModPhases: { - type: DataTypes.TEXT, - get: function (): BadWords { - return jsonParseGet.call(this, 'autoModPhases'); - }, - set: function (val: BadWords) { - return jsonParseSet.call(this, 'autoModPhases', val); - }, - allowNull: false, - defaultValue: '{}' - }, - enabledFeatures: jsonArrayInit('enabledFeatures'), - joinRoles: jsonArrayInit('joinRoles'), - logChannels: { - type: DataTypes.TEXT, - get: function (): LogChannelDB { - return jsonParseGet.call(this, 'logChannels'); - }, - set: function (val: LogChannelDB) { - return jsonParseSet.call(this, 'logChannels', val); - }, - allowNull: false, - defaultValue: '{}' - }, - bypassChannelBlacklist: jsonArrayInit('bypassChannelBlacklist'), - noXpChannels: jsonArrayInit('noXpChannels'), - levelRoles: { - type: DataTypes.TEXT, - get: function (): { [level: number]: Snowflake } { - return jsonParseGet.call(this, 'levelRoles'); - }, - set: function (val: { [level: number]: Snowflake }) { - return jsonParseSet.call(this, 'levelRoles', val); - }, - allowNull: false, - defaultValue: '{}' - }, - levelUpChannel: { - type: DataTypes.STRING, - allowNull: true - } - }, - { sequelize: sequelize } - ); - } -} diff --git a/src/lib/models/Level.ts b/src/lib/models/Level.ts index 0960d4f..7f418fd 100644 --- a/src/lib/models/Level.ts +++ b/src/lib/models/Level.ts @@ -1,7 +1,6 @@ import { Snowflake } from 'discord.js'; import { DataTypes, Sequelize } from 'sequelize'; import { BaseModel } from './BaseModel'; -import { NEVER_USED } from './__helpers'; export interface LevelModel { user: Snowflake; @@ -15,40 +14,20 @@ export interface LevelModelCreationAttributes { xp?: number; } -export class Level extends BaseModel<LevelModel, LevelModelCreationAttributes> { - /** - * The user's id. - */ - public get user(): Snowflake { - throw new Error(NEVER_USED); - } - public set user(_: Snowflake) { - throw new Error(NEVER_USED); - } +// declaration merging so that the fields don't override Sequelize's getters +export interface Level { + /** The user's id. */ + user: Snowflake; - /** - * The guild where the user is gaining xp. - */ - public get guild(): Snowflake { - throw new Error(NEVER_USED); - } - public set guild(_: Snowflake) { - throw new Error(NEVER_USED); - } + /** The guild where the user is gaining xp. */ + guild: Snowflake; - /** - * The user's xp. - */ - public get xp(): number { - throw new Error(NEVER_USED); - } - public set xp(_: number) { - throw new Error(NEVER_USED); - } + /**The user's xp.*/ + xp: number; +} - /** - * The user's level. - */ +export class Level extends BaseModel<LevelModel, LevelModelCreationAttributes> implements LevelModel { + /** The user's level. */ public get level(): number { return Level.convertXpToLevel(this.xp); } @@ -56,24 +35,15 @@ export class Level extends BaseModel<LevelModel, LevelModelCreationAttributes> { public static initModel(sequelize: Sequelize): void { Level.init( { - user: { - type: DataTypes.STRING, - allowNull: false - }, - guild: { - type: DataTypes.STRING, - allowNull: false - }, - xp: { - type: DataTypes.INTEGER, - allowNull: false, - defaultValue: 0 - } + user: { type: DataTypes.STRING, allowNull: false }, + guild: { type: DataTypes.STRING, allowNull: false }, + xp: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0 } }, - { sequelize: sequelize } + { sequelize } ); } - static convertXpToLevel(xp: number): number { + + public static convertXpToLevel(xp: number): number { let i = 1; let lvl: number; // eslint-disable-next-line no-constant-condition @@ -88,14 +58,16 @@ export class Level extends BaseModel<LevelModel, LevelModelCreationAttributes> { } return lvl - 1; // I have to do this don't question it ok } - static convertLevelToXp(level: number): number { + + public static convertLevelToXp(level: number): number { let xp = 0; for (let i = 0; i < level; i++) { xp += 100 * i + 75; } return xp; } - static genRandomizedXp(): number { + + public static genRandomizedXp(): number { return Math.floor(Math.random() * (40 - 15 + 1)) + 15; } } diff --git a/src/lib/models/ModLog.ts b/src/lib/models/ModLog.ts index 3675649..0151cf6 100644 --- a/src/lib/models/ModLog.ts +++ b/src/lib/models/ModLog.ts @@ -2,7 +2,7 @@ import { Snowflake } from 'discord.js'; import { nanoid } from 'nanoid'; import { DataTypes, Sequelize } from 'sequelize'; import { BaseModel } from './BaseModel'; -import { jsonParseGet, jsonParseSet, NEVER_USED } from './__helpers'; +import { jsonBoolean } from './__helpers'; export enum ModLogType { PERM_BAN = 'PERM_BAN', @@ -47,171 +47,55 @@ export interface ModLogModelCreationAttributes { hidden?: boolean; } -export class ModLog extends BaseModel<ModLogModel, ModLogModelCreationAttributes> implements ModLogModel { - /** - * The primary key of the modlog entry. - */ - public get id(): string { - throw new Error(NEVER_USED); - } - public set id(_: string) { - throw new Error(NEVER_USED); - } +// declaration merging so that the fields don't override Sequelize's getters +export interface ModLog { + /** The primary key of the modlog entry. */ + id: string; - /** - * The type of punishment. - */ - public get type(): ModLogType { - throw new Error(NEVER_USED); - } - public set type(_: ModLogType) { - throw new Error(NEVER_USED); - } + /** The type of punishment. */ + type: ModLogType; - /** - * The user being punished. - */ - public get user(): Snowflake { - throw new Error(NEVER_USED); - } - public set user(_: Snowflake) { - throw new Error(NEVER_USED); - } + /** The user being punished. */ + user: Snowflake; - /** - * The user carrying out the punishment. - */ - public get moderator(): Snowflake { - throw new Error(NEVER_USED); - } - public set moderator(_: Snowflake) { - throw new Error(NEVER_USED); - } + /** The user carrying out the punishment. */ + moderator: Snowflake; - /** - * The reason the user is getting punished - */ - public get reason(): string | null { - throw new Error(NEVER_USED); - } - public set reason(_: string | null) { - throw new Error(NEVER_USED); - } + /** The reason the user is getting punished. */ + reason: string | null; - /** - * The amount of time the user is getting punished for. - */ - public get duration(): number | null { - throw new Error(NEVER_USED); - } - public set duration(_: number | null) { - throw new Error(NEVER_USED); - } + /** The amount of time the user is getting punished for. */ + duration: number | null; - /** - * The guild the user is getting punished in. - */ - public get guild(): Snowflake { - throw new Error(NEVER_USED); - } - public set guild(_: Snowflake) { - throw new Error(NEVER_USED); - } + /** The guild the user is getting punished in. */ + guild: Snowflake; - /** - * Evidence of what the user is getting punished for. - */ - public get evidence(): string { - throw new Error(NEVER_USED); - } - public set evidence(_: string) { - throw new Error(NEVER_USED); - } + /** Evidence of what the user is getting punished for. */ + evidence: string; - /** - * Not an actual modlog just used so a punishment entry can be made - */ - public get pseudo(): boolean { - throw new Error(NEVER_USED); - } - public set pseudo(_: boolean) { - throw new Error(NEVER_USED); - } + /** Not an actual modlog just used so a punishment entry can be made. */ + pseudo: boolean; - /** - * Hides from the modlog command unless show hidden is specified. - */ - public get hidden(): boolean { - throw new Error(NEVER_USED); - } - public set hidden(_: boolean) { - throw new Error(NEVER_USED); - } + /** Hides from the modlog command unless show hidden is specified. */ + hidden: boolean; +} +export class ModLog extends BaseModel<ModLogModel, ModLogModelCreationAttributes> implements ModLogModel { public static initModel(sequelize: Sequelize): void { ModLog.init( { - id: { - type: DataTypes.STRING, - primaryKey: true, - allowNull: false, - defaultValue: nanoid - }, - type: { - type: DataTypes.STRING, //# This is not an enum because of a sequelize issue: https://github.com/sequelize/sequelize/issues/2554 - allowNull: false - }, - user: { - type: DataTypes.STRING, - allowNull: false - }, - moderator: { - type: DataTypes.STRING, - allowNull: false - }, - duration: { - type: DataTypes.STRING, - allowNull: true - }, - reason: { - type: DataTypes.TEXT, - allowNull: true - }, - guild: { - type: DataTypes.STRING, - references: { - model: 'Guilds', - key: 'id' - } - }, - evidence: { - type: DataTypes.TEXT, - allowNull: true - }, - pseudo: { - type: DataTypes.STRING, - get: function (): boolean { - return jsonParseGet.call(this, 'pseudo'); - }, - set: function (val: boolean) { - return jsonParseSet.call(this, 'pseudo', val); - }, - allowNull: false, - defaultValue: 'false' - }, - hidden: { - type: DataTypes.STRING, - get: function (): boolean { - return jsonParseGet.call(this, 'hidden'); - }, - set: function (val: boolean) { - return jsonParseSet.call(this, 'hidden', val); - }, - allowNull: false, - defaultValue: 'false' - } + id: { type: DataTypes.STRING, primaryKey: true, allowNull: false, defaultValue: nanoid }, + type: { type: DataTypes.STRING, allowNull: false }, //# This is not an enum because of a sequelize issue: https://github.com/sequelize/sequelize/issues/2554 + user: { type: DataTypes.STRING, allowNull: false }, + moderator: { type: DataTypes.STRING, allowNull: false }, + duration: { type: DataTypes.STRING, allowNull: true }, + 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') }, - { sequelize: sequelize } + { sequelize } ); } } diff --git a/src/lib/models/Stat.ts b/src/lib/models/Stat.ts index 0059898..89fc00f 100644 --- a/src/lib/models/Stat.ts +++ b/src/lib/models/Stat.ts @@ -1,56 +1,34 @@ import { DataTypes, Sequelize } from 'sequelize'; import { BaseModel } from './BaseModel'; -import { NEVER_USED } from './__helpers'; +import { jsonBigint } from './__helpers'; + +type Environment = 'production' | 'development' | 'beta'; export interface StatModel { - environment: 'production' | 'development' | 'beta'; + environment: Environment; commandsUsed: bigint; } export interface StatModelCreationAttributes { - environment: 'production' | 'development' | 'beta'; + environment: Environment; commandsUsed?: bigint; } -export class Stat extends BaseModel<StatModel, StatModelCreationAttributes> implements StatModel { - /** - * The bot's environment. - */ - public get environment(): 'production' | 'development' | 'beta' { - throw new Error(NEVER_USED); - } - public set environment(_: 'production' | 'development' | 'beta') { - throw new Error(NEVER_USED); - } +// declaration merging so that the fields don't override Sequelize's getters +export interface Stat { + /** The bot's environment. */ + environment: Environment; - /** - * The number of commands used - */ - public get commandsUsed(): bigint { - throw new Error(NEVER_USED); - } - public set commandsUsed(_: bigint) { - throw new Error(NEVER_USED); - } + /** The number of commands used */ + commandsUsed: bigint; +} +export class Stat extends BaseModel<StatModel, StatModelCreationAttributes> implements StatModel { public static initModel(sequelize: Sequelize): void { Stat.init( { - environment: { - type: DataTypes.STRING, - primaryKey: true - }, - commandsUsed: { - type: DataTypes.TEXT, - allowNull: false, - get: function (): bigint { - return BigInt(this.getDataValue('commandsUsed') as unknown as string); - }, - set: function (val: bigint) { - return this.setDataValue('commandsUsed', `${val}` as any); - }, - defaultValue: '0' - } + environment: { type: DataTypes.STRING, primaryKey: true }, + commandsUsed: jsonBigint('commandsUsed') }, { sequelize } ); diff --git a/src/lib/models/StickyRole.ts b/src/lib/models/StickyRole.ts index 4c06b32..77e1889 100644 --- a/src/lib/models/StickyRole.ts +++ b/src/lib/models/StickyRole.ts @@ -1,7 +1,7 @@ import { Snowflake } from 'discord.js'; import { DataTypes, Sequelize } from 'sequelize'; import { BaseModel } from './BaseModel'; -import { jsonArrayInit, NEVER_USED } from './__helpers'; +import { jsonArray } from './__helpers'; export interface StickyRoleModel { user: Snowflake; @@ -16,63 +16,29 @@ export interface StickyRoleModelCreationAttributes { nickname?: string; } -export class StickyRole extends BaseModel<StickyRoleModel, StickyRoleModelCreationAttributes> implements StickyRoleModel { - /** - * The id of the user the roles belongs to - */ - public get user(): Snowflake { - throw new Error(NEVER_USED); - } - public set user(_: Snowflake) { - throw new Error(NEVER_USED); - } +// declaration merging so that the fields don't override Sequelize's getters +export interface StickyRole { + /** The id of the user the roles belongs to. */ + user: Snowflake; - /** - * The guild where this should happen - */ - public get guild(): Snowflake { - throw new Error(NEVER_USED); - } - public set guild(_: Snowflake) { - throw new Error(NEVER_USED); - } + /** The guild where this should happen. */ + guild: Snowflake; - /** - * The roles that the user should have returned - */ - public get roles(): Snowflake[] { - throw new Error(NEVER_USED); - } - public set roles(_: Snowflake[]) { - throw new Error(NEVER_USED); - } + /** The roles that the user should have returned */ + roles: Snowflake[]; - /** - * The user's previous nickname - */ - public get nickname(): string { - throw new Error(NEVER_USED); - } - public set nickname(_: string) { - throw new Error(NEVER_USED); - } + /** The user's previous nickname */ + nickname: string; +} +export class StickyRole extends BaseModel<StickyRoleModel, StickyRoleModelCreationAttributes> implements StickyRoleModel { public static initModel(sequelize: Sequelize): void { StickyRole.init( { - user: { - type: DataTypes.STRING, - allowNull: false - }, - guild: { - type: DataTypes.STRING, - allowNull: false - }, - roles: jsonArrayInit('roles'), - nickname: { - type: DataTypes.STRING, - allowNull: true - } + user: { type: DataTypes.STRING, allowNull: false }, + guild: { type: DataTypes.STRING, allowNull: false }, + roles: jsonArray('roles'), + nickname: { type: DataTypes.STRING, allowNull: true } }, { sequelize } ); diff --git a/src/lib/models/__helpers.ts b/src/lib/models/__helpers.ts index 243b274..dafbd84 100644 --- a/src/lib/models/__helpers.ts +++ b/src/lib/models/__helpers.ts @@ -1,6 +1,5 @@ import { DataTypes, Model } from 'sequelize'; -export const NEVER_USED = 'This should never be executed'; export function jsonParseGet(this: Model, key: string): any { return JSON.parse(this.getDataValue(key)); } @@ -8,7 +7,21 @@ export function jsonParseSet(this: Model, key: string, value: any): any { return this.setDataValue(key, JSON.stringify(value)); } -export function jsonArrayInit(key: string): any { +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): any { return { type: DataTypes.TEXT, get: function (): string[] { @@ -21,3 +34,31 @@ export function jsonArrayInit(key: string): any { 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}` + }; +} |