diff options
-rw-r--r-- | src/bot.ts | 2 | ||||
-rw-r--r-- | src/commands/config/blacklist.ts | 4 | ||||
-rw-r--r-- | src/commands/config/disable.ts | 2 | ||||
-rw-r--r-- | src/commands/dev/superUser.ts | 2 | ||||
-rw-r--r-- | src/commands/info/help.ts | 4 | ||||
-rw-r--r-- | src/commands/info/invite.ts | 2 | ||||
-rw-r--r-- | src/config/example-options.ts | 69 | ||||
-rw-r--r-- | src/lib/extensions/discord-akairo/BushClient.ts | 28 | ||||
-rw-r--r-- | src/lib/extensions/discord-akairo/BushClientUtil.ts | 3 | ||||
-rw-r--r-- | src/lib/models/Global.ts | 6 | ||||
-rw-r--r-- | src/lib/utils/BushLogger.ts | 4 | ||||
-rw-r--r-- | src/lib/utils/Config.ts | 49 | ||||
-rw-r--r-- | src/listeners/client/ready.ts | 4 | ||||
-rw-r--r-- | src/tasks/updateCache.ts | 4 |
14 files changed, 116 insertions, 67 deletions
@@ -1,5 +1,5 @@ import 'module-alias/register'; -import * as config from './config/options'; +import config from './config/options'; import { BushClient } from './lib/'; BushClient.preStart(); diff --git a/src/commands/config/blacklist.ts b/src/commands/config/blacklist.ts index 4706041..9047c41 100644 --- a/src/commands/config/blacklist.ts +++ b/src/commands/config/blacklist.ts @@ -77,9 +77,9 @@ export default class BlacklistCommand extends BushCommand { if (global) { if (action === 'toggle') { - const blacklistedUsers = (await Global.findByPk(this.client.config.dev ? 'development' : 'production')) + const blacklistedUsers = (await Global.findByPk(this.client.config.environment)) .blacklistedUsers; - const blacklistedChannels = (await Global.findByPk(this.client.config.dev ? 'development' : 'production')) + const blacklistedChannels = (await Global.findByPk(this.client.config.environment)) .blacklistedChannels; action = blacklistedUsers.includes(targetID) || blacklistedChannels.includes(targetID) ? 'unblacklist' : 'blacklist'; } diff --git a/src/commands/config/disable.ts b/src/commands/config/disable.ts index 007cdb1..f6caf88 100644 --- a/src/commands/config/disable.ts +++ b/src/commands/config/disable.ts @@ -71,7 +71,7 @@ export default class DisableCommand extends BushCommand { if (global) { if (action === 'toggle') { - const disabledCommands = (await Global.findByPk(this.client.config.dev ? 'development' : 'production')) + const disabledCommands = (await Global.findByPk(this.client.config.environment)) .disabledCommands; action = disabledCommands.includes(commandID) ? 'disable' : 'enable'; } diff --git a/src/commands/dev/superUser.ts b/src/commands/dev/superUser.ts index 83623df..e06d064 100644 --- a/src/commands/dev/superUser.ts +++ b/src/commands/dev/superUser.ts @@ -43,7 +43,7 @@ export default class SuperUserCommand extends BushCommand { if (!message.author.isOwner()) return await message.util.reply(`${this.client.util.emojis.error} Only my developers can run this command.`); - const superUsers = (await Global.findByPk(this.client.config.dev ? 'development' : 'production')).superUsers; + const superUsers = (await Global.findByPk(this.client.config.environment)).superUsers; let success; if (args.action === 'add') { if (superUsers.includes(args.user.id)) { diff --git a/src/commands/info/help.ts b/src/commands/info/help.ts index a0b03a0..e485d24 100644 --- a/src/commands/info/help.ts +++ b/src/commands/info/help.ts @@ -46,9 +46,9 @@ export default class HelpCommand extends BushCommand { message: BushMessage | BushSlashMessage, args: { command: BushCommand | string; showHidden?: boolean } ): Promise<unknown> { - const prefix = this.client.config.dev ? 'dev ' : message.util.parsed.prefix; + const prefix = this.client.config.isDevelopment ? 'dev ' : message.util.parsed.prefix; const components = - !this.client.config.dev || !this.client.guilds.cache.some((guild) => guild.ownerId === message.author.id) + !this.client.config.isDevelopment || !this.client.guilds.cache.some((guild) => guild.ownerId === message.author.id) ? [ new MessageActionRow().addComponents( new MessageButton({ diff --git a/src/commands/info/invite.ts b/src/commands/info/invite.ts index f53e930..923c54a 100644 --- a/src/commands/info/invite.ts +++ b/src/commands/info/invite.ts @@ -19,7 +19,7 @@ export default class InviteCommand extends BushCommand { } public async exec(message: BushMessage | BushSlashMessage): Promise<unknown> { - if (this.client.config.dev) + if (this.client.config.isDevelopment) return await message.util.reply(`${this.client.util.emojis.error} The dev bot cannot be invited.`); const ButtonRow = new MessageActionRow().addComponents( new MessageButton({ diff --git a/src/config/example-options.ts b/src/config/example-options.ts index 1b5d15a..ed3fd51 100644 --- a/src/config/example-options.ts +++ b/src/config/example-options.ts @@ -1,38 +1,33 @@ -import { Snowflake } from 'discord.js'; +import { Config } from '../lib/utils/Config'; -// Credentials -export const credentials = { - token: 'Token Here', - devToken: 'Token Here', - hypixelApiKey: 'API Key Here' -}; - -// Options -export const owners: Snowflake[] = [ - '322862723090219008', //IRONM00N - '464970779944157204', //TrashCan - '487443883127472129' //Tyman -]; -export const prefix = '-' as string; -export const dev = true as boolean; -export const devGuild = '1000000000000000' as Snowflake; -export const channels: { log: Snowflake; error: Snowflake; dm: Snowflake; } = { - log: '1000000000000000', - error: '1000000000000000', - dm: '1000000000000000', -}; - -// Database specific -export const db = { - host: 'localhost', - port: 5432, - username: 'username here', - password: 'password here' -}; - -// Logging -export const logging: { db: boolean; verbose: boolean; info: boolean } = { - db: false, - verbose: true, - info: true -}; +export default new Config({ + credentials: { + token: '[TOKEN]', + betaToken: '[TOKEN]', + devToken: '[TOKEN]', + hypixelApiKey: '[API_KEY]' + }, + environment: 'development', + owners: [ + '322862723090219008', //IRONM00N + '464970779944157204', //TrashCan + '487443883127472129' //Tyman + ], + prefix: '-', + channels: { + log: '1000000000000000', + error: '1000000000000000', + dm: '1000000000000000' + }, + db: { + host: 'localhost', + port: 5432, + username: '[USER_NAME]', + password: '[PASSWORD]' + }, + logging: { + db: false, + verbose: true, + info: true + } +}); diff --git a/src/lib/extensions/discord-akairo/BushClient.ts b/src/lib/extensions/discord-akairo/BushClient.ts index 3b14200..b8e9d00 100644 --- a/src/lib/extensions/discord-akairo/BushClient.ts +++ b/src/lib/extensions/discord-akairo/BushClient.ts @@ -17,7 +17,6 @@ import readline from 'readline'; import { Sequelize } from 'sequelize'; import { contentWithDurationTypeCaster } from '../../../arguments/contentWithDuration'; import { durationTypeCaster } from '../../../arguments/duration'; -import * as config from '../../../config/options'; import { UpdateCacheTask } from '../../../tasks/updateCache'; import { Ban } from '../../models/Ban'; import { Global } from '../../models/Global'; @@ -31,6 +30,7 @@ import { AllowedMentions } from '../../utils/AllowedMentions'; import { BushCache } from '../../utils/BushCache'; import { BushConstants } from '../../utils/BushConstants'; import { BushLogger } from '../../utils/BushLogger'; +import { Config } from '../../utils/Config'; import { BushButtonInteraction } from '../discord.js/BushButtonInteraction'; import { BushCategoryChannel } from '../discord.js/BushCategoryChannel'; import { BushCommandInteraction } from '../discord.js/BushCommandInteraction'; @@ -57,7 +57,6 @@ import { BushInhibitorHandler } from './BushInhibitorHandler'; import { BushListenerHandler } from './BushListenerHandler'; import { BushTaskHandler } from './BushTaskHandler'; -export type BotConfig = typeof config; export type BushReplyMessageType = string | MessagePayload | ReplyMessageOptions; export type BushEditMessageType = string | MessageEditOptions | MessagePayload; export type BushSendMessageType = string | MessagePayload | MessageOptions; @@ -96,7 +95,7 @@ export class BushClient extends AkairoClient { Structures.extend('SelectMenuInteraction', () => BushSelectMenuInteraction); } - public config: BotConfig; + public config: Config; public listenerHandler: BushListenerHandler; public inhibitorHandler: BushInhibitorHandler; public commandHandler: BushCommandHandler; @@ -107,7 +106,7 @@ export class BushClient extends AkairoClient { public logger: BushLogger; public constants = BushConstants; public cache = BushCache; - public constructor(config: BotConfig) { + public constructor(config: Config) { super( { ownerID: config.owners, @@ -164,9 +163,9 @@ export class BushClient extends AkairoClient { this.commandHandler = new BushCommandHandler(this, { directory: path.join(__dirname, '..', '..', '..', 'commands'), prefix: async ({ guild }: { guild: Guild }) => { - if (this.config.dev) return 'dev '; + if (this.config.isDevelopment) return 'dev '; const row = await GuildModel.findByPk(guild.id); - return (row?.prefix || this.config.prefix) as string; + return (row?.prefix || this.config.isBeta ? 'bush ' : this.config.prefix) as string; }, allowMention: true, handleEdits: true, @@ -193,12 +192,17 @@ export class BushClient extends AkairoClient { }); this.util = new BushClientUtil(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: this.config.logging.db ? (sql) => this.logger.debug(sql) : false - }); + this.db = new Sequelize( + this.config.isDevelopment ? 'bushbot-dev' : 'bushbot', + this.config.db.username, + this.config.db.password, + { + dialect: 'postgres', + host: this.config.db.host, + port: this.config.db.port, + logging: this.config.logging.db ? (sql) => this.logger.debug(sql) : false + } + ); this.logger = new BushLogger(this); } diff --git a/src/lib/extensions/discord-akairo/BushClientUtil.ts b/src/lib/extensions/discord-akairo/BushClientUtil.ts index 6a08c54..46e81df 100644 --- a/src/lib/extensions/discord-akairo/BushClientUtil.ts +++ b/src/lib/extensions/discord-akairo/BushClientUtil.ts @@ -527,8 +527,7 @@ export class BushClientUtil extends ClientUtil { key: keyof typeof BushCache['global'], value: any ): Promise<Global | void> { - const environment = this.client.config.dev ? 'development' : 'production'; - const row = await Global.findByPk(environment); + const row = await Global.findByPk(this.client.config.environment); const oldValue: any[] = row[key]; const newValue = this.addOrRemoveFromArray(action, oldValue, value); row[key] = newValue; diff --git a/src/lib/models/Global.ts b/src/lib/models/Global.ts index 8664365..08392c5 100644 --- a/src/lib/models/Global.ts +++ b/src/lib/models/Global.ts @@ -3,7 +3,7 @@ import { DataTypes, Sequelize } from 'sequelize'; import { BaseModel } from './BaseModel'; export interface GlobalModel { - environment: 'production' | 'development'; + environment: 'production' | 'development'|'beta'; superUsers: Snowflake[]; disabledCommands: string[]; blacklistedUsers: Snowflake[]; @@ -12,7 +12,7 @@ export interface GlobalModel { } export interface GlobalModelCreationAttributes { - environment: 'production' | 'development'; + environment: 'production' | 'development'|'beta'; superUsers?: Snowflake[]; disabledCommands?: string[]; blacklistedUsers?: Snowflake[]; @@ -24,7 +24,7 @@ export class Global extends BaseModel<GlobalModel, GlobalModelCreationAttributes /** * The bot's environment. */ - environment: 'production' | 'development'; + environment: 'production' | 'development'|'beta'; /** * Trusted users. */ diff --git a/src/lib/utils/BushLogger.ts b/src/lib/utils/BushLogger.ts index 915d8f1..98c3c04 100644 --- a/src/lib/utils/BushLogger.ts +++ b/src/lib/utils/BushLogger.ts @@ -86,7 +86,7 @@ export class BushLogger { * @param depth - The depth the content will inspected. Defaults to 0. */ public debug(content: any, depth = 0): void { - if (!this.client.config.dev) return; + if (!this.client.config.isDevelopment) return; const newContent = this.inspectContent(content, depth, true); console.log(`${chalk.bgMagenta(this.getTimeStamp())} ${chalk.magenta('[Debug]')}`, newContent); } @@ -96,7 +96,7 @@ export class BushLogger { * @param content - The content to log. */ public debugRaw(...content: any): void { - if (!this.client.config.dev) return; + if (!this.client.config.isDevelopment) return; console.log(`${chalk.bgMagenta(this.getTimeStamp())} ${chalk.magenta('[Debug]')}`, ...content); } diff --git a/src/lib/utils/Config.ts b/src/lib/utils/Config.ts new file mode 100644 index 0000000..d6b5802 --- /dev/null +++ b/src/lib/utils/Config.ts @@ -0,0 +1,49 @@ +import { Snowflake } from 'discord.js'; + +export interface ConfigOptions { + credentials: { token: string; betaToken: string; devToken: string; hypixelApiKey: string }; + environment: 'production' | 'beta' | 'development'; + owners: Snowflake[]; + prefix: string; + channels: { log: Snowflake; error: Snowflake; dm: Snowflake }; + db: { host: string; port: number; username: string; password: string }; + logging: { db: boolean; verbose: boolean; info: boolean }; +} + +export class Config { + public credentials: { token: string; betaToken: string; devToken: string; hypixelApiKey: string }; + public environment: 'production' | 'beta' | 'development'; + public owners: Snowflake[]; + public prefix: string; + public channels: { log: Snowflake; error: Snowflake; dm: Snowflake }; + public db: { host: string; port: number; username: string; password: string }; + public logging: { db: boolean; verbose: boolean; info: boolean }; + + public constructor(options: ConfigOptions) { + this.credentials = options.credentials; + this.environment = options.environment; + this.owners = options.owners; + this.prefix = options.prefix; + this.channels = options.channels; + this.db = options.db; + this.logging = options.logging; + } + + public get token(): string { + return this.environment === 'production' + ? this.credentials.token + : this.environment === 'beta' + ? this.credentials.betaToken + : this.credentials.devToken; + } + + public get isProduction(): boolean { + return this.environment === 'production'; + } + public get isBeta(): boolean { + return this.environment === 'beta'; + } + public get isDevelopment(): boolean { + return this.environment === 'development'; + } +} diff --git a/src/listeners/client/ready.ts b/src/listeners/client/ready.ts index c3d9b45..b5d0e25 100644 --- a/src/listeners/client/ready.ts +++ b/src/listeners/client/ready.ts @@ -17,7 +17,9 @@ export default class ReadyListener extends BushListener { this.client.logger.success('Ready', `Logged in to ${tag} serving ${guildCount} guilds and ${userCount} users.`); console.log( chalk.blue( - `------------------------------------------------------------------------------${this.client.config.dev ? '---' : ''}` + `------------------------------------------------------------------------------${ + this.client.config.isDevelopment ? '---' : this.client.config.isBeta ? '----' : '' + }` ) ); } diff --git a/src/tasks/updateCache.ts b/src/tasks/updateCache.ts index 05c7dc0..3f19091 100644 --- a/src/tasks/updateCache.ts +++ b/src/tasks/updateCache.ts @@ -1,7 +1,7 @@ import { BushClient } from '../lib/extensions/discord-akairo/BushClient'; import { BushTask } from '../lib/extensions/discord-akairo/BushTask'; import { Global } from '../lib/models/Global'; -import * as config from './../config/options'; +import config from './../config/options'; export class UpdateCacheTask extends BushTask { public constructor() { @@ -20,7 +20,7 @@ export class UpdateCacheTask extends BushTask { } private static async updateCache(client: BushClient): Promise<void> { - const environment = config.dev ? 'development' : 'production'; + const environment = config.environment; const row = (await Global.findByPk(environment)) || (await Global.create({ |