How to create Voice Channels in Discord.JS V14

2/2/2023, 11:47:17 PM

This is the second (of four) parts of the Discord.JS V14 tutorial that I've published. You can read the here. You can also read the full version of this tutorial on Medium if you prefer.

Creating a Voice Channel isn't that much different than creating a Text Channel. Voice Channels won't get their names normalized to lowercase and have their spaces replaced by dashes, so whatever you write will be what will be used. Because of how similar Voice and Text Channels are, we can reuse most of the code we used for the Text Channel and make small adjustments.

// Importing SlashCommandBuilder is required for every slash command // We import PermissionFlagsBits so we can restrict this command usage // We also import ChannelType to define what kind of Channel we are creating const { SlashCommandBuilder, PermissionFlagsBits, ChannelType } = require( "discord.js", ); module.exports = { data: new SlashCommandBuilder() .setName("createvoicechannel") // Command name matching file name .setDescription("Creates a new voice channel") // Voice Channel name .addStringOption((option) => option .setName("voicechannelname") // option names need to always be lowercase and have no spaces .setDescription("Choose the name to give to the voice channel") .setMinLength(1) // A Voice Channel needs to be named .setMaxLength(25) // Discord will cut-off names past the 25 characters, // so that's a good hard limit to set. You can manually increase this if you wish .setRequired(true) ) // You will usually only want users that can create new Channels to // be able to use this command and this is what this line does. // Feel free to remove it if you want to allow any users to // create new Channels .setDefaultMemberPermissions(PermissionFlagsBits.ManageChannels) // It's impossible to create Voice Channels inside DMs, so // it's in your best interest in disabling this command through DMs .setDMPermission(false), async execute(interaction) { // Before executing any other code, we need to acknowledge the interaction. // Discord only gives us 3 seconds to acknowledge an interaction before // the interaction gets voided and can't be used anymore. await interaction.reply({ content: "Fetched all input and working on your request!", }); // After acknowledging the interaction, we retrieve the string sent by the user const chosenVoiceChannelName = interaction.options.getString( "voicechannelname", ); // Do note that the string passed to the method .getString() needs to // match EXACTLY the name of the option provided (line 12 in this file). // If it's not a perfect match, this will always return null try { // Check if this Channel where the command was used is stray if (!interaction.channel.parent) { // If the Channel where the command was used is stray, // create another stray Voice Channel in the server. await interaction.guild.channels.create({ name: chosenVoiceChannelName, // The name given to the Channel by the user type: ChannelType.GuildVoice, // The type of the Channel created. }); // Notice how we are creating a Channel in the list of Channels // of the server. This will cause the Channel to spawn at the top // of the Channels list, without belonging to any Categories // If we managed to create the Channel, edit the initial response with // a success message await interaction.editReply({ content: "Your voice channel was successfully created!", }); return; } // Check if this Channel where the command was used belongs to a Category if (interaction.channel.parent) { // If the Channel where the command belongs to a Category, // create another Channel in the same Category. await interaction.channel.parent.children.create({ name: chosenVoiceChannelName, // The name given to the Channel by the user type: ChannelType.GuildVoice, // The type of the Channel created. }); // If we managed to create the Channel, edit the initial response with // a success message await interaction.editReply({ content: "Your voice channel was successfully created in the same category!", }); return; } } catch (error) { // If an error occurred and we were not able to create the Channel // the bot is most likely received the "Missing Permissions" error. // Log the error to the console console.log(error); // Also inform the user that an error occurred and give them feedback // about how to avoid this error if they want to try again await interaction.editReply({ content: "Your voice channel could not be created! Please check if the bot has the necessary permissions!", }); } }, };
View/Download file

A few tweaks were made, variables were renamed, comments were updated, and error messages were updated, but the process isn't much different from creating a Text Channel. One of the few features that Voice Channels have over Text Channels is the ability to limit the number of users it can hold at once. Let's take a look at how to do that now.

Create a Voice Channel that has a maximum number of concurrent users

One of the exclusive features that Voice Channels have over other Channels is the ability to limit the number of users that can use it at the same time. To set this number, all you gotta do is pass in an integer for the userLimit key when creating a Voice Channel.

// ... // initial code unchanged // ... // Voice Channel limit of participants .addIntegerOption( (option) => option .setName('voiceuserlimit') // option names need to always be lowercase and have no spaces .setDescription( 'Select the maximum number of concurrent users for the voice channel' ) .setMinValue(2) // A voice Channel with less than 2 users will be useless // for nearly every case, so we will disable users from creating voice // Channels that can take less than that .setRequired(false) ) // ... // code in between unchanged // ... // After acknowledging the interaction, we retrieve the input sent by the user const chosenVoiceChannelName = interaction.options.getString('voicechannelname'); const voiceChannelUserLimit = interaction.options.getInteger('voiceuserlimit') ?? undefined; // ... // code in between unchanged // ... name: chosenVoiceChannelName, // The name given to the Channel by the user type: ChannelType.GuildVoice, // The type of the Channel created. userLimit: voiceChannelUserLimit, // The max number of concurrent users // ... // rest of the code unchanged // ...
View/Download file

There are a few key points that need to be talked about. The first of them is that we are not requiring the limit to be set (line 30). This allows the user to set a limit if they want but also allows the Voice Channel to be unlimited if they don't.

const voiceChannelUserLimit = interaction.options.getInteger('voiceuserlimit') ?? undefined;

To avoid this causing an error when creating the Voice Channel, we check (line 49) if a value was passed and, if .getInteger() returns us null, then then the Javascript Nullish Coalescence operator ?? will set the value to undefined.

userLimit: voiceChannelUserLimit

Passing undefined to the userLimit key when creating a Voice Channel will make it unlimited, while any integer would be used as the actual limit.

Next post: How to create Categories in Discord.JS V14

Written with 💞 by TheYuriG