[英]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.