繁体   English   中英

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

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

我有一个问题:我想检查 Bot 是否在 msg.guild 的频道上。 我有一个命令?checkchannel来回复机器人是否在所述公会的语音频道上。 如果机器人在其中,它应该回复:“在语音频道上”,如果他不在,他应该回复他不在。

谢谢你。

真的很简单,试试这个:

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

笔记:

这只会检查messagechannel而不是机器人是否连接到服务器上的任何voice channel

如果你想检查,如果他连接到任何voice channel ,你应该检查Viriatos 的回答

编辑:

您还可以将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')

但是你必须自己决定这是否更清楚。


参考:

如果我理解正确并且您想检查机器人是否在与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