Giuliopime
Giuliopime

Reputation: 1187

Creating channel and setChannel troubles

I'm having trouble creating a discord bot, what it should do is detect whenever someone jkoins a specific voice Chat, and if a user does, the bot would have to create a new channel with, as the name, the nickname of the user who joined, then move that user to the new created channel and set that channel to private so no one can join.

Currently my problems are: -I can't set the name of the new channel as the nickname of the user -I can't move the user to that channel -And I think the rest may work

Here is the part of my code:

client.on('voiceStateUpdate', (oldMember, newMember) => {
    if(newMember.channelID != '693086073244483614') return;

    const nick = newMember.nickname;
    newMember.guild.channels.create('test', { type: 'voice' })
    .then(() => {
        newMember.setChannel('test');
        console.log(`Hello from ${newMember.channel}!`);
        const nChannel = newMember.channel;
        nChannel.setParent('690292158636360032');
        nChannel.overwritePermissions([
            {
                parent: '#690292158636360032',
                id: '532236212967047169',
                deny: ['CONNECT'],
            },
        ]);
    });
});

Any help would be appriciated, I'm new to both discord bots and javascript, so thanks a lot!

Upvotes: 0

Views: 1333

Answers (2)

Dorian349
Dorian349

Reputation: 1589

client.on('voiceStateUpdate', (oldMember, newMember) => {
  var oldUserChannel = oldMember.voiceChannel;
  var newUserChannel = newMember.voiceChannel;
  if(oldUserChannel === undefined && newUserChannel !== undefined) { //User joined the channel.
    if(newMember.voiceChannel.id != '693086073244483614') return; //Check if the user join the right voice channel.

    let nick = newMember.nickname;
    if(nick == null){
      nick = newMember.user.username; //If the member doesn't have a nickname.
    }
    newMember.guild.createChannel(nick, { type: 'voice' })
    .then((nChannel) => {
        console.log(`Hello from ${nChannel}!`);
        nChannel.setParent('690292158636360032'); //Your category ID
        nChannel.overwritePermissions(nChannel.guild.defaultRole.id, {
            VIEW_CHANNEL: false
        });
        newMember.setVoiceChannel(nChannel)
    });

  } else if(newUserChannel === undefined){ //User left the channel.
    if(oldMember.voiceChannel.parent == null) return;
    if(oldMember.voiceChannel.parent.id != '690292158636360032') return; //Check if the voice channel is under the specified category.
    if(oldMember.voiceChannel.members.first() == null){ //There is no more users in this channel.
      oldMember.voiceChannel.delete(); //Delete the channel.
    }
  }
});

Here is the final result, fully working. I also add a system to automatically remove the channel if there is nobody in. I have added some annotations, tell me if you have any questions.

Upvotes: 0

yogurtsyum
yogurtsyum

Reputation: 348

The Client#voiceStateUpdate event does not return a member, it returns a VoiceState. https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=e-voiceStateUpdate

From the VoiceState, you can get a member with <VoiceState>.member.

So to fix your error, replace const nick = newMember.nickname to const nick = newMember.member.nickname.

Upvotes: 2

Related Questions