Search code examples
javascriptnode.jsdiscorddiscord.jsbots

Discord.js v14 Creating a voice channel throws error: Invalid Form Body name[BASE_TYPE_REQUIRED]: This field is required


I'm writing some code for creating temporary voice channels.

Every time a user joins a specific voice channel, the bot should create a new one where that user will be moved and so on up to "n". When all users leave the channel, that temporary channel is deleted. While debugging I'm getting the following error:

node:events:491
      throw er; // Unhandled 'error' event
      ^

DiscordAPIError[50035]: Invalid Form Body
name[BASE_TYPE_REQUIRED]: This field is required
    at SequentialHandler.runRequest (/home/enigma/opt/progetti/ITH-BOT-master/node_modules/@discordjs/rest/dist/index.js:667:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async SequentialHandler.queueRequest (/home/enigma/opt/progetti/ITH-BOT-master/node_modules/@discordjs/rest/dist/index.js:464:14)
    at async REST.request (/home/enigma/opt/progetti/ITH-BOT-master/node_modules/@discordjs/rest/dist/index.js:910:22)
    at async GuildChannelManager.create (/home/enigma/opt/progetti/ITH-BOT-master/node_modules/discord.js/src/managers/GuildChannelManager.js:169:18)
    at async Client.<anonymous> (/home/enigma/opt/progetti/ITH-BOT-master/index.js:214:23)
Emitted 'error' event on Client instance at:
    at emitUnhandledRejectionOrErr (node:events:394:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:84:21) {
  requestBody: {
    files: undefined,
    json: {
      name: undefined,
      topic: undefined,
      type: undefined,
      nsfw: undefined,
      bitrate: undefined,
      user_limit: undefined,
      parent_id: undefined,
      position: undefined,
      permission_overwrites: undefined,
      rate_limit_per_user: undefined,
      rtc_region: undefined,
      video_quality_mode: undefined,
      available_tags: undefined,
      default_reaction_emoji: undefined,
      default_auto_archive_duration: undefined,
      default_sort_order: undefined
    }
  },
  rawError: {
    code: 50035,
    errors: {
      name: {
        _errors: [
          {
            code: 'BASE_TYPE_REQUIRED',
            message: 'This field is required'
          }
        ]
      }
    },
    message: 'Invalid Form Body'
  },
  code: 50035,
  status: 400,
  method: 'POST',
  url: 'https://discord.com/api/v10/guilds/729001324976275516/channels'
}

Node.js v18.14.2

this is my code:

client.on('voiceStateUpdate', async (oldState, newState) => {
  const channelID = '802270488125636609'; // ID del canale da monitorare

  // Verifica se l'utente si sta unendo al canale specificato
  if (newState.channel && newState.channel.id === channelID) {
    const guild = newState.guild;
    const user = newState.member.user;

    // Crea un nuovo canale vocale
   const newChannel = await guild.channels.create(`${user.username}'s Channel`, {
  type: 'voice',
  parent: newState.channel.parent,
  userLimit: 4
});


    // Sposta l'utente nel nuovo canale
    newState.setChannel(newChannel);

    console.log(`Il canale vocale ${newChannel.name} è stato creato per ${user.username}.`);
  }

  // Verifica se l'utente sta lasciando il canale creato per lui
  if (oldState.channel && oldState.channel.name.includes(`${oldState.member.user.username}'s Channel`) && oldState.channel.members.size === 0) {
    oldState.channel.delete();
    console.log(`Il canale vocale ${oldState.channel.name} è stato eliminato.`);
  }
});

I tried to search the net for some solution but I had no luck, please, can you help me?


Solution

  • The error says that the name field is required. While previously the first parameter was the created channel's name, in the latest version of discord.js, GuildChannelManager#create() accepts a single parameter, an options object, that includes the name of the new channel too.

    Another error is that the type option must be a ChannelType enum; in your case, it's ChannelType.GuildVoice. (You will need to import ChannelType from discord.js.) The following should work as expected:

    const { ChannelType } = require('discord.js');
    
    const newChannel = await guild.channels.create({
      name: `${user.username}'s Channel`,
      type: ChannelType.GuildVoice,
      parent: newState.channel.parent,
      userLimit: 4,
    });