Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

121
Views
ReferenceError: voice is not defined discord.js

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

7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

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;  
      })
    });
  }
7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs