Syber
Syber

Reputation: 451

undefined member.voiceChannel

I am having trouble with my discord bot not knowing what channel a user is in. If I check member.voiceChannel it always returns undefined. Even if I am inside a voice channel.

Code:

let voiceChannel;

voiceChannel = msg.member.voiceChannel;

if (!voiceChannel) {   
    return msg.reply('Please join a voice channel before using this command.');
}

console.log(voiceChannel); prints undefined regardless of me being in a Voice Channel or not.

Upvotes: 2

Views: 14962

Answers (3)

user14674305
user14674305

Reputation:

solution:

let voiceChannel = msg.member.voice.channel;
voiceChannel.join();
if (!voiceChannel) {   
    return msg.reply('Please join a voice channel before using this command.');
}

Upvotes: 1

AterNyctos
AterNyctos

Reputation: 74

Happened to me just now while using Discord.js v12.

Apparently the variable changed names between versions and now instead of:

message.member.voiceChannel

It is:

message.member.voice.channel

Changing the variable like this worked for me

Upvotes: 5

Yağızhan Y.
Yağızhan Y.

Reputation: 384

This code probably will be your solution. Remember to modify it to make it suitable with your bot's codes, Don't just copy & paste it.

// Only try to join the sender's voice channel if they are in one themselves
if (message.member.voiceChannel) {
  message.member.voiceChannel.join()
    .then(connection => { // Connection is an instance of VoiceConnection
      message.reply('I have successfully connected to the channel!');
    })
    .catch(console.log);
} else {
  message.reply('You need to join a voice channel first!');
}

Upvotes: 3

Related Questions