Refactor production checks

This commit is contained in:
Federico fuji97 Rapetti 2022-01-12 00:47:54 +01:00
parent 19848ba9f7
commit 03fdf1aab7
3 changed files with 20 additions and 19 deletions

View file

@ -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,15 +126,12 @@ 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)},
);
break;
default:
} else {
// If development, set commands guild-wide
this.client.guilds.cache.each(async guild => {
await rest.put(

View file

@ -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);

View file

@ -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;