#Unable to register slash commands with API v10

30 messages · Page 1 of 1 (latest)

normal ore
#

Hello,

I hear you to automatically register slash commands on** all servers** where the bot is present, but I can't do it. I have no errors, nothing happens.

Here is what I do in my command file:

exports.help = new SlashCommandBuilder()
.setName("link")
.setDescription("Link your MSPRetro account with your Discord account")
.addStringOption(option =>
    option.setName("username")
        .setDescription("Your MSPRetro username")
        .setRequired(true)
);

Here is what I do in my main file:

readdir("./Commands/", async(error, files) => {
    const commands = files.filter(f => f.split(".").pop() === "js");
  
    let commandsArray = [ ];
    commands.forEach(commandFile => {
      let command = require(`./Commands/${commandFile}`);
      console.log(`Loaded ${commandFile}!`);
      client.commands.set(command.help.name, command);
  
      commandsArray.push(command);
    });
  
    const finalArray = commandsArray.map((e) => e.help.toJSON());
    await slash(bot.id, false, finalArray);
});

Here is my function to register the slash commands:

const { bot } = require("../config.json");
const { REST, Routes } = require("discord.js");

let cmdDatas;

exports.slash = async(clientId, guildId = false, commands = false) => {  
  const rest = new REST({ version: "10" }).setToken(bot.token);

  try {
    if (guildId) {
      await rest.put(Routes.applicationGuildCommands(clientId, guildId), {
        body: cmdDatas,
      })
      .then(() => {
        return true;
      });
    } else {
      console.log("Started refreshing application (/) commands.");
      cmdDatas = commands;

      await rest.put(Routes.applicationCommands(bot.id), { body: cmdDatas })
      .then(() => {
        console.log("Successfully reloaded application (/) commands.");
        return true;
      });
    }
  } catch (error) {
    return false;
  }
};

Can you please help me?

lone topazBOT
#

• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.

normal ore
#

The version of discord.js is 14.3.0.

coral island
#

Any error?

normal ore
coral island
#

Are you registering global commands or guild commands?

normal ore
#

global commands

#

as you can see here

coral island
#

Try registering guild commands

#

To see if the error persists

normal ore
#

Yes, no error

#

I just tried

#

oh wait

#

it's undefined

#

my bad

coral island
#

I didn't see that either FeelsBadMan

#

You aren't console logging the error in the catch

#

So you won't see the error anyway

#

Also, thre returns inside the .then wont return the slash function, they will only return the .then

normal ore
#

oh okay

normal ore
#

I have tried globally, and the result looks good

#
  // try {
    if (guildId) {
      await rest.put(Routes.applicationGuildCommands(bot.id, guildId), {
        body: commands,
      })
      .then((a) => {
        console.log(a);
        return true;
      });
    } else {
      console.log("Started refreshing application (/) commands.");

      await rest.put(Routes.applicationCommands(bot.id), { body: commands })
      .then((a) => {
        console.log(a);
        console.log("Successfully reloaded application (/) commands.");
        return true;
      });
    }
  // } catch (error) {
    return false;
  // }
#

But I don't see the slash command on the Discord server

#

It's maybe the Discord cache

#

Problem solved!

#

Have a nice day, and thanks for the help!