I am trying to make a small Music Bot in Discord.js v16.6 which can play local files, but I got the error:
ReferenceError: voice is not defined
I don't understand why voice is not defined. How can I fix this error?
Here is the part of my code:
if( isReady && startsWithInList(message.content, settings.commandPlay) ) {
isReady = false;
const args = message.content.slice(10).trim().split(' ');
if( args.length != 1 || !args[0] || args[0] === "" ) {
return message.channel.send(settings.warningPlayArgsSentence);
}
var voiceChannel = message.member.voice.channel;
voice.channel.join().then( connection => {
const dispatcher = connection.play(settings.filesDir+args[0]+'.mp3')
dispatcher.on('finish', () => {
voiceChannel.leave();
isReady = true;
})
});
}
*I can't supply the whole code because stackoverflow won't allow this
Discord.js only goes up to version v13.4 as of the time of writing this. The code here looks like it's for version v12. I'm guessing you're on Node.js v16.6.
Regarding the actual issue, you're referencing a variable voice
that doesn't exist. See this line
voice.channel.join().then( connection => {
I'm guessing you just meant to reference the variable created directly before this line instead.
if( isReady && startsWithInList(message.content, settings.commandPlay) ) {
isReady = false;
const args = message.content.slice(10).trim().split(' ');
if( args.length != 1 || !args[0] || args[0] === "" ) {
return message.channel.send(settings.warningPlayArgsSentence);
}
var voiceChannel = message.member.voice.channel;
// See below for the change
voiceChannel.join().then( connection => {
const dispatcher = connection.play(settings.filesDir+args[0]+'.mp3')
dispatcher.on('finish', () => {
voiceChannel.leave();
isReady = true;
})
});
}