【问题标题】:how to locate the voice chat that the discord bot is connected to如何定位不和谐机器人连接的语音聊天
【发布时间】:2019-08-01 00:36:42
【问题描述】:

我正在制作一个通过语音识别激活的不和谐机器人,我一开始现在我让他加入一个语音频道(正在工作),我试图发出命令让他离开。

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 并且机器人离开, 我想让他只能从同一个语音频道控制, 我该怎么办

【问题讨论】:

  • 对不起,我不明白你的问题。您想控制机器人将语音通道留在同一语音通道中吗?你能解释一下吗,我会帮你的! :-)

标签: javascript ffmpeg bots discord discord.js


【解决方案1】:

这很容易做到。您需要做的就是抓取机器人的语音通道和用户的语音通道(如果他在一个)并检查它们是否相同。

您可以在下面找到一些示例代码。试一试,告诉我进展如何。

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

【讨论】:

猜你喜欢
  • 2017-11-20
  • 2018-06-16
  • 1970-01-01
  • 2021-03-07
  • 2021-09-10
  • 1970-01-01
  • 2021-05-24
  • 1970-01-01
  • 2021-05-27
相关资源
最近更新 更多