const commands = [];
// Grab all the command files from the commands directory you created earlier
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
//console.log("Iterating:");
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) {
const command = require(./commands/${file});
//I changed this part since it was creating an error. original code: commands.push(command.data.toJSON());
commands.push(JSON.stringify(command.data));
console.log(`Iterating: ${file}`);
}
// Construct and prepare an instance of the REST module
const rest = new REST({ version: '10' }).setToken(token);
// and deploy your commands!
(async () => {
try {
console.log(Started refreshing ${commands.length} application (/) commands.);
// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(
Routes.applicationCommands(clientId),
{ body: commands},
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
Part of the guide or code sample the question is about
https://discordjs.guide/creating-your-bot/command-deployment.html#guild-commands
Question
**I am attempting to register my slash commands using the deploy-commands.js code presented here: https://discordjs.guide/creating-your-bot/command-deployment.html#guild-commands This is the specific code I am running. There is on modification to correct another apparent bug.
To simplify de-bugging I limited by commands to the ping command.
The contents of my files and the error message are below:**
deploy-commands.js
const { REST, Routes } = require('discord.js'); const { clientId, guildId, token } = require('./config.json'); const fs = require('node:fs');
const commands = []; // Grab all the command files from the commands directory you created earlier const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
//console.log("Iterating:");
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment for (const file of commandFiles) { const command = require(
./commands/${file}
);}
// Construct and prepare an instance of the REST module const rest = new REST({ version: '10' }).setToken(token);
// and deploy your commands! (async () => { try { console.log(
Started refreshing ${commands.length} application (/) commands.
);})();
ping.js
const { SlashCommandBuilder } = require('discord.js');
module.exports = { data: new SlashCommandBuilder() .setName('ping') .setDescription('Replies with Pong!'), async execute(interaction) { await interaction.reply('Pong!'); }, };
error message
PS F:\sentimentBot> node deploy-commands.js Started refreshing 1 application (/) commands. DiscordAPIError[50035]: Invalid Form Body 0[DICT_TYPE_CONVERT]: Only dictionaries may be used in a DictType at SequentialHandler.runRequest (F:\sentimentBot\node_modules\@discordjs\rest\dist\index.js:667:15) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async SequentialHandler.queueRequest (F:\sentimentBot\node_modules\@discordjs\rest\dist\index.js:464:14) at async REST.request (F:\sentimentBot\node_modules\@discordjs\rest\dist\index.js:910:22) at async F:\sentimentBot\deploy-commands.js:29:16 { requestBody: { files: undefined, json: [ '{"options":[],"name":"ping","description":"Replies with Pong!"}' ] }, rawError: { code: 50035, errors: { '0': [Object] }, message: 'Invalid Form Body' }, code: 50035, }, rawError: { code: 50035, errors: { '0': [Object] }, message: 'Invalid Form Body' }, code: 50035, status: 400, method: 'PUT', url: 'https://discord.com/api/v10/applications/106928450743959571/commands' }