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?
