From 284e1d1d693486f6c50cdb8b38f01cdf74eb63d2 Mon Sep 17 00:00:00 2001 From: TymanWasTaken Date: Sun, 16 May 2021 21:40:52 -0400 Subject: fix logging for slash command syncing, rename a few files --- src/lib/extensions/BotClient.ts | 118 ++-------------------------- src/lib/models/Ban.ts | 44 +++++++++++ src/lib/models/Guild.ts | 19 ++++- src/lib/models/Level.ts | 19 +++++ src/lib/models/Modlog.ts | 50 ++++++++++++ src/listeners/client/CreateSlashCommands.ts | 58 -------------- src/listeners/client/syncslashcommands.ts | 61 ++++++++++++++ src/listeners/guild/Unban.ts | 25 ------ src/listeners/guild/syncunban.ts | 25 ++++++ 9 files changed, 222 insertions(+), 197 deletions(-) delete mode 100644 src/listeners/client/CreateSlashCommands.ts create mode 100644 src/listeners/client/syncslashcommands.ts delete mode 100644 src/listeners/guild/Unban.ts create mode 100644 src/listeners/guild/syncunban.ts (limited to 'src') diff --git a/src/lib/extensions/BotClient.ts b/src/lib/extensions/BotClient.ts index 99b40ef..76b1a1b 100644 --- a/src/lib/extensions/BotClient.ts +++ b/src/lib/extensions/BotClient.ts @@ -6,13 +6,12 @@ import { } from 'discord-akairo'; import { Guild } from 'discord.js'; import * as path from 'path'; -import { DataTypes, Sequelize } from 'sequelize'; +import { Sequelize } from 'sequelize'; import * as Models from '../models'; import { BotGuild } from './BotGuild'; import { BotMessage } from './BotMessage'; import { Util } from './Util'; import * as Tasks from '../../tasks'; -import { v4 as uuidv4 } from 'uuid'; import { exit } from 'process'; import { Intents } from 'discord.js'; import * as config from '../../config/options'; @@ -139,117 +138,10 @@ export class BotClient extends AkairoClient { public async dbPreInit(): Promise { await this.db.authenticate(); - Models.Guild.init( - { - id: { - type: DataTypes.STRING, - primaryKey: true - }, - prefix: { - type: DataTypes.STRING, - allowNull: false, - defaultValue: this.config.prefix - } - }, - { sequelize: this.db } - ); - Models.Modlog.init( - { - id: { - type: DataTypes.STRING, - primaryKey: true, - allowNull: false, - defaultValue: uuidv4 - }, - type: { - type: new DataTypes.ENUM( - 'BAN', - 'TEMPBAN', - 'MUTE', - 'TEMPMUTE', - 'KICK', - 'WARN' - ), - allowNull: false - }, - user: { - type: DataTypes.STRING, - allowNull: false - }, - moderator: { - type: DataTypes.STRING, - allowNull: false - }, - duration: { - type: DataTypes.STRING, - allowNull: true - }, - reason: { - type: DataTypes.STRING, - allowNull: true - }, - guild: { - type: DataTypes.STRING, - references: { - model: Models.Guild - } - } - }, - { sequelize: this.db } - ); - Models.Ban.init( - { - id: { - type: DataTypes.STRING, - primaryKey: true, - allowNull: false, - defaultValue: uuidv4 - }, - user: { - type: DataTypes.STRING, - allowNull: false - }, - guild: { - type: DataTypes.STRING, - allowNull: false, - references: { - model: Models.Guild, - key: 'id' - } - }, - expires: { - type: DataTypes.DATE, - allowNull: true - }, - reason: { - type: DataTypes.STRING, - allowNull: true - }, - modlog: { - type: DataTypes.STRING, - allowNull: false, - references: { - model: Models.Modlog - } - } - }, - { sequelize: this.db } - ); - Models.Level.init( - { - id: { - type: DataTypes.STRING, - primaryKey: true, - allowNull: false - }, - xp: { - type: DataTypes.INTEGER, - allowNull: false, - defaultValue: 0 - } - }, - { sequelize: this.db } - ); + Models.Guild.initModel(this.db, this); + Models.Modlog.initModel(this.db); + Models.Ban.initModel(this.db); + Models.Level.initModel(this.db); try { await this.db.sync(); // Sync all tables to fix everything if updated } catch { diff --git a/src/lib/models/Ban.ts b/src/lib/models/Ban.ts index 032a48b..c30c4c5 100644 --- a/src/lib/models/Ban.ts +++ b/src/lib/models/Ban.ts @@ -1,4 +1,7 @@ +import { DataTypes, Sequelize } from 'sequelize'; import { BaseModel } from './BaseModel'; +import * as Models from './'; +import { v4 as uuidv4 } from 'uuid'; export interface BanModel { id: string; @@ -44,4 +47,45 @@ export class Ban * The ref to the modlog entry */ modlog: string; + + static initModel(sequelize: Sequelize): void { + Ban.init( + { + id: { + type: DataTypes.STRING, + primaryKey: true, + allowNull: false, + defaultValue: uuidv4 + }, + user: { + type: DataTypes.STRING, + allowNull: false + }, + guild: { + type: DataTypes.STRING, + allowNull: false, + references: { + model: Models.Guild, + key: 'id' + } + }, + expires: { + type: DataTypes.DATE, + allowNull: true + }, + reason: { + type: DataTypes.STRING, + allowNull: true + }, + modlog: { + type: DataTypes.STRING, + allowNull: false, + references: { + model: Models.Modlog + } + } + }, + { sequelize: sequelize } + ); + } } diff --git a/src/lib/models/Guild.ts b/src/lib/models/Guild.ts index 3e6c6bf..e4e317f 100644 --- a/src/lib/models/Guild.ts +++ b/src/lib/models/Guild.ts @@ -1,4 +1,5 @@ -import { Optional } from 'sequelize'; +import { DataTypes, Optional, Sequelize } from 'sequelize'; +import { BotClient } from '../extensions/BotClient'; import { BaseModel } from './BaseModel'; export interface GuildModel { @@ -12,4 +13,20 @@ export class Guild implements GuildModel { id: string; prefix: string; + static initModel(seqeulize: Sequelize, client: BotClient): void { + Guild.init( + { + id: { + type: DataTypes.STRING, + primaryKey: true + }, + prefix: { + type: DataTypes.STRING, + allowNull: false, + defaultValue: client.config.prefix + } + }, + { sequelize: seqeulize } + ); + } } diff --git a/src/lib/models/Level.ts b/src/lib/models/Level.ts index f08f29b..426ec1a 100644 --- a/src/lib/models/Level.ts +++ b/src/lib/models/Level.ts @@ -1,3 +1,4 @@ +import { DataTypes, Sequelize } from 'sequelize'; import { BaseModel } from './BaseModel'; export interface LevelModel { @@ -16,6 +17,24 @@ export class Level extends BaseModel { get level(): number { return Level.convertXpToLevel(this.xp); } + + static initModel(sequelize: Sequelize): void { + Level.init( + { + id: { + type: DataTypes.STRING, + primaryKey: true, + allowNull: false + }, + xp: { + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: 0 + } + }, + { sequelize: sequelize } + ); + } static convertXpToLevel(xp: number): number { let i = 1; let lvl: number; diff --git a/src/lib/models/Modlog.ts b/src/lib/models/Modlog.ts index 0a3feba..6dd5e26 100644 --- a/src/lib/models/Modlog.ts +++ b/src/lib/models/Modlog.ts @@ -1,4 +1,7 @@ +import { DataTypes, Sequelize } from 'sequelize'; import { BaseModel } from './BaseModel'; +import { v4 as uuidv4 } from 'uuid'; +import * as Models from './'; export enum ModlogType { BAN = 'BAN', @@ -39,4 +42,51 @@ export class Modlog guild: string; reason: string | null; duration: number | null; + + static initModel(sequelize: Sequelize): void { + Modlog.init( + { + id: { + type: DataTypes.STRING, + primaryKey: true, + allowNull: false, + defaultValue: uuidv4 + }, + type: { + type: new DataTypes.ENUM( + 'BAN', + 'TEMPBAN', + 'MUTE', + 'TEMPMUTE', + 'KICK', + 'WARN' + ), + allowNull: false + }, + user: { + type: DataTypes.STRING, + allowNull: false + }, + moderator: { + type: DataTypes.STRING, + allowNull: false + }, + duration: { + type: DataTypes.STRING, + allowNull: true + }, + reason: { + type: DataTypes.STRING, + allowNull: true + }, + guild: { + type: DataTypes.STRING, + references: { + model: Models.Guild + } + } + }, + { sequelize: sequelize } + ); + } } diff --git a/src/listeners/client/CreateSlashCommands.ts b/src/listeners/client/CreateSlashCommands.ts deleted file mode 100644 index c395b3a..0000000 --- a/src/listeners/client/CreateSlashCommands.ts +++ /dev/null @@ -1,58 +0,0 @@ -import chalk from 'chalk'; -import { BotListener } from '../../lib/extensions/BotListener'; - -export default class CreateSlashCommands extends BotListener { - constructor() { - super('createslashcommands', { - emitter: 'client', - event: 'ready' - }); - } - async exec(): Promise { - try { - const enabled = await this.client.application.commands.fetch(); - for (const command of enabled) { - if ( - !this.client.commandHandler.modules.find( - (cmd) => cmd.id == command[1].name - ) - ) { - await this.client.application.commands.delete(command[1].id); - this.client.logger.verbose( - `{red Deleted slash command ${command[1].name}}` - ); - } - } - - for (const cmd of this.client.commandHandler.modules) { - if (cmd[1].execSlash) { - const found = enabled.find((i) => i.name == cmd[1].id); - - const slashdata = { - name: cmd[1].id, - description: cmd[1].description.content, - options: cmd[1].options.slashCommandOptions - }; - - if (found?.id) { - if (slashdata.description !== found.description) { - await this.client.application.commands.edit(found.id, slashdata); - } - } else { - this.client.logger.verbose( - `{red Deleted slash command ${cmd[1].id}}` - ); - await this.client.application.commands.create(slashdata); - } - } - } - - return this.client.logger.log(chalk.green('Slash commands registered')); - } catch (e) { - console.log(chalk.red(e)); - return this.client.logger.error( - '{red Slash commands not registered, see above error.}' - ); - } - } -} diff --git a/src/listeners/client/syncslashcommands.ts b/src/listeners/client/syncslashcommands.ts new file mode 100644 index 0000000..2388104 --- /dev/null +++ b/src/listeners/client/syncslashcommands.ts @@ -0,0 +1,61 @@ +import chalk from 'chalk'; +import { BotListener } from '../../lib/extensions/BotListener'; + +export default class CreateSlashCommands extends BotListener { + constructor() { + super('createslashcommands', { + emitter: 'client', + event: 'ready' + }); + } + async exec(): Promise { + try { + const registered = await this.client.application.commands.fetch(); + for (const [, registeredCommand] of registered) { + if ( + !this.client.commandHandler.modules.find( + (cmd) => cmd.id == registeredCommand.name + ) + ) { + await this.client.application.commands.delete(registeredCommand.id); + this.client.logger.verbose( + `{red Deleted slash command ${registeredCommand.name}}` + ); + } + } + + for (const [, botCommand] of this.client.commandHandler.modules) { + if (botCommand.execSlash) { + const found = registered.find((i) => i.name == botCommand.id); + + const slashdata = { + name: botCommand.id, + description: botCommand.description.content, + options: botCommand.options.slashCommandOptions + }; + + if (found?.id) { + if (slashdata.description !== found.description) { + await this.client.application.commands.edit(found.id, slashdata); + this.client.logger.verbose( + `{orange Edited slash command ${botCommand.id}}` + ); + } + } else { + await this.client.application.commands.create(slashdata); + this.client.logger.verbose( + `{green Created slash command ${botCommand.id}}` + ); + } + } + } + + return this.client.logger.log(chalk.green('Slash commands registered')); + } catch (e) { + console.log(chalk.red(e)); + return this.client.logger.error( + '{red Slash commands not registered, see above error.}' + ); + } + } +} diff --git a/src/listeners/guild/Unban.ts b/src/listeners/guild/Unban.ts deleted file mode 100644 index 63267bd..0000000 --- a/src/listeners/guild/Unban.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { User } from 'discord.js'; -import { BotGuild } from '../../lib/extensions/BotGuild'; -import { BotListener } from '../../lib/extensions/BotListener'; -import { Ban } from '../../lib/models'; - -export default class CommandBlockedListener extends BotListener { - public constructor() { - super('guildBanRemove', { - emitter: 'client', - event: 'guildBanRemove' - }); - } - - public async exec(guild: BotGuild, user: User): Promise { - const bans = await Ban.findAll({ - where: { - user: user.id, - guild: guild.id - } - }); - for (const dbBan of bans) { - await dbBan.destroy(); - } - } -} diff --git a/src/listeners/guild/syncunban.ts b/src/listeners/guild/syncunban.ts new file mode 100644 index 0000000..63267bd --- /dev/null +++ b/src/listeners/guild/syncunban.ts @@ -0,0 +1,25 @@ +import { User } from 'discord.js'; +import { BotGuild } from '../../lib/extensions/BotGuild'; +import { BotListener } from '../../lib/extensions/BotListener'; +import { Ban } from '../../lib/models'; + +export default class CommandBlockedListener extends BotListener { + public constructor() { + super('guildBanRemove', { + emitter: 'client', + event: 'guildBanRemove' + }); + } + + public async exec(guild: BotGuild, user: User): Promise { + const bans = await Ban.findAll({ + where: { + user: user.id, + guild: guild.id + } + }); + for (const dbBan of bans) { + await dbBan.destroy(); + } + } +} -- cgit