未定义 member.voiceChannel

undefined member.voiceChannel

我的 discord 机器人有问题,不知道用户在哪个频道。如果我检查 member.voiceChannel 它总是 returns undefined。即使我在语音频道内。

代码:

let voiceChannel;

voiceChannel = msg.member.voiceChannel;

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

console.log(voiceChannel); 打印 undefined 无论我是否在语音频道中。

这段代码可能就是您的解决方案。请记住修改它以使其适合您的机器人代码,不要只是复制和粘贴它。

// 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!');
}

我刚才在使用 Discord.js v12 时发生了这件事。

显然变量在版本之间更改了名称,现在代替了:

message.member.voiceChannel

是:

message.member.voice.channel

像这样更改变量对我有用

解决方案:

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