[英]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!`);
}
這只會檢查message
的channel
,而不是機器人是否連接到服務器上的任何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.