Search code examples
javascriptdiscord.js

Error creating a temporary channel using a bot


I’m trying to make a script in which a user joins a certain channel, the bot creates a temporary channel and transfers the user there, and after the channel is empty, the bot deletes it, but it gives an error that the name value is not specified for the created channel.
I use discord.js v14

robot.on('voiceStateUpdate', async (oldState, newState) => {
    const voiceChannelID = '1153371417182752940';

    if (newState.channelId === voiceChannelID) {
        // Create a new voice channel
        const channel = await newState.guild.channels.create(`Channel ${newState.member.user.username}`, {
            type: 'GUILD_VOICE',
            parent: newState.channel.parent,
            name: `Channel ${newState.member.user.username}`,
        });

        // Move the user to the new voice channel
        newState.setChannel(channel);

        // Delete the voice channel when it becomes empty
        channel.on('voiceStateUpdate', (oldState) => {
            if (oldState.channel.members.size === 0) oldState.channel.delete();
        });
    }
});

I tried to find a solution to my problem in the discord.js documentation, but I couldn’t find anything suitable.


Solution

  • According to the discord.js docs, (See Here) the GuildChannelManager.create() method takes only one parameter - "options". As well as the name issue, you may also need to set your channel type to the ChannelType.GuildVoice, as shown in the example in the docs. In the above, on lines 6-10, try removing the first string parameter like so:

    const { ChannelType } = require("discord.js");
    
    const channel = await newState.guild.channels.create({
                type: ChannelType.GuildVoice,
                parent: newState.channel.parent,
                name: `channel-${newState.member.user.username}`,
            });
    

    Do also note that this may not work for some users, as the channel name might contain an invalid string. Perhaps use their ID instead?

    name: `channel-${newstate.member.user.id}`,