【问题标题】:Discord.js check if bot is in a voice channelDiscord.js 检查机器人是否在语音通道中
【发布时间】:2021-08-18 15:40:15
【问题描述】:

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

谢谢。

【问题讨论】:

  • @nouvist 这不是问题,我想检查 BOT 是否在语音通道中,但我如何检查他是否在一个语音通道中
  • 哦,没看你的问题。对不起。

标签: javascript discord.js


【解决方案1】:

真的很简单,试试这个:

// 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 answer

编辑:

您还可以将Vitiaro's answer 减少为一行:

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

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


参考资料:

【讨论】:

  • 谢谢,但我需要查找所有频道
【解决方案2】:

如果我理解正确,你想检查bot是否在msg.guild对应的公会的any语音频道中:

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');
}

【讨论】:

  • 我认为您甚至可以将其缩减为这样的一行: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')
  • 但你必须自己决定这是否更清楚。
  • 是的,这更干净。您也可以先将缓存的频道过滤为仅包含 VoiceChannel 的数组,并使用 msg.guild.channels.cache.filter(channel => channel.type === 'voice'),然后在其上执行 .some(...),可能会更快。
  • @Viriato 可能,但我怀疑 1000 个用户会同时运行此命令:D 我认为这会节省几毫秒。是否值得他必须自己决定:D
  • @Neotastisch 我将“更清洁”版本更新为我的答案,在那里看起来比在 cmets 中要好得多^^
猜你喜欢
  • 2021-05-01
  • 2020-09-09
  • 2021-07-04
  • 2021-12-13
  • 2021-04-13
  • 1970-01-01
  • 2020-05-31
  • 2020-07-17
  • 2020-10-24
相关资源
最近更新 更多