mirror of
https://github.com/BluemediaGER/muse.git
synced 2024-11-23 09:15:29 +01:00
Refactor production checks
This commit is contained in:
parent
19848ba9f7
commit
03fdf1aab7
17
src/bot.ts
17
src/bot.ts
|
@ -18,21 +18,25 @@ import {Routes} from 'discord-api-types/v9';
|
||||||
export default class {
|
export default class {
|
||||||
private readonly client: Client;
|
private readonly client: Client;
|
||||||
private readonly token: string;
|
private readonly token: string;
|
||||||
private readonly env: string;
|
private readonly isProduction: boolean;
|
||||||
private readonly commandsByName!: Collection<string, Command>;
|
private readonly commandsByName!: Collection<string, Command>;
|
||||||
private readonly commandsByButtonId!: Collection<string, Command>;
|
private readonly commandsByButtonId!: Collection<string, Command>;
|
||||||
|
|
||||||
constructor(@inject(TYPES.Client) client: Client, @inject(TYPES.Config) config: Config) {
|
constructor(@inject(TYPES.Client) client: Client, @inject(TYPES.Config) config: Config) {
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.token = config.DISCORD_TOKEN;
|
this.token = config.DISCORD_TOKEN;
|
||||||
this.env = config.NODE_ENV;
|
this.isProduction = config.IS_PRODUCTION;
|
||||||
this.commandsByName = new Collection();
|
this.commandsByName = new Collection();
|
||||||
this.commandsByButtonId = new Collection();
|
this.commandsByButtonId = new Collection();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async listen(): Promise<void> {
|
public async listen(): Promise<void> {
|
||||||
// Log environment
|
// 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
|
// Load in commands
|
||||||
container.getAll<Command>(TYPES.Command).forEach(command => {
|
container.getAll<Command>(TYPES.Command).forEach(command => {
|
||||||
|
@ -122,15 +126,12 @@ export default class {
|
||||||
// Update commands
|
// Update commands
|
||||||
const rest = new REST({version: '9'}).setToken(this.token);
|
const rest = new REST({version: '9'}).setToken(this.token);
|
||||||
|
|
||||||
switch (this.env) {
|
if (this.isProduction) {
|
||||||
case 'production':
|
|
||||||
// If production, set commands bot-wide
|
|
||||||
await rest.put(
|
await rest.put(
|
||||||
Routes.applicationCommands(this.client.user!.id),
|
Routes.applicationCommands(this.client.user!.id),
|
||||||
{body: this.commandsByName.map(command => command.slashCommand ? command.slashCommand.toJSON() : null)},
|
{body: this.commandsByName.map(command => command.slashCommand ? command.slashCommand.toJSON() : null)},
|
||||||
);
|
);
|
||||||
break;
|
} else {
|
||||||
default:
|
|
||||||
// If development, set commands guild-wide
|
// If development, set commands guild-wide
|
||||||
this.client.guilds.cache.each(async guild => {
|
this.client.guilds.cache.each(async guild => {
|
||||||
await rest.put(
|
await rest.put(
|
||||||
|
|
|
@ -17,7 +17,7 @@ export default async (guild: Guild): Promise<void> => {
|
||||||
const config = container.get<Config>(TYPES.Config);
|
const config = container.get<Config>(TYPES.Config);
|
||||||
|
|
||||||
// Setup slash commands
|
// Setup slash commands
|
||||||
if (config.NODE_ENV === 'production') {
|
if (config.IS_PRODUCTION) {
|
||||||
const commands: ApplicationCommandData[] = container.getAll<Command>(TYPES.Command)
|
const commands: ApplicationCommandData[] = container.getAll<Command>(TYPES.Command)
|
||||||
.filter(command => command.slashCommand?.name)
|
.filter(command => command.slashCommand?.name)
|
||||||
.map(command => command.slashCommand as ApplicationCommandData);
|
.map(command => command.slashCommand as ApplicationCommandData);
|
||||||
|
|
|
@ -12,7 +12,7 @@ const CONFIG_MAP = {
|
||||||
YOUTUBE_API_KEY: process.env.YOUTUBE_API_KEY,
|
YOUTUBE_API_KEY: process.env.YOUTUBE_API_KEY,
|
||||||
SPOTIFY_CLIENT_ID: process.env.SPOTIFY_CLIENT_ID,
|
SPOTIFY_CLIENT_ID: process.env.SPOTIFY_CLIENT_ID,
|
||||||
SPOTIFY_CLIENT_SECRET: process.env.SPOTIFY_CLIENT_SECRET,
|
SPOTIFY_CLIENT_SECRET: process.env.SPOTIFY_CLIENT_SECRET,
|
||||||
NODE_ENV: process.env.NODE_ENV ?? 'development',
|
IS_PRODUCTION: process.env.NODE_ENV === 'production',
|
||||||
DATA_DIR,
|
DATA_DIR,
|
||||||
CACHE_DIR: path.join(DATA_DIR, 'cache'),
|
CACHE_DIR: path.join(DATA_DIR, 'cache'),
|
||||||
CACHE_LIMIT_IN_BYTES: xbytes.parseSize(process.env.CACHE_LIMIT ?? '2GB'),
|
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 YOUTUBE_API_KEY!: string;
|
||||||
readonly SPOTIFY_CLIENT_ID!: string;
|
readonly SPOTIFY_CLIENT_ID!: string;
|
||||||
readonly SPOTIFY_CLIENT_SECRET!: string;
|
readonly SPOTIFY_CLIENT_SECRET!: string;
|
||||||
readonly NODE_ENV!: string;
|
readonly IS_PRODUCTION!: boolean;
|
||||||
readonly DATA_DIR!: string;
|
readonly DATA_DIR!: string;
|
||||||
readonly CACHE_DIR!: string;
|
readonly CACHE_DIR!: string;
|
||||||
readonly CACHE_LIMIT_IN_BYTES!: number;
|
readonly CACHE_LIMIT_IN_BYTES!: number;
|
||||||
|
|
Loading…
Reference in a new issue