我一直在制作音乐不和谐机器人,我想检查用户是否与机器人在同一语音频道中。
我尝试了以下方法,但没有用:
let voiceBot = client.voice.connections.channel;
const voiceChannel = msg.member.voice.channel;
if(!voiceBot == voiceChannel)
return msg.channel.send('you need to be in the same channel as the bot!')
答案 0 :(得分:1)
client.voice.connections.find(i => i.channel.id === msg.member.voice.channel.id);
这将遍历机器人当前所在的语音频道列表,并根据消息作者的当前语音频道的 ID 检查频道 ID。它将返回通道对象,如果未找到任何内容,则返回 undefined
。如果您只想检查 id,可以在 if 语句中使用它:
if (!client.voice.connections.find(i => i.channel.id === msg.member.voice.channel.id)) {
console.log("The member isn't in a shared voice channel!");
};
或者将其定义为常量并从中获取其他信息:
const vc = client.voice.connections.find(i => i.channel.id === msg.member.voice.channel.id);
if (vc) { //If the channel is valid
console.log(`The voice channel id is: ${vc.channel.id}`);
} else { //If the channel is undefined
console.log("The member isn't in a shared voice channel!");
};
答案 1 :(得分:1)
由于 voice.connections
是连接的集合,您可以使用 .some()
方法遍历这些连接并检查是否有任何 connection's channel 具有相同的 channel.id
member.voice.channelID
。
如果成员和机器人在同一频道中,.some()
方法将返回 true
,否则返回 false
。所以你可以这样使用它:
const inSameChannel = client.voice.connections.some(
(connection) => connection.channel.id === msg.member.voice.channelID
)
if (!inSameChannel)
return msg.channel.send('You need to be in the same channel as the bot!')