Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何检查bot是否与用户处于同一通道中?_Javascript_Discord_Discord.js_Bots - Fatal编程技术网

Javascript 如何检查bot是否与用户处于同一通道中?

Javascript 如何检查bot是否与用户处于同一通道中?,javascript,discord,discord.js,bots,Javascript,Discord,Discord.js,Bots,我一直在制作一个music discord机器人,我想检查用户是否与机器人处于同一个语音频道 我尝试了以下方法,但无效: 让voiceBot=client.voice.connections.channel; const voiceChannel=msg.member.voice.channel; 如果(!voiceBot==voiceChannel) 返回msg.channel.send('您需要与bot位于同一个通道中!') 这将循环浏览bot当前所在的语音频道列表,并根据消息作者当前语音

我一直在制作一个music discord机器人,我想检查用户是否与机器人处于同一个语音频道

我尝试了以下方法,但无效:

让voiceBot=client.voice.connections.channel;
const voiceChannel=msg.member.voice.channel;
如果(!voiceBot==voiceChannel)
返回msg.channel.send('您需要与bot位于同一个通道中!')
这将循环浏览bot当前所在的语音频道列表,并根据消息作者当前语音频道的id检查频道id。它将返回通道对象,如果未找到任何内容,则返回
未定义的
。如果只想检查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!");
    };
作为连接的集合,您可以使用
.some()
方法迭代这些连接,并检查其中是否有与
成员.voice.channelID
相同的
channel.id

如果成员和bot位于同一通道中,则该方法将返回
true
,否则返回
false
。所以你可以这样使用它:

const inSameChannel=client.voice.connections.some(
(连接)=>connection.channel.id==msg.member.voice.channelID
)
如果(!inSameChannel)
返回msg.channel.send('您需要与bot位于同一个通道中!')

比较ID而不是对象。但请稍候,我如何获取机器人通道的ID(不客气;)您也可以只检查
msg.guild.me.voice.channelID
,而不必反复访问
client.voice.connections
。这将要快得多,尤其是当机器人在多个行会中时。但是我如何仅在机器人在语音频道中时进行检查
    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!");
    };