如何检查此人在执行命令并移动到 Node.js 中的其他语音通道后所在的语音通道 ID?



我需要检查该人在执行命令后所在的语音通道ID。如果它在此通道上,我希望机器人移动到另一个所需的通道。

var idchannel = member.get.voiceChannelID;
if(idchannel === "ID"){
//command
// and i need to move this user to another channel.
}
else {
message.reply("You are not on the correct Channel.");
}

编辑:

从 Discord.js v12 开始,由于语音相关功能的变化,我原来的答案将不再有效。

假设您有公会成员member,您可以使用member.voice访问与语音相关的选项。通过该 VoiceState,可以引用VoiceState#channelID属性来访问成员连接到的 VoiceChannel 的 ID(如果有(。把它放在一起,这就是member.voice.channelID.

至于将成员移动到特定频道,您将使用VoiceState#setChannel()方法执行此操作,因此member.voice.setChannel(...).

更新后的代码将如下所示:

const voiceChannelID = member.voice.channelID;
if (voiceChannelID === 'some channel ID') {
member.voice.setChannel('target channel ID') // you may want to await this, async fn required
.catch(console.error);
} else {
message.reply('You are not in the correct channel.') // see last comment
.catch(console.error);
}
<小时 />

原始 (v11(:

您可以使用GuildMember.voiceChannel引用用户连接到的语音通道。然后根据预期的 ID 检查通道的id属性。

若要将成员从一个语音通道移动到另一个语音通道,可以使用GuildMember.setVoiceChannel()方法。

const voiceChannel = message.member.voiceChannel; // Keep in mind this may be undefined if
// they aren't connected to any channel.
if (voiceChannel && voiceChannel.id === "channel ID") {
message.member.setVoiceChannel(/* some other channel or ID */);
} else message.reply("You are not in the correct channel.");

确保从您的承诺中捕捉到任何错误。请参阅此 MDN 文档。

最新更新