Hey everyone! I’m working on a Discord bot and I’ve run into a bit of a problem. Right now, my bot can be disconnected from any voice channel, but I want to make it so it can only be disconnected from one specific channel. Ideally, users should only be able to disconnect the bot if they’re in the same voice channel as the bot. Here’s a snippet of my current code:
const { Command } = require('discord-helper');
class ExitVoiceCommand extends Command {
constructor(bot) {
super(bot, {
name: 'exit',
aliases: ['x'],
group: 'audio',
memberName: 'exit',
description: 'Exits the voice channel :(',
guildOnly: true
});
}
execute(msg) {
let userVoiceChannel = msg.member.voice.channel;
if (!userVoiceChannel) {
msg.reply('Please join a voice channel and try again');
return;
}
const botConnection = this.bot.voice.connections.get(msg.guild.id);
if (!botConnection) return msg.reply('I\'m not in a voice channel.');
botConnection.channel.leave();
return msg.reply(`I\'ve left the voice channel - "**__${botConnection.channel.name}__**"`);
}
}
Any ideas on how I can modify this to achieve what I’m looking for? Thanks in advance for your help!
As someone who’s been in your shoes, I’d suggest a slightly different approach. Instead of hardcoding a specific channel ID, consider using a configuration file or database to store allowed channels. This way, you can easily update or add multiple channels without changing the code.
Here’s a rough idea:
- Create a config file (e.g., ‘config.json’) with an array of allowed channel IDs.
- In your execute function, load this config and check against it.
- Also, ensure the user is in the same channel as the bot.
This method gives you more flexibility and makes your bot more maintainable in the long run. I’ve used this approach in several of my bots, and it’s saved me a lot of headaches, especially when managing multiple servers with different requirements.
To restrict your bot’s disconnection to a specific voice channel, you can modify the execute function. First, define the allowed channel ID at the top of your class. Then, add a check to compare the bot’s current channel with the allowed channel:
const ALLOWED_CHANNEL_ID = ‘your-specific-channel-id’;
execute(msg) {
const botConnection = this.bot.voice.connections.get(msg.guild.id);
if (!botConnection) return msg.reply(‘I’m not in a voice channel.’);
if (botConnection.channel.id !== ALLOWED_CHANNEL_ID) {
return msg.reply('I can only be disconnected from the designated channel.');
}
if (msg.member.voice.channel !== botConnection.channel) {
return msg.reply('You must be in the same voice channel as the bot to disconnect it.');
}
botConnection.channel.leave();
return msg.reply(`I've left the voice channel - "${botConnection.channel.name}"`);
}
This ensures the bot can only be disconnected from the specified channel and by users in the same channel.
hey flyingeagle, try adding a check in your execute function.
if (userVoiceChannel.id !== ‘your-specific-channel-id’) {
return msg.reply(‘sry, can only disconnect from designated channel’);
}
this ensures disconnects only happen in the right channel. hope that hlps!