簡體   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