Lucifer Morningstar
Lucifer Morningstar

Reputation: 13

Discord.js/javascript issue with a error. ReferenceError: command not defined

const Discord = require("discord.js");


const bot = new Discord.Client();

let prefix="!"

bot.on("ready", () => {
  bot.user.setStatus("idle");
console.log(`Demon Is Online`)


const update = () => {
    bot.user.setActivity("!help | residing on " + bot.guilds.size + " Servers", { type: 'WATCHING' }); 
};

bot.on('ready', update);
bot.on('guildCreate', update);
bot.on('guildRemove', update);

bot.on("message", message => {
    if(message.author.bot) return;
    if(message.channel.type === "dm") return;

    let messageArray = message.content.split(" ");
    let command = messageArray[0];
    let args = messageArray.slice(1);

   if(!command.startsWith(prefix)) return;

});

    if(command === `${prefix}userinfo`) {
        let embed = new Discord.RichEmbed()
        .setAuthor(message.author.username)
        .setColor("#3498db")
        .setThumbnail( `${message.author.avatarURL}`)
        .addField("Name", `${message.author.username}#${message.author.discriminator}`)
        .addField("ID", message.author.id)
       
        message.reply("I've Sent Your User Info Through DM!");
        message.channel.send({embed}); 

}}); 


    if("command" === `${prefix}help`) {
        let embed = new Discord.RichEmbed()
        .addField("!help", "gives you this current information")
        .setTitle("Help")
        .setColor("#3498db")
        .addField("!userinfo", "gives you info about a user(currently being worked on)")
        .addField("!serverinfo","gives you info about a server(currently working on it)") 
        .addField("link to support server","https://discord.gg/NZ2Zvjm")
        .addField("invite link for bot","https://discordapp.com/api/oauth2/authorize?client_id=449983742224760853&permissions=84993&scope=bot")


  message.reply("here's a list of commands that i'm able to do")
        message.channel.send({embed});
    

 
    messageArray = message.content.split("");
    let command = messageArray[0];
        if(command === `${prefix}serverinfo`) {
            let embed = new Discord.RichEmbed()
            .setAuthor(message.author.username)
            .setColor("#3498db")
            .addField("Name", `${message.guild.name}`)
            .addField("Owner", `${message.guild.owner.user}`)
            .addField("Server ID" , message.guild.id)
            .addField("User Count", `${message.guild.members.filter(m => m.presence.status !== 'offline').size} / ${message.guild.memberCount}`)
            .addField("Roles", `${message.guild.roles.size}`);


            message.channel.send({embed});
         } 
        };

bot.login("token goes here")

I have this code that I need help with its stopping my discord bot from coming online. I use javascript and discord.js and node.js and could use help I tried searching youtube and also servers on discord and asking some friends but they all tell me to learn the language which I been trying. but anyways here's my error

output

Upvotes: 1

Views: 10822

Answers (1)

Matt Pengelly
Matt Pengelly

Reputation: 1596

command doesnt exist in the context that its being referenced.

you have

bot.on("message", message => {
    if(message.author.bot) return;
    if(message.channel.type === "dm") return;

    let messageArray = message.content.split(" ");
    let command = messageArray[0];
    let args = messageArray.slice(1);

   if(!command.startsWith(prefix)) return;

});

and then youre referencing command below here. which means it is out of scope, since it is defined int eh scope of bot.on('message' ... paste the command snippet into the bot.on('message' code and, reset ur bot and try sending it a message, i think you'll see desired results.

full example will look like this:

bot.on('ready', update)
bot.on('guildCreate', update)
bot.on('guildRemove', update)

bot.on('message', message => {
  if (message.author.bot) return
  if (message.channel.type === 'dm') return

  let messageArray = message.content.split(' ')
  let command = messageArray[0]
  let args = messageArray.slice(1)

  if (!command.startsWith(prefix)) return

  if (command === `${prefix}userinfo`) {
    let embed = new Discord.RichEmbed()
      .setAuthor(message.author.username)
      .setColor('#3498db')
      .setThumbnail(`${message.author.avatarURL}`)
      .addField(
        'Name',
        `${message.author.username}#${message.author.discriminator}`
      )
      .addField('ID', message.author.id)

    message.reply("I've Sent Your User Info Through DM!")
    message.channel.send({ embed })
  }

  if (`${prefix}help` === 'command') {
    let embed = new Discord.RichEmbed()
      .addField('!help', 'gives you this current information')
      .setTitle('Help')
      .setColor('#3498db')
      .addField(
        '!userinfo',
        'gives you info about a user(currently being worked on)'
      )
      .addField(
        '!serverinfo',
        'gives you info about a server(currently working on it)'
      )
      .addField('link to support server', 'https://discord.gg/NZ2Zvjm')
      .addField(
        'invite link for bot',
        'https://discordapp.com/api/oauth2/authorize?client_id=449983742224760853&permissions=84993&scope=bot'
      )

    message.reply("here's a list of commands that i'm able to do")
    message.channel.send({ embed })

    messageArray = message.content.split('')
    let command = messageArray[0]
    if (command === `${prefix}serverinfo`) {
      let embed = new Discord.RichEmbed()
        .setAuthor(message.author.username)
        .setColor('#3498db')
        .addField('Name', `${message.guild.name}`)
        .addField('Owner', `${message.guild.owner.user}`)
        .addField('Server ID', message.guild.id)
        .addField(
          'User Count',
          `${
            message.guild.members.filter(m => m.presence.status !== 'offline')
              .size
          } / ${message.guild.memberCount}`
        )
        .addField('Roles', `${message.guild.roles.size}`)

      message.channel.send({ embed })
    }
  }
})

bot.login('token goes here')

Note i didnt fix ALL your issues, there seems to be a few more stragglers left, i just tried to answer your specific question :)

Upvotes: 1

Related Questions