我正在制作一个通过语音识别激活的不和谐机器人,我从一开始就让他加入语音 channel (正在运行),并且我试图发出命令让他离开。
const commando = require('discord.js-commando');
class LeaveChannelCommand extends commando.Command
{
constructor(client){!
super(client,{
name: 'leave',
group: 'music',
memberName: 'leave',
description: 'leaves a voice channel'
});
}
async run(message, args)
{
if(message.guild.voiceConnection)
{
message.guild.voiceConnection.disconnect();
}
else
{
message.channel.sendMessage("seccessfully left")
}
}
}
module.exports = LeaveChannelCommand;
现在您可以从服务器中的任何位置输入 !leave ,机器人就会离开, 我希望只能通过同一个语音 channel 来控制他, 我该怎么办
最佳答案
这很容易做到。您需要做的就是获取机器人的 voiceChannel 和用户的 voiceChannel(如果他在其中)并检查它们是否相同。
下面您可以找到一些示例代码。尝试一下,让我知道进展如何。
async run(message, args)
{
// If the client isn't in a voiceChannel, don't execute any other code
if(!message.guild.voiceConnection)
{
return;
}
// Get the user's voiceChannel (if he is in one)
let userVoiceChannel = message.member.voiceChannel;
// Return from the code if the user isn't in a voiceChannel
if (!userVoiceChannel) {
return;
}
// Get the client's voiceConnection
let clientVoiceConnection = message.guild.voiceConnection;
// Compare the voiceChannels
if (userVoiceChannel === clientVoiceConnection.channel) {
// The client and user are in the same voiceChannel, the client can disconnect
clientVoiceConnection.disconnect();
message.channel.send('Client has disconnected!');
} else {
// The client and user are NOT in the same voiceChannel
message.channel.send('You can only execute this command if you share the same voiceChannel as the client!');
}
}
关于javascript - 如何找到discord机器人所连接的语音聊天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55089293/