From ab164353c1151666a8ed339fd753099391a1f08d Mon Sep 17 00:00:00 2001 From: Federico fuji97 Rapetti Date: Mon, 10 Jan 2022 20:13:50 +0100 Subject: [PATCH] Refactor bot commands update system --- src/bot.ts | 28 ++++++++++++++++++++++------ src/events/guild-create.ts | 15 ++++++++++----- src/services/config.ts | 2 ++ 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index fd606cf..bcd6c20 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -18,17 +18,22 @@ import {Routes} from 'discord-api-types/v9'; export default class { private readonly client: Client; private readonly token: string; + private readonly env: string; 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.commandsByName = new Collection(); this.commandsByButtonId = new Collection(); } public async listen(): Promise { + // Log environment + console.log(`Starting environment: ${this.env}\n`); + // Load in commands container.getAll(TYPES.Command).forEach(command => { // TODO: remove ! @@ -117,12 +122,23 @@ export default class { // Update commands const rest = new REST({version: '9'}).setToken(this.token); - 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)}, - ); - }); + switch (this.env) { + case 'production': + // If production, set commands bot-wide + await rest.put( + Routes.applicationCommands(this.client.user!.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=applications.commands%20bot&permissions=2184236096`); }); diff --git a/src/events/guild-create.ts b/src/events/guild-create.ts index 6263673..5e0ee59 100644 --- a/src/events/guild-create.ts +++ b/src/events/guild-create.ts @@ -7,18 +7,23 @@ import {chunk} from '../utils/arrays.js'; import container from '../inversify.config.js'; import Command from '../commands'; import {TYPES} from '../types.js'; +import Config from '../services/config.js'; const DEFAULT_PREFIX = '!'; export default async (guild: Guild): Promise => { await Settings.upsert({guildId: guild.id, prefix: DEFAULT_PREFIX}); - // Setup slash commands - const commands: ApplicationCommandData[] = container.getAll(TYPES.Command) - .filter(command => command.slashCommand?.name) - .map(command => command.slashCommand as ApplicationCommandData); + const config = container.get(TYPES.Config); - await guild.commands.set(commands); + // Setup slash commands + if (config.NODE_ENV === 'production') { + const commands: ApplicationCommandData[] = container.getAll(TYPES.Command) + .filter(command => command.slashCommand?.name) + .map(command => command.slashCommand as ApplicationCommandData); + + await guild.commands.set(commands); + } const owner = await guild.client.users.fetch(guild.ownerId); diff --git a/src/services/config.ts b/src/services/config.ts index 96b161c..838873f 100644 --- a/src/services/config.ts +++ b/src/services/config.ts @@ -12,6 +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', DATA_DIR, CACHE_DIR: path.join(DATA_DIR, 'cache'), CACHE_LIMIT_IN_BYTES: xbytes.parseSize(process.env.CACHE_LIMIT ?? '2GB'), @@ -23,6 +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 DATA_DIR!: string; readonly CACHE_DIR!: string; readonly CACHE_LIMIT_IN_BYTES!: number;