Electric2024

deploy-commands

Jul 1st, 2024
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { REST, Routes } = require('discord.js');
  2. const fs = require('node:fs');
  3. const path = require('node:path');
  4. const ping = require('./src/commands/utility/ping');
  5. require('dotenv').config();
  6.  
  7. const commands = [ping];
  8. const foldersPath = path.join(__dirname, 'commands');
  9. const commandFolders = fs.readdirSync(foldersPath);
  10.  
  11. for (const folder of commandFolders) {
  12.     const commandsPath = path.join(foldersPath, folder);
  13.     const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
  14.     for (const file of commandFiles) {
  15.         const filePath = path.join(commandsPath, file);
  16.         const command = require(filePath);
  17.         if ('data' in command && 'execute' in command) {
  18.             commands.push(command.data.toJSON());
  19.         } else {
  20.             console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
  21.         }
  22.     }
  23. }
  24.  
  25. const clientId = process.env.CLIENT_ID;
  26. const guildId = process.env.GUILD_ID;
  27. const token = process.env.TOKEN;
  28.  
  29. const rest = new REST().setToken(token);
  30.  
  31. (async () => {
  32.     try {
  33.         console.log(`Started refreshing ${commands.length} application (/) commands.`);
  34.  
  35.         const data = await rest.put(
  36.             Routes.applicationGuildCommands(clientId, guildId),
  37.             { body: commands },
  38.         );
  39.  
  40.         console.log(`Successfully reloaded ${data.length} application (/) commands.`);
  41.     } catch (error) {
  42.         console.error(error);
  43.         console.log(commands)
  44.     }
  45. })();
Advertisement
Add Comment
Please, Sign In to add comment