Nicky
Nicky

Reputation: 291

Trouble making slash commands global

Since my bots new update is releasing soon I have been trying to figure out how to make the slash commands go global. I keep hitting bumps and I have no idea what to do next. Below is some of my code of creating the commands.

ready.js


const {prefix} = require("../../botconfig.json")
const {createCmd} = require("../../dataHandler.js")
module.exports = bot => {
  
    console.log(`${bot.user.username} is online`);

    setInterval(() => bot.user.setActivity(`!help | Switch Support`, { type: "WATCHING" }), 15000)
    createCmd(bot, '781631298749726730') //second param is guildId
};

DataHandler(Creates the data for the comamnds)

async function createCmd(Client, guildId) {
  const data = [
    //ping cmd
    {
      name: 'test',
      description: 'test slash command'
    }
  ]

  await Client.guilds.cache.get(guildId).commands.set(data)
}

module.exports = {createCmd}

How will I use these to make the commands global so every server can use these commands when the update is released.

Upvotes: 1

Views: 1127

Answers (1)

Compositr
Compositr

Reputation: 737

See the documentation here. All the code below is taken from there.

In your createCmd function add

const rest = new REST({ version: '9' }).setToken("your discord token");

try {
 console.log('Started refreshing application (/) commands.');

 await rest.put(
    Routes.applicationCommands("REPLACE_WITH_CLIENT_ID"),
    { body: data },
 );

        console.log('Successfully reloaded application (/) commands.');
    } catch (error) {
        console.error(error);
    }

Require these at the beginning of your file

const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');

Just a note: Global commands take up to an hour to refresh across Discord. Keep that in mind when updating.

Upvotes: 1

Related Questions