javascript - 如何找到discord机器人所连接的语音聊天

标签 javascript ffmpeg bots discord discord.js

我正在制作一个通过语音识别激活的不和谐机器人,我从一开始就让他加入语音 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/

相关文章:

ffmpeg - Alfresco 无法创建视频缩略图

javascript - 将原始字符串源转换为 html 以使用 jquery 选择器

javascript - 在 asp.Net 中调用 javascript 函数正则表达式验证器失败

javascript - AngularJS:ng-repeat 添加一些带有第一个元素的文本

正则表达式 : How can I surround all words starting with @ with <b> tags?

android - 如何将 AVFrame 转换为 glTexImage2D 使用的纹理?

java - MediaCodec 解码时 AV 同步

javascript - 如何阻止恶意机器人访问者的链接?

python - 如何使我的Discord机器人播放YouTube上的音频

javascript - 类型错误 : Cannot read property 'message' of undefined - Twitter API