简体   繁体   English

Discord.js 检查机器人是否在语音通道中

[英]Discord.js check if bot is in a voice channel

I have a question: I want to check if the Bot is on a channel in msg.guild.我有一个问题:我想检查 Bot 是否在 msg.guild 的频道上。 I have a command ?checkchannel that replies whether or not the bot is on a voice channel of said guild.我有一个命令?checkchannel来回复机器人是否在所述公会的语音频道上。 If the bot is on one it should reply: "Is on a voice channel" and if he is not he should reply he isn't.如果机器人在其中,它应该回复:“在语音频道上”,如果他不在,他应该回复他不在。

Thank you.谢谢你。

It's really simple, try this:真的很简单,试试这个:

// If the bot is not connected to a voice channel, the 'channel' object should be 'undefined'
if(msg.guild.voice.cannel)
  {
    msg.channel.send(`I'm in a voice channel!`);
  }
else
  {
    console.log(`I'm not in a voice channel!`);
  }

Note:笔记:

This only checks the channel of the message , not if the bot is connected to any voice channel on the server!这只会检查messagechannel而不是机器人是否连接到服务器上的任何voice channel

If you want to check, if he's connected to any voice channel you should check out Viriatos answer如果你想检查,如果他连接到任何voice channel ,你应该检查Viriatos 的回答

Edit:编辑:

You can also reduce Vitiaro's answer to a single line:您还可以将Vitiaro 的答案简化为一行:

msg.guild.channels.cache.some(channel => (channel.type === 'voice' && channel.members.has(Client.user.id)) ? msg.channel.send('It is on a voice channel') : msg.channel.send('It is not on a voice channel')

But you have to decide for yourself whether this is clearer or not.但是你必须自己决定这是否更清楚。


References:参考:

If I understood correctly and you want to check whether the bot is in any voice channel of the guild corresponding to msg.guild :如果我理解正确并且您想检查机器人是否在与msg.guild对应的公会的任何语音通道中:

if(msg.guild.channels.cache.some(channel => (channel.type === 'voice' && channel.members.has(Client.user.id)) {
    msg.channel.send('It is on a voice channel'); // Replies on the same channel the command was sent to
}
else {
    msg.channel.send('It is not on a voice channel');
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM