From 86e9936578d19c2115fc03acae1794532a09d62e Mon Sep 17 00:00:00 2001 From: Max Isom Date: Tue, 18 Jan 2022 22:09:07 -0600 Subject: Update README, naming --- src/bot.ts | 21 +++++++-------------- src/events/guild-create.ts | 2 +- src/services/config.ts | 6 ++++-- 3 files changed, 12 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/bot.ts b/src/bot.ts index cb10d1b..fb92d14 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -13,32 +13,24 @@ import Config from './services/config.js'; import {generateDependencyReport} from '@discordjs/voice'; import {REST} from '@discordjs/rest'; import {Routes} from 'discord-api-types/v9'; -import {Promise} from 'bluebird'; @injectable() export default class { private readonly client: Client; private readonly token: string; - private readonly isProduction: boolean; + private readonly shouldRegisterCommandsOnBot: boolean; private readonly commandsByName!: Collection; private readonly commandsByButtonId!: Collection; constructor(@inject(TYPES.Client) client: Client, @inject(TYPES.Config) config: Config) { this.client = client; this.token = config.DISCORD_TOKEN; - this.isProduction = config.IS_PRODUCTION; + this.shouldRegisterCommandsOnBot = config.REGISTER_COMMANDS_ON_BOT; this.commandsByName = new Collection(); this.commandsByButtonId = new Collection(); } public async listen(): Promise { - // Log environment - if (this.isProduction) { - console.log('Production environment\n'); - } else { - console.log('Development environment\n'); - } - // Load in commands container.getAll(TYPES.Command).forEach(command => { // TODO: remove ! @@ -122,18 +114,19 @@ export default class { this.client.once('ready', async () => { debug(generateDependencyReport()); - spinner.text = '📡 Updating commands in all guilds...'; - // Update commands const rest = new REST({version: '9'}).setToken(this.token); - if (this.isProduction) { + if (this.shouldRegisterCommandsOnBot) { + spinner.text = '📡 updating commands on bot...'; + await rest.put( Routes.applicationCommands(this.client.user!.id), {body: this.commandsByName.map(command => command.slashCommand ? command.slashCommand.toJSON() : null)}, ); } else { - // If development, set commands guild-wide + spinner.text = '📡 updating commands in all guilds...'; + await Promise.all( this.client.guilds.cache.map(async guild => { await rest.put( diff --git a/src/events/guild-create.ts b/src/events/guild-create.ts index 008e22b..e7a6467 100644 --- a/src/events/guild-create.ts +++ b/src/events/guild-create.ts @@ -17,7 +17,7 @@ export default async (guild: Guild): Promise => { const config = container.get(TYPES.Config); // Setup slash commands - if (!config.IS_PRODUCTION) { + if (!config.REGISTER_COMMANDS_ON_BOT) { const commands: ApplicationCommandData[] = container.getAll(TYPES.Command) .filter(command => command.slashCommand?.name) .map(command => command.slashCommand as ApplicationCommandData); diff --git a/src/services/config.ts b/src/services/config.ts index ae7ef4b..34e612c 100644 --- a/src/services/config.ts +++ b/src/services/config.ts @@ -12,7 +12,7 @@ const CONFIG_MAP = { YOUTUBE_API_KEY: process.env.YOUTUBE_API_KEY, SPOTIFY_CLIENT_ID: process.env.SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET: process.env.SPOTIFY_CLIENT_SECRET, - IS_PRODUCTION: process.env.NODE_ENV === 'production', + REGISTER_COMMANDS_ON_BOT: process.env.REGISTER_COMMANDS_ON_BOT === 'true', DATA_DIR, CACHE_DIR: path.join(DATA_DIR, 'cache'), CACHE_LIMIT_IN_BYTES: xbytes.parseSize(process.env.CACHE_LIMIT ?? '2GB'), @@ -24,7 +24,7 @@ export default class Config { readonly YOUTUBE_API_KEY!: string; readonly SPOTIFY_CLIENT_ID!: string; readonly SPOTIFY_CLIENT_SECRET!: string; - readonly IS_PRODUCTION!: boolean; + readonly REGISTER_COMMANDS_ON_BOT!: boolean; readonly DATA_DIR!: string; readonly CACHE_DIR!: string; readonly CACHE_LIMIT_IN_BYTES!: number; @@ -40,6 +40,8 @@ export default class Config { this[key as ConditionalKeys] = value; } else if (typeof value === 'string') { this[key as ConditionalKeys] = value; + } else if (typeof value === 'boolean') { + this[key as ConditionalKeys] = value; } else { throw new Error(`Unsupported type for ${key}`); } -- cgit v1.2.3