From 03fdf1aab7ff5278f22c7dda9fc77e49c458d76a Mon Sep 17 00:00:00 2001 From: Federico fuji97 Rapetti Date: Wed, 12 Jan 2022 00:47:54 +0100 Subject: [PATCH] Refactor production checks --- src/bot.ts | 33 +++++++++++++++++---------------- src/events/guild-create.ts | 2 +- src/services/config.ts | 4 ++-- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index d42a718..74b56d8 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -18,21 +18,25 @@ import {Routes} from 'discord-api-types/v9'; export default class { private readonly client: Client; private readonly token: string; - private readonly env: string; + private readonly isProduction: 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.env = config.NODE_ENV; + this.isProduction = config.IS_PRODUCTION; this.commandsByName = new Collection(); this.commandsByButtonId = new Collection(); } public async listen(): Promise { // Log environment - console.log(`Starting environment: ${this.env}\n`); + if (this.isProduction) { + console.log('Production environment\n'); + } else { + console.log('Development environment\n'); + } // Load in commands container.getAll(TYPES.Command).forEach(command => { @@ -122,22 +126,19 @@ export default class { // Update commands const rest = new REST({version: '9'}).setToken(this.token); - switch (this.env) { - case 'production': - // If production, set commands bot-wide + if (this.isProduction) { + 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 + this.client.guilds.cache.each(async guild => { await rest.put( - Routes.applicationCommands(this.client.user!.id), + Routes.applicationGuildCommands(this.client.user!.id, guild.id), {body: this.commandsByName.map(command => command.slashCommand ? command.slashCommand.toJSON() : null)}, ); - break; - default: - // If development, set commands guild-wide - this.client.guilds.cache.each(async guild => { - await rest.put( - Routes.applicationGuildCommands(this.client.user!.id, guild.id), - {body: this.commandsByName.map(command => command.slashCommand ? command.slashCommand.toJSON() : null)}, - ); - }); + }); } spinner.succeed(`Ready! Invite the bot with https://discordapp.com/oauth2/authorize?client_id=${this.client.user?.id ?? ''}&scope=bot%20applications.commands&permissions=2184236096`); diff --git a/src/events/guild-create.ts b/src/events/guild-create.ts index 5e0ee59..c305527 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.NODE_ENV === 'production') { + if (config.IS_PRODUCTION) { 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 838873f..ae7ef4b 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, - NODE_ENV: process.env.NODE_ENV ?? 'development', + IS_PRODUCTION: process.env.NODE_ENV === 'production', 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 NODE_ENV!: string; + readonly IS_PRODUCTION!: boolean; readonly DATA_DIR!: string; readonly CACHE_DIR!: string; readonly CACHE_LIMIT_IN_BYTES!: number;