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 {
private readonly client: Client;
private readonly token: string;
private readonly env: string;
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.commandsByName = new Collection();
this.commandsByButtonId = new Collection();
}
public async listen(): Promise<void> {
// Log environment
console.log(`Starting environment: ${this.env}\n`);
// Load in commands
container.getAll<Command>(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`);
});

View file

@ -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<void> => {
await Settings.upsert({guildId: guild.id, prefix: DEFAULT_PREFIX});
// Setup slash commands
const commands: ApplicationCommandData[] = container.getAll<Command>(TYPES.Command)
.filter(command => command.slashCommand?.name)
.map(command => command.slashCommand as ApplicationCommandData);
const config = container.get<Config>(TYPES.Config);
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);

View file

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