0

so i was trying to make a discord bot and it is giving me this error:

DiscordAPIError[50035]: Invalid Form Body 0.name[BASE_TYPE_REQUIRED]: This field is required at SequentialHandler.runRequest (C:\Users\Admin\Desktop\bot\node_modules@discordjs\rest\dist\lib\handlers\SequentialHandler.cjs:287:15) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async SequentialHandler.queueRequest (C:\Users\Admin\Desktop\bot\node_modules@discordjs\rest\dist\lib\handlers\SequentialHandler.cjs:99:14) at async REST.request (C:\Users\Admin\Desktop\bot\node_modules@discordjs\rest\dist\lib\REST.cjs:52:22) at async Client.client.handleCommands (C:\Users\Admin\Desktop\bot\src\functions\handlers\handleCommands.js:28:9) { rawError: { code: 50035, errors: { '0': [Object] }, message: 'Invalid Form Body' }, code: 50035, status: 400, method: 'PUT', url: 'https://discord.com/api/v9/applications/1009341661572243516/guilds/688019305299706004/commands', requestBody: { files: undefined, json: [ [Object], [Object] ] } }

the code is as follows:

const { Routes } = require('discord-api-types/v9');
const fs = require('fs');

module.exports = (client) => {
  client.handleCommands = async () => {
    const commandFolders = fs.readdirSync('./src/commands');
    for (const folder of commandFolders) {
      const commandFiles = fs
        .readdirSync(`./src/commands/${folder}`)
        .filter((file) => file.endsWith('.js'));

      const { commands, commandArray } = client;
      for (const file of commandFiles) {
        const command = require(`../../commands/${folder}/${file}`);
        client.commands.set(command.data.name, command);
        commandArray.push(command, command.data.toJSON());
      }
    }

    const clientId = '1009341661572243516';
    const guildId = '688019305299706004';
    const rest = new REST({ version: '9' }).setToken(process.env.token);
    try {
      console.log('Started refreshing application (/) commands.');

      await rest.put(Routes.applicationGuildCommands(clientId, guildId), {
        body: client.commandArray,
      });
      console.log('Successfully reloaded application(/) commands.');
    } catch (error) {
      console.error(error);
    }
  };
};

command file:

 
module.exports = {
  data: new SlashCommandBuilder()
    .setName("ping")
    .setDescription("Returns a ping"),
  async execute(interaction, client) {
    const message = await interaction.deferReply({
      fetchReply: true,
    });
 
    const newMessage = `API Latency: ${client.ws.ping}\n Client Ping: ${
      message.createdTimestamp - interaction.createdTimestamp
    }`;
 
    await interaction.reply({
      content: newMessage,
    });
  },
};
2
  • How many command files do you have? There is an error with data property in one of your exported objects. Can you post them if it's not too many? Commented Aug 20, 2022 at 6:56
  • till now i just have one command file :
    – Harshita
    Commented Aug 20, 2022 at 7:28

1 Answer 1

0

The error is from your commandArray, you are pushing 2 elements but as you can see in the documentation (https://discordjs.guide/interactions/slash-commands.html#guild-commands ), we just need to pass command.data.toJSON() and not command, command.data.toJSON(). I had the same issue and it worked for me.

Here is the right formated code

const { Routes } = require('discord-api-types/v9');
const fs = require('fs');

module.exports = (client) => {
  client.handleCommands = async () => {
    const commandFolders = fs.readdirSync('./src/commands');
    for (const folder of commandFolders) {
      const commandFiles = fs
        .readdirSync(`./src/commands/${folder}`)
        .filter((file) => file.endsWith('.js'));

      const { commands, commandArray } = client;
      for (const file of commandFiles) {
        const command = require(`../../commands/${folder}/${file}`);
        client.commands.set(command.data.name, command);
        commandArray.push(command.data.toJSON());
      }
    }

    const clientId = '1009341661572243516';
    const guildId = '688019305299706004';
    const rest = new REST({ version: '9' }).setToken(process.env.token);
    try {
      console.log('Started refreshing application (/) commands.');

      await rest.put(Routes.applicationGuildCommands(clientId, guildId), {
        body: client.commandArray,
      });
      console.log('Successfully reloaded application(/) commands.');
    } catch (error) {
      console.error(error);
    }
  };
};

Not the answer you're looking for? Browse other questions tagged or ask your own question.