Search code examples
discord.js

UNHANDLED_EXCEPTION: DiscordAPIError[50035]: Invalid Form Body 3.type[ENUM_TYPE_COERCE]: Value "6" is not a valid enum value


I'm developping a Discord BOT on repplit and when i launch it with CTR+Enter (or with the run button), i'm getting the following error :

UNHANDLED_EXCEPTION: DiscordAPIError[50035]: Invalid Form Body
3.type[ENUM_TYPE_COERCE]: Value "6" is not a valid enum value.
Promise {
  <rejected> DiscordAPIError[50035]: Invalid Form Body
  3.type[ENUM_TYPE_COERCE]: Value "6" is not a valid enum value.
      at SequentialHandler.runRequest (/home/runner/AIUC/node_modules/discord.js/node_modules/@discordjs/rest/dist/index.js:667:15)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async SequentialHandler.queueRequest (/home/runner/AIUC/node_modules/discord.js/node_modules/@discordjs/rest/dist/index.js:464:14)
      at async REST.request (/home/runner/AIUC/node_modules/discord.js/node_modules/@discordjs/rest/dist/index.js:910:22) {
    requestBody: { files: undefined, json: [Array] },
    rawError: { code: 50035, errors: [Object], message: 'Invalid Form Body' },
    code: 50035,
    status: 400,
    method: 'PUT',
    url: 'https://discord.com/api/v10/applications/1069961347166646272/guilds/1069355488258560122/commands'
  }
}

I don't know where my error could come from. You can see bellow my package.json

{
  "name": "nodejs",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@discordjs/opus": "^0.8.0",
    "@discordjs/rest": "^1.5.1-dev.1676247033-0e4224b.0",
    "@discordjs/voice": "^0.14.0",
    "@distube/soundcloud": "^1.3.0",
    "@distube/spotify": "^1.5.1",
    "@distube/yt-dlp": "^1.1.3",
    "chalk": "4.1.2",
    "dayjs": "1.10.7",
    "discord.js": "^14.7.1",
    "distube": "^4.0.4",
    "express": "^4.18.2",
    "ffmpeg-static": "4.4.0",
    "glob": "7.2.0",
    "libsodium-wrappers": "^0.7.10",
    "lyrics-finder": "^21.7.0",
    "moment": "^2.29.4",
    "moment-duration-format": "^2.3.2",
    "mongoose": "^6.9.1",
    "ms": "2.1.3",
    "node-fetch": "^3.2.6",
    "opusscript": "^0.0.8",
    "pm2": "^5.2.2",
    "systeminformation": "^5.17.9",
    "ytdl-core": "^4.11.2"
  },
  "description": ""
}

This error appear when i'm update my Discord BOT from DiscordJS v13 to DiscordJS v14. I've tried to add a register.js file to initialize my REST API but the problem still...

EDIT

index.js

const { Client, Collection, GatewayIntentBits } = require('discord.js');
const { DisTube } = require('distube');
const { SpotifyPlugin } = require('@distube/spotify');
const { SoundCloudPlugin } = require('@distube/soundcloud');
const { YtDlpPlugin } = require('@distube/yt-dlp');
const client = new Client(
  {
    intents: [
      GatewayIntentBits.Guilds,
      GatewayIntentBits.GuildMembers,
      GatewayIntentBits.GuildMessages,
      GatewayIntentBits.GuildVoiceStates,
      GatewayIntentBits.MessageContent
    ]
  }
);
const mongoose = require('mongoose');
const Logger = require('./utils/Logger');

['commands', 'buttons', 'selects'].forEach(x => client[x] = new Collection());
['CommandUtil', 'EventUtil', 'ButtonUtil', 'SelectUtil'].forEach(handler => { require(`./utils/handlers/${handler}`)(client) });
require('./utils/Functions')(client);

mongoose.set('strictQuery', true);

process.on('exit', code => { Logger.client(`Process shutdown with the following code: ${code}`) });
process.on('uncaughtException', (err, origin) => {
  Logger.error(`UNCAUGHT_EXCEPTION: ${err}`);
  console.error(`Origin: ${origin}`);
});
process.on('unhandledRejection', (reason, promise) => {
  Logger.warn(`UNHANDLED_EXCEPTION: ${reason}`);
  console.log(promise);
});
process.on('warning', (...args) => Logger.warn(...args));

mongoose.connect(process.env.DATABASE_URI, {
  autoIndex: false,
  maxPoolSize: 10,
  serverSelectionTimeoutMS: 5000,
  socketTimeoutMS: 45000,
  family: 4
}).then(() => { Logger.client('Connected to the database!') })
  .catch(err => { Logger.error(err) });

client.distube = new DisTube(client, {
  leaveOnStop: false,
  emitNewSongOnly: true,
  emitAddSongWhenCreatingQueue: false,
  emitAddListWhenCreatingQueue: false,
  plugins: [
    new SpotifyPlugin({
      emitEventsAfterFetching: true
    }),
    new SoundCloudPlugin(),
    new YtDlpPlugin()
  ]
});

client.login(process.env.DISCORD_TOKEN);

resident-evil.js

const { EmbedBuilder, ApplicationCommandOptionType } = require('discord.js');
const { ResidentEvilRPGCharacter } = require('../../models');

module.exports = {
  name: 'resident-evil',
  category: 'context',
  permissions: ['SEND_MESSAGES'],
  ownerOnly: false,
  usage: 'Use context menu!',
  examples: ['Use context menu!'],
  type: ApplicationCommandOptionType.User,
  async runInteraction(client, interaction) {
    const member = await interaction.guild.members.fetch(interaction.targetId);
    const residentEvilData = await ResidentEvilRPGCharacter.findOne({ id: interaction.guild.id, member: member.id });

    if (!residentEvilData) return interaction.reply({ content: 'You have no character in Resident Evil RPG' })

    const embed = new EmbedBuilder()
      .setAuthor({ name: `${member.user.tag} (${member.id})` })
      .setColor('#0047AB')
      .setImage(`${residentEvilData['picture']}`)
      .setThumbnail(member.user.displayAvatarURL())
      .setDescription(`**+ Lastname**\n${residentEvilData['lastname']}\n\n**+ Firstname**\n${residentEvilData['firstname']}\n\n**+ Surname**\n${residentEvilData['surname']}\n\n**+ Age**\n${residentEvilData['age']}\n\n**+ Sexe**\n${residentEvilData['sexe']}\n\n**+ Family**\n${residentEvilData['family']}\n\n**+ Nationality**\n${residentEvilData['nationality']}\n\n**+ Sexual Orientation**\n${residentEvilData['sexualorientation']}\n\n**+ Political Party**\n${residentEvilData['politicalparty']}\n\n**+ Religion**\n${residentEvilData['religion']}\n\n**+ History**\n${residentEvilData['history']}\n\n${residentEvilData['other']}`);

    interaction.reply({ embeds: [embed] });
  }
}

I finally find where this error come from, and the error come from my context command resident-evil.js


Solution

  • Instead of type: ApplicationCommandOptionType.User, we need to replace this value by 2! Why ??? I don't know, because the DiscordJS Documentation tell us to use ApplicationCommandOptionType.User! My only conclusion of that is with Discord JS v14 context menu are split into 2 different type :

    1. UserContextMenu
    2. MessageContextMenu So maybe DiscordJS considered these context menu command as SubCommandGroup. So the correct file is the following files:

    resident-evil.js

    const { EmbedBuilder, ApplicationCommandOptionType } = require('discord.js');
    const { ResidentEvilRPGCharacter } = require('../../models');
    
    module.exports = {
      name: 'resident-evil',
      category: 'context',
      permissions: ['SEND_MESSAGES'],
      ownerOnly: false,
      usage: 'Use context menu!',
      examples: ['Use context menu!'],
      type: 2,
      async runInteraction(client, interaction) {
        const member = await interaction.guild.members.fetch(interaction.targetId);
        const residentEvilData = await ResidentEvilRPGCharacter.findOne({ id: interaction.guild.id, member: member.id });
    
        if (!residentEvilData) return interaction.reply({ content: 'You have no character in Resident Evil RPG' })
    
        const embed = new EmbedBuilder()
          .setAuthor({ name: `${member.user.tag} (${member.id})` })
          .setColor('#0047AB')
          .setImage(`${residentEvilData['picture']}`)
          .setThumbnail(member.user.displayAvatarURL())
          .setDescription(`**+ Lastname**\n${residentEvilData['lastname']}\n\n**+ Firstname**\n${residentEvilData['firstname']}\n\n**+ Surname**\n${residentEvilData['surname']}\n\n**+ Age**\n${residentEvilData['age']}\n\n**+ Sexe**\n${residentEvilData['sexe']}\n\n**+ Family**\n${residentEvilData['family']}\n\n**+ Nationality**\n${residentEvilData['nationality']}\n\n**+ Sexual Orientation**\n${residentEvilData['sexualorientation']}\n\n**+ Political Party**\n${residentEvilData['politicalparty']}\n\n**+ Religion**\n${residentEvilData['religion']}\n\n**+ History**\n${residentEvilData['history']}\n\n${residentEvilData['other']}`);
    
        interaction.reply({ embeds: [embed] });
      }
    }