user11582821
user11582821

Reputation:

How to fix 'args is not defined' error in Node.js?

by wanting to create a musical bot, I quickly noticed by ending coded that the bot does not start for an error that is indicated below the code. Thank you for enlightening me as possible.

Thanks to help me

switch (args[0].tolowerCase()) {
  case "play":
    if (!args[1]) {
      message.channel.send("Indiquez le __lien__ s'il vous plaît !");
      return; 
   }

if(!message.member.voiceChannel) {
  message.channel.send("Vous devez vous trouvez dans un salon **vocal** 
!");
  return;     

    }

    if(!servers[message.guild.id]) servers[message.guild.id] = {
      queue : []
    }

   var server = servers[message.guild.id]

    server.queue.push(args[1]);

    if(!message.guild.voiceConnection) 
message.member.voiceChannel.join().then(function(connection) {
  play(connection, message);
    })
    break;
  case "skip" :
    var server = servers[message.guild.id];

    if (server.dispatcher) server.dispatcher.end();
    break;
  case "stop" :
    var servers = servers[message.guild.id];

    if (message.guild.voiceConnection) 
    message.guild.voiceConnection.disconnect();
        break;
  default:
    message.channel.send("La commande saisie est invalide !");
}

This is the console log.

ReferenceError: args is not defined
    at Object.<anonymous> (C:\Users\Thomas\Desktop\Nolosha 
 Bot\index.js:170:1)

Upvotes: 1

Views: 8708

Answers (1)

slothiful
slothiful

Reputation: 5623

Welcome to Stack Overflow.

The error you receive is saying that you haven't declared the variable args, but you're trying to use it anyway. Make sure that you have set args prior to using it.

const args = message.content.trim().split(/ +/g);

This code takes the message content, removes any whitespace on the beginning or end, and then splits it up into pieces by spaces, returning an array. Keep in mind that it can't be redefined within the same block because it's a constant.

Upvotes: 3

Related Questions