I am trying to make my bot leave the voice channel, using the code from discordjs guide (discord version dev 14.0.0 )
when I try to acces the created connection, it errors: ReferenceError: myVoiceChannel is not defined.
my code in the leave command:
const Command = require('../Structures/Command.js');
const { getVoiceConnection } = require("@discordjs/voice");
const connection = getVoiceConnection(myVoiceChannel.guild.id);
module.exports = new Command({
name: "lvc",
description: "leaves voice",
permission: "CONNECT",
async run(message, args, client) {
connection.destroy();
}
})
my code in the join command:
const Command = require('../Structures/Command.js');
const { VoiceConnectionStatus, AudioPlayerStatus, AudioPlayer, joinVoiceChannel } = require('@discordjs/voice');
module.exports = new Command({
name: "join",
description: "joins voice",
permission: "CONNECT",
async run(message, args, client) {
const connection = joinVoiceChannel({
channelId: "904277856773869583",
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator
})
},
})
CodePudding user response:
In your first code you've added the code
const connection = getVoiceConnection(myVoiceChannel.guild.id);
Nowhere in your code you've defined myVoiceChannel. This is the reason why this error is being thrown. You could use this example instead:
const Command = require('../Structures/Command.js');
const { getVoiceConnection } = require("@discordjs/voice");
module.exports = new Command({
name: "lvc",
description: "leaves voice",
permission: "CONNECT",
async run(message, args, client) {
const connection = getVoiceConnection(message.guild.id);
if(connection) connection.destroy();
else return message.channel.send({ content: `I am not connected to a voice channel` });
}
})
