diff options
| author | Federico fuji97 Rapetti <[email protected]> | 2022-01-12 00:47:54 +0100 |
|---|---|---|
| committer | Federico fuji97 Rapetti <[email protected]> | 2022-01-12 00:47:54 +0100 |
| commit | 03fdf1aab7ff5278f22c7dda9fc77e49c458d76a (patch) | |
| tree | 651368ee2eb8f244ceedcde0c31c3d83b9cfc4a4 /src | |
| parent | 19848ba9f71cb59c4e1b5a6a7e2ea567a192365f (diff) | |
| download | muse-03fdf1aab7ff5278f22c7dda9fc77e49c458d76a.tar.xz muse-03fdf1aab7ff5278f22c7dda9fc77e49c458d76a.zip | |
Refactor production checks
Diffstat (limited to 'src')
| -rw-r--r-- | src/bot.ts | 33 | ||||
| -rw-r--r-- | src/events/guild-create.ts | 2 | ||||
| -rw-r--r-- | src/services/config.ts | 4 |
3 files changed, 20 insertions, 19 deletions
@@ -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<string, Command>; private readonly commandsByButtonId!: Collection<string, Command>; 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<void> { // 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<Command>(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<void> => { const config = container.get<Config>(TYPES.Config); // Setup slash commands - if (config.NODE_ENV === 'production') { + if (config.IS_PRODUCTION) { const commands: ApplicationCommandData[] = container.getAll<Command>(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; |
