javascript - how to locate the voice chat that the discord bot is connected to - Stack Overflow

I am making a discord bot that is being activated by voice recognition, im at the very beginning right

I am making a discord bot that is being activated by voice recognition, im at the very beginning right now im making him join a voice channel (which is working), and im trying to make a mand to make him leave.

const mando = require('discord.js-mando');

class LeaveChannelCommand extends mando.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;

I am making a discord bot that is being activated by voice recognition, im at the very beginning right now im making him join a voice channel (which is working), and im trying to make a mand to make him leave.

const mando = require('discord.js-mando');

class LeaveChannelCommand extends mando.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;

right now you can type !leave from anywhere in the server and the bot leaves, i want to make it possible to control him only from the same voice channel, what should i do

Share Improve this question asked Mar 10, 2019 at 15:32 SwaggZ ReYanSwaggZ ReYan 211 gold badge1 silver badge2 bronze badges 1
  • Sorry, I don't get what is your question. You want to control the bot from leaving the voice channel in the same voice channel? Can you explain this and I will help you! :-) – Gilles Heinesch Commented Mar 10, 2019 at 16:00
Add a ment  | 

1 Answer 1

Reset to default 2

It's fairly easy to do. All you need to do is grab the bot's voiceChannel and the user's voiceChannel (if he is in one) and check if they are the same.

Below you can find some example code. Give it a try and let me know how it goes.

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 mand if you share the same voiceChannel as the client!');
  }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744877102a4598613.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信