如何找到Discord机器人连接到的语音聊天

时间:2019-03-10 15:32:52

标签: javascript ffmpeg bots discord discord.js

我正在制作一个由语音识别激活的不和谐机器人,即时通讯从一开始就立即让他加入了语音通道(正在运行),并试图发出命令使他离开。< / p>

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,然后漫游器会离开, 我想让只能从同一语音通道控制他, 我该怎么办

1 个答案:

答案 0 :(得分:0)

这很容易做到。您需要做的就是抓住机器人的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!');
  }
}