Refactor bot commands update system

This commit is contained in:
Federico fuji97 Rapetti 2022-01-10 20:13:50 +01:00
parent ac1fef836f
commit ab164353c1
3 changed files with 34 additions and 11 deletions

View file

@ -18,17 +18,22 @@ 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 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.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
console.log(`Starting environment: ${this.env}\n`);
// Load in commands // Load in commands
container.getAll<Command>(TYPES.Command).forEach(command => { container.getAll<Command>(TYPES.Command).forEach(command => {
// TODO: remove ! // TODO: remove !
@ -117,12 +122,23 @@ 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);
this.client.guilds.cache.each(async guild => { switch (this.env) {
await rest.put( case 'production':
Routes.applicationGuildCommands(this.client.user!.id, guild.id), // If production, set commands bot-wide
{body: this.commandsByName.map(command => command.slashCommand ? command.slashCommand.toJSON() : null)}, 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`); spinner.succeed(`Ready! Invite the bot with https://discordapp.com/oauth2/authorize?client_id=${this.client.user?.id ?? ''}&scope=applications.commands%20bot&permissions=2184236096`);
}); });

View file

@ -7,18 +7,23 @@ import {chunk} from '../utils/arrays.js';
import container from '../inversify.config.js'; import container from '../inversify.config.js';
import Command from '../commands'; import Command from '../commands';
import {TYPES} from '../types.js'; import {TYPES} from '../types.js';
import Config from '../services/config.js';
const DEFAULT_PREFIX = '!'; const DEFAULT_PREFIX = '!';
export default async (guild: Guild): Promise<void> => { export default async (guild: Guild): Promise<void> => {
await Settings.upsert({guildId: guild.id, prefix: DEFAULT_PREFIX}); await Settings.upsert({guildId: guild.id, prefix: DEFAULT_PREFIX});
// Setup slash commands const config = container.get<Config>(TYPES.Config);
const commands: ApplicationCommandData[] = container.getAll<Command>(TYPES.Command)
.filter(command => command.slashCommand?.name)
.map(command => command.slashCommand as ApplicationCommandData);
await guild.commands.set(commands); // Setup slash commands
if (config.NODE_ENV === 'production') {
const commands: ApplicationCommandData[] = container.getAll<Command>(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); const owner = await guild.client.users.fetch(guild.ownerId);

View file

@ -12,6 +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',
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'),
@ -23,6 +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 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;