I have two commands: join and disconnect. Join function makes bot to join voice channel using method joinVoiceChannel, whilst disconnect command removes bot from the channel by using getVoiceConnection method:
join.js
const Command = require('../handlers/Command.js');
const { joinVoiceChannel } = require('@discordjs/voice');
module.exports = new Command({
name: "join",
description: "Joins voice channel",
async run(message, args, client) {
const channel = message.member.voice.channel;
if (!channel) return message.channel.send('You need to be in a voice channel.');
const permissions = channel.permissionsFor(message.client.user);
if (!permissions.has('CONNECT') || !permissions.has('SPEAK')) return message.channel.send("You don't have the right permission.");
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
});
}
});
disconnect.js
const Command = require('../handlers/Command.js');
const { getVoiceConnection } = require('@discordjs/voice');
module.exports = new Command({
name: "disconnect",
description: "Disconnects from the voice channel",
async run(message, args, client) {
try {
const channel = message.member.voice.channel;
const connection = getVoiceConnection(channel.guild.id);
connection.destroy();
} catch (error) { }
}
});
Can I somehow import the connection constant from join.js to disconnect.js to avoid using other methods?