admin管理员组

文章数量:1404924

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

本文标签: javascripthow to locate the voice chat that the discord bot is connected toStack Overflow