简体   繁体   English

如何检查机器人是否与用户在同一频道?

[英]How to check if the bot is in the same channel as the user?

I have been making a music discord bot and I wanted to check if the user is in the same voice channel as the bot.我一直在制作音乐 discord 机器人,我想检查用户是否与机器人在同一个语音通道中。

I tried the following but it didn't work:我尝试了以下但没有奏效:

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!')
    client.voice.connections.find(i => i.channel.id === msg.member.voice.channel.id);

This will loop through the list of voice channels that the bot is currently in and check the channel id against the id of the message author's current voice channel.这将遍历机器人当前所在的语音频道列表,并根据消息作者当前语音频道的 ID 检查频道 ID。 It will return the channel object, or undefined if nothing is found.它将返回通道 object,如果没有找到则返回undefined You can use it inside of an if statement if you just want to check the ids:如果您只想检查 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!");
    };

Or define it as a constant and get other information from it:或者将其定义为常量并从中获取其他信息:

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

As voice.connections is a collection of connections, you can use the .some() method to iterate over these connections and check if any of the connection's channel has the same channel.id as the member.voice.channelID .由于voice.connections是连接的集合,您可以使用.some()方法遍历这些连接并检查连接的任何通道是否具有与member.voice.channelID相同的channel.id

The .some() method will return true if the member and the bot is in the same channel, false otherwise.如果成员和机器人在同一个频道中, .some()方法将返回true ,否则返回false So you can use it like this:所以你可以像这样使用它:

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!')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 discord.js awaitReactions 过滤器,用于检查用户是否与 bot 处于同一频道 - discord.js awaitReactions filter to check if the user is in the same channel like bot 如何通过节点中的 discord 机器人代码和 javascript 将用户添加到私人频道? - How to add a user to a private channel by discord bot code in node and javascript? 如何检查某个用户的某个频道/角色 - How to check for a certain channel /role by a certain user 如何检查用户是否指定了频道? - How do I check if the user specified a channel? Discord.js 检查机器人是否在语音通道中 - Discord.js check if bot is in a voice channel Bot 在加入和离开频道时播放相同的音频 - Bot plays same audio for joining and leaving the channel discord.js 如何检查机器人是否有来自服务器/频道的权限? - discord.js How to check if the bot has permissions from server/channel? 如何检查用户是否拥有特定语音通道的权限? - How can I check if a user has permissions in a specific voice channel? 如何让 discord.js 机器人在特定频道中说出自定义消息并在消息中标记用户 - How to make a discord.js bot say custom messages in a specific channel & tag a user in a message 如何让 discord 机器人回复用户消息,这样其他人就无法看到频道中的消息? - How to make discord bot reply messages to user such that no one else can see the messages in channel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM