From 0caccda67d97dd74405aa4ece5d3f07e7c7dfc66 Mon Sep 17 00:00:00 2001 From: IRONM00N <64110067+IRONM00N@users.noreply.github.com> Date: Wed, 26 May 2021 21:36:50 -0400 Subject: some rebranding --- src/lib/extensions/BotClient.ts | 169 ------------------------------------ src/lib/extensions/BotCommand.ts | 20 ----- src/lib/extensions/BotInhibitor.ts | 6 -- src/lib/extensions/BotListener.ts | 6 -- src/lib/extensions/BotTask.ts | 6 -- src/lib/extensions/BushClient.ts | 169 ++++++++++++++++++++++++++++++++++++ src/lib/extensions/BushCommand.ts | 20 +++++ src/lib/extensions/BushInhibitor.ts | 6 ++ src/lib/extensions/BushListener.ts | 6 ++ src/lib/extensions/BushTask.ts | 6 ++ src/lib/extensions/Util.ts | 24 ++--- src/lib/models/Guild.ts | 4 +- src/lib/utils/Logger.ts | 6 +- 13 files changed, 224 insertions(+), 224 deletions(-) delete mode 100644 src/lib/extensions/BotClient.ts delete mode 100644 src/lib/extensions/BotCommand.ts delete mode 100644 src/lib/extensions/BotInhibitor.ts delete mode 100644 src/lib/extensions/BotListener.ts delete mode 100644 src/lib/extensions/BotTask.ts create mode 100644 src/lib/extensions/BushClient.ts create mode 100644 src/lib/extensions/BushCommand.ts create mode 100644 src/lib/extensions/BushInhibitor.ts create mode 100644 src/lib/extensions/BushListener.ts create mode 100644 src/lib/extensions/BushTask.ts (limited to 'src/lib') diff --git a/src/lib/extensions/BotClient.ts b/src/lib/extensions/BotClient.ts deleted file mode 100644 index c98f50d..0000000 --- a/src/lib/extensions/BotClient.ts +++ /dev/null @@ -1,169 +0,0 @@ -import { - AkairoClient, - CommandHandler, - InhibitorHandler, - ListenerHandler, - TaskHandler -} from 'discord-akairo'; -import { Guild } from 'discord.js'; -import * as path from 'path'; -import { Sequelize } from 'sequelize'; -import * as Models from '../models'; -import { Util } from './Util'; -import { exit } from 'process'; -import { Intents } from 'discord.js'; -import * as config from '../../config/options'; -import { Logger } from '../utils/Logger'; -import chalk from 'chalk'; - -export type BotConfig = typeof config; - -export class BotClient extends AkairoClient { - public config: BotConfig; - public listenerHandler: ListenerHandler; - public inhibitorHandler: InhibitorHandler; - public commandHandler: CommandHandler; - public taskHandler: TaskHandler; - public util: Util; - public ownerID: string[]; - public db: Sequelize; - public logger: Logger; - constructor(config: BotConfig) { - super( - { - ownerID: config.owners, - intents: Intents.NON_PRIVILEGED - }, - { - allowedMentions: { parse: ['users'] }, // No everyone or role mentions by default - intents: Intents.NON_PRIVILEGED - } - ); - - // Set token - this.token = config.credentials.botToken; - - // Set config - this.config = config; - - // Create listener handler - this.listenerHandler = new ListenerHandler(this, { - directory: path.join(__dirname, '..', '..', 'listeners'), - automateCategories: true - }); - - // Create inhibitor handler - this.inhibitorHandler = new InhibitorHandler(this, { - directory: path.join(__dirname, '..', '..', 'inhibitors'), - automateCategories: true - }); - - // Create task handler - this.taskHandler = new TaskHandler(this, { - directory: path.join(__dirname, '..', '..', 'tasks') - }); - - // Create command handler - this.commandHandler = new CommandHandler(this, { - directory: path.join(__dirname, '..', '..', 'commands'), - prefix: async ({ guild }: { guild: Guild }) => { - const row = await Models.Guild.findByPk(guild.id); - if (!row) return this.config.prefix; - return row.prefix as string; - }, - allowMention: true, - handleEdits: true, - commandUtil: true, - commandUtilLifetime: 3e5, - argumentDefaults: { - prompt: { - timeout: 'Timed out.', - ended: 'Too many tries.', - cancel: 'Canceled.', - time: 3e4 - } - }, - ignorePermissions: this.config.owners, - ignoreCooldown: this.config.owners, - automateCategories: true - }); - - this.util = new Util(this); - this.db = new Sequelize( - this.config.dev ? 'bushbot-dev' : 'bushbot', - this.config.db.username, - this.config.db.password, - { - dialect: 'postgres', - host: this.config.db.host, - port: this.config.db.port, - logging: false - } - ); - this.logger = new Logger(this); - } - - // Initialize everything - private async _init(): Promise { - this.commandHandler.useListenerHandler(this.listenerHandler); - this.commandHandler.useInhibitorHandler(this.inhibitorHandler); - this.listenerHandler.setEmitters({ - commandHandler: this.commandHandler, - listenerHandler: this.listenerHandler, - process - }); - // loads all the handlers - const loaders = { - commands: this.commandHandler, - listeners: this.listenerHandler, - inhibitors: this.inhibitorHandler, - tasks: this.taskHandler - }; - for (const loader of Object.keys(loaders)) { - try { - loaders[loader].loadAll(); - this.logger.log( - chalk.green('Successfully loaded ' + chalk.cyan(loader) + '.') - ); - } catch (e) { - console.error( - chalk.red( - 'Unable to load loader ' + chalk.cyan(loader) + ' with error ' + e - ) - ); - } - } - this.taskHandler.startAll(); - await this.dbPreInit(); - } - - public async dbPreInit(): Promise { - await this.db.authenticate(); - 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 { - // Ignore error - } - } - - public async start(): Promise { - try { - await this._init(); - await this.login(this.token); - } catch (e) { - console.error(chalk.red(e.stack)); - exit(2); - } - } - - public destroy(relogin = true): void | Promise { - super.destroy(); - if (relogin) { - return this.login(this.token); - } - } -} diff --git a/src/lib/extensions/BotCommand.ts b/src/lib/extensions/BotCommand.ts deleted file mode 100644 index c5d31e9..0000000 --- a/src/lib/extensions/BotCommand.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { Command, CommandOptions } from 'discord-akairo'; -import { APIApplicationCommandOption } from 'discord-api-types'; -import { BotClient } from './BotClient'; - -export interface BotCommandOptions extends CommandOptions { - slashCommandOptions?: APIApplicationCommandOption[]; - description: { - content: string; - usage: string; - examples: string[]; - }; -} - -export class BotCommand extends Command { - public client: BotClient; - constructor(id: string, options?: BotCommandOptions) { - super(id, options); - this.options = options; - } -} diff --git a/src/lib/extensions/BotInhibitor.ts b/src/lib/extensions/BotInhibitor.ts deleted file mode 100644 index 960aade..0000000 --- a/src/lib/extensions/BotInhibitor.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Inhibitor } from 'discord-akairo'; -import { BotClient } from './BotClient'; - -export class BotInhibitor extends Inhibitor { - public client: BotClient; -} diff --git a/src/lib/extensions/BotListener.ts b/src/lib/extensions/BotListener.ts deleted file mode 100644 index 9ec17b2..0000000 --- a/src/lib/extensions/BotListener.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Listener } from 'discord-akairo'; -import { BotClient } from './BotClient'; - -export class BotListener extends Listener { - public client: BotClient; -} diff --git a/src/lib/extensions/BotTask.ts b/src/lib/extensions/BotTask.ts deleted file mode 100644 index 4d5cdcd..0000000 --- a/src/lib/extensions/BotTask.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Task } from 'discord-akairo'; -import { BotClient } from './BotClient'; - -export class BotTask extends Task { - public client: BotClient; -} diff --git a/src/lib/extensions/BushClient.ts b/src/lib/extensions/BushClient.ts new file mode 100644 index 0000000..8a8ae91 --- /dev/null +++ b/src/lib/extensions/BushClient.ts @@ -0,0 +1,169 @@ +import { + AkairoClient, + CommandHandler, + InhibitorHandler, + ListenerHandler, + TaskHandler +} from 'discord-akairo'; +import { Guild } from 'discord.js'; +import * as path from 'path'; +import { Sequelize } from 'sequelize'; +import * as Models from '../models'; +import { Util } from './Util'; +import { exit } from 'process'; +import { Intents } from 'discord.js'; +import * as config from '../../config/options'; +import { Logger } from '../utils/Logger'; +import chalk from 'chalk'; + +export type BotConfig = typeof config; + +export class BushClient extends AkairoClient { + public config: BotConfig; + public listenerHandler: ListenerHandler; + public inhibitorHandler: InhibitorHandler; + public commandHandler: CommandHandler; + public taskHandler: TaskHandler; + public util: Util; + public ownerID: string[]; + public db: Sequelize; + public logger: Logger; + constructor(config: BotConfig) { + super( + { + ownerID: config.owners, + intents: Intents.NON_PRIVILEGED + }, + { + allowedMentions: { parse: ['users'] }, // No everyone or role mentions by default + intents: Intents.NON_PRIVILEGED + } + ); + + // Set token + this.token = config.credentials.botToken; + + // Set config + this.config = config; + + // Create listener handler + this.listenerHandler = new ListenerHandler(this, { + directory: path.join(__dirname, '..', '..', 'listeners'), + automateCategories: true + }); + + // Create inhibitor handler + this.inhibitorHandler = new InhibitorHandler(this, { + directory: path.join(__dirname, '..', '..', 'inhibitors'), + automateCategories: true + }); + + // Create task handler + this.taskHandler = new TaskHandler(this, { + directory: path.join(__dirname, '..', '..', 'tasks') + }); + + // Create command handler + this.commandHandler = new CommandHandler(this, { + directory: path.join(__dirname, '..', '..', 'commands'), + prefix: async ({ guild }: { guild: Guild }) => { + const row = await Models.Guild.findByPk(guild.id); + if (!row) return this.config.prefix; + return row.prefix as string; + }, + allowMention: true, + handleEdits: true, + commandUtil: true, + commandUtilLifetime: 3e5, + argumentDefaults: { + prompt: { + timeout: 'Timed out.', + ended: 'Too many tries.', + cancel: 'Canceled.', + time: 3e4 + } + }, + ignorePermissions: this.config.owners, + ignoreCooldown: this.config.owners, + automateCategories: true + }); + + this.util = new Util(this); + this.db = new Sequelize( + this.config.dev ? 'bushbot-dev' : 'bushbot', + this.config.db.username, + this.config.db.password, + { + dialect: 'postgres', + host: this.config.db.host, + port: this.config.db.port, + logging: false + } + ); + this.logger = new Logger(this); + } + + // Initialize everything + private async _init(): Promise { + this.commandHandler.useListenerHandler(this.listenerHandler); + this.commandHandler.useInhibitorHandler(this.inhibitorHandler); + this.listenerHandler.setEmitters({ + commandHandler: this.commandHandler, + listenerHandler: this.listenerHandler, + process + }); + // loads all the handlers + const loaders = { + commands: this.commandHandler, + listeners: this.listenerHandler, + inhibitors: this.inhibitorHandler, + tasks: this.taskHandler + }; + for (const loader of Object.keys(loaders)) { + try { + loaders[loader].loadAll(); + this.logger.log( + chalk.green('Successfully loaded ' + chalk.cyan(loader) + '.') + ); + } catch (e) { + console.error( + chalk.red( + 'Unable to load loader ' + chalk.cyan(loader) + ' with error ' + e + ) + ); + } + } + this.taskHandler.startAll(); + await this.dbPreInit(); + } + + public async dbPreInit(): Promise { + await this.db.authenticate(); + 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 { + // Ignore error + } + } + + public async start(): Promise { + try { + await this._init(); + await this.login(this.token); + } catch (e) { + console.error(chalk.red(e.stack)); + exit(2); + } + } + + public destroy(relogin = true): void | Promise { + super.destroy(); + if (relogin) { + return this.login(this.token); + } + } +} diff --git a/src/lib/extensions/BushCommand.ts b/src/lib/extensions/BushCommand.ts new file mode 100644 index 0000000..4f9dc6e --- /dev/null +++ b/src/lib/extensions/BushCommand.ts @@ -0,0 +1,20 @@ +import { Command, CommandOptions } from 'discord-akairo'; +import { APIApplicationCommandOption } from 'discord-api-types'; +import { BushClient } from './BushClient'; + +export interface BushCommandOptions extends CommandOptions { + slashCommandOptions?: APIApplicationCommandOption[]; + description: { + content: string; + usage: string; + examples: string[]; + }; +} + +export class BushCommand extends Command { + public client: BushClient; + constructor(id: string, options?: BushCommandOptions) { + super(id, options); + this.options = options; + } +} diff --git a/src/lib/extensions/BushInhibitor.ts b/src/lib/extensions/BushInhibitor.ts new file mode 100644 index 0000000..d9a9b68 --- /dev/null +++ b/src/lib/extensions/BushInhibitor.ts @@ -0,0 +1,6 @@ +import { Inhibitor } from 'discord-akairo'; +import { BushClient } from './BushClient'; + +export class BushInhibitor extends Inhibitor { + public client: BushClient; +} diff --git a/src/lib/extensions/BushListener.ts b/src/lib/extensions/BushListener.ts new file mode 100644 index 0000000..6a13210 --- /dev/null +++ b/src/lib/extensions/BushListener.ts @@ -0,0 +1,6 @@ +import { Listener } from 'discord-akairo'; +import { BushClient } from './BushClient'; + +export class BushListener extends Listener { + public client: BushClient; +} diff --git a/src/lib/extensions/BushTask.ts b/src/lib/extensions/BushTask.ts new file mode 100644 index 0000000..21655e9 --- /dev/null +++ b/src/lib/extensions/BushTask.ts @@ -0,0 +1,6 @@ +import { Task } from 'discord-akairo'; +import { BushClient } from './BushClient'; + +export class BushTask extends Task { + public client: BushClient; +} diff --git a/src/lib/extensions/Util.ts b/src/lib/extensions/Util.ts index 54df670..7c1d4b2 100644 --- a/src/lib/extensions/Util.ts +++ b/src/lib/extensions/Util.ts @@ -1,5 +1,5 @@ import { ClientUtil } from 'discord-akairo'; -import { BotClient } from './BotClient'; +import { BushClient } from './BushClient'; import { promisify } from 'util'; import { exec } from 'child_process'; import got from 'got'; @@ -57,9 +57,9 @@ export interface SlashCommandOption { export class Util extends ClientUtil { /** * The client of this ClientUtil - * @type {BotClient} + * @type {BushClient} */ - public client: BotClient; + public client: BushClient; /** * The hastebin urls used to post to hastebin, attempts to post in order * @type {string[]} @@ -83,7 +83,7 @@ export class Util extends ClientUtil { * Creates this client util * @param client The client to initialize with */ - constructor(client: BotClient) { + constructor(client: BushClient) { super(client); } @@ -273,14 +273,14 @@ export class Util extends ClientUtil { } } - for (const [, botCommand] of this.client.commandHandler.modules) { - if (botCommand.execSlash) { - const found = registered.find((i) => i.name == botCommand.id); + for (const [, BushCommand] of this.client.commandHandler.modules) { + if (BushCommand.execSlash) { + const found = registered.find((i) => i.name == BushCommand.id); const slashdata = { - name: botCommand.id, - description: botCommand.description.content, - options: botCommand.options.slashCommandOptions + name: BushCommand.id, + description: BushCommand.description.content, + options: BushCommand.options.slashCommandOptions }; if (found?.id && !force) { @@ -292,7 +292,7 @@ export class Util extends ClientUtil { ) : fetchedGuild.commands.edit(found.id, slashdata); this.client.logger.verbose( - chalk`{yellow Edited slash command ${botCommand.id}${ + chalk`{yellow Edited slash command ${BushCommand.id}${ guild !== undefined ? ` in guild ${fetchedGuild.name}` : '' }}` ); @@ -302,7 +302,7 @@ export class Util extends ClientUtil { ? await this.client.application.commands.create(slashdata) : fetchedGuild.commands.create(slashdata); this.client.logger.verbose( - chalk`{green Created slash command ${botCommand.id}${ + chalk`{green Created slash command ${BushCommand.id}${ guild !== undefined ? ` in guild ${fetchedGuild.name}` : '' }}` ); diff --git a/src/lib/models/Guild.ts b/src/lib/models/Guild.ts index 1cb3abb..c224bf5 100644 --- a/src/lib/models/Guild.ts +++ b/src/lib/models/Guild.ts @@ -1,5 +1,5 @@ import { DataTypes, Optional, Sequelize } from 'sequelize'; -import { BotClient } from '../extensions/BotClient'; +import { BushClient } from '../extensions/BushClient'; import { BaseModel } from './BaseModel'; export interface GuildModel { @@ -14,7 +14,7 @@ export class Guild { id: string; prefix: string; - static initModel(seqeulize: Sequelize, client: BotClient): void { + static initModel(seqeulize: Sequelize, client: BushClient): void { Guild.init( { id: { diff --git a/src/lib/utils/Logger.ts b/src/lib/utils/Logger.ts index f38365e..604b45e 100644 --- a/src/lib/utils/Logger.ts +++ b/src/lib/utils/Logger.ts @@ -1,10 +1,10 @@ import { TextChannel } from 'discord.js'; -import { BotClient } from '../extensions/BotClient'; +import { BushClient } from '../extensions/BushClient'; import chalk from 'chalk'; export class Logger { - private client: BotClient; - public constructor(client: BotClient) { + private client: BushClient; + public constructor(client: BushClient) { this.client = client; } private stripColor(text: string): string { -- cgit