/ syncCommands.ts
syncCommands.ts
 1  import { REST, Routes, RESTPostAPIApplicationCommandsJSONBody } from 'discord.js';
 2  import fs from 'fs';
 3  import path from 'path';
 4  
 5  import { Command } from './src/modules/commandHandler';
 6  import config from './src/modules/config';
 7  
 8  
 9  const commandsDir = './src/modules/commands/';
10  const commands: RESTPostAPIApplicationCommandsJSONBody[] = [];
11  const commandFiles = fs.readdirSync(path.resolve(commandsDir)).filter(file => file.endsWith('.ts'));
12  for (const file of commandFiles) {
13    const command: Command = require(path.resolve(commandsDir, file)).default;
14    commands.push(command.data.toJSON());
15  }
16  
17  
18  const rest = new REST().setToken(config.TOKEN);
19  
20  (async () => {
21    console.log(`Started refreshing ${commands.length} application (/) commands.`);
22    let data: any;
23    if (config.GUILD) {
24      data = await rest.put(Routes.applicationGuildCommands(config.APPLICATION_ID, config.GUILD), {
25        body: commands
26      });
27    } else {
28      data = await rest.put(Routes.applicationCommands(config.APPLICATION_ID), {
29        body: commands
30      });
31    }
32  
33    console.log(`Successfully reloaded ${data.length} application (/) commands.`);
34  })();