RegisterSlashCommands.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const fs = require("fs");
  2. const path = require("path");
  3. /**
  4. * Register slash commands for a guild
  5. * @param {require("../structures/DiscordMusicBot")} client
  6. * @param {string} guild
  7. */
  8. module.exports = (client, guild) => {
  9. client.log("Registering slash commands for " + guild);
  10. let commandsDir = path.join(__dirname, "..", "commands");
  11. fs.readdir(commandsDir, (err, files) => {
  12. if (err) throw err;
  13. files.forEach(async (file) => {
  14. let cmd = require(commandsDir + "/" + file);
  15. if (!cmd.SlashCommand || !cmd.SlashCommand.run) return;
  16. let dataStuff = {
  17. name: cmd.name,
  18. description: cmd.description,
  19. options: cmd.SlashCommand.options,
  20. };
  21. //Creating variables like this, So you might understand my code :)
  22. let ClientAPI = client.api.applications(client.user.id);
  23. let GuildAPI = ClientAPI.guilds(guild);
  24. client.log(
  25. "[Slash Command]: [POST] Guild " +
  26. guild +
  27. ", Command: " +
  28. dataStuff.name
  29. );
  30. try {
  31. await GuildAPI.commands.post({ data: dataStuff });
  32. } catch (e) {
  33. client.log(
  34. "[Slash Command]: [POST-FAILED] Guild " +
  35. guild +
  36. ", Command: " +
  37. dataStuff.name
  38. );
  39. console.log(e);
  40. }
  41. });
  42. });
  43. };