Getting 'message not defined' ReferenceError when creating a Discord bot

I’m developing a Discord bot with discord.js, but I’m encountering a ReferenceError that states ‘message is not defined.’ Since I’m new to programming with JavaScript, I’m unsure about what could be causing this problem.

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

const bot = new Discord.Client();
let commandPrefix = '!';

bot.on('ready', () => {
  bot.user.setStatus('online');
  console.log('Bot is up and running!');
});

bot.on('ready', guild => {
  bot.user.setActivity('!help | available on ' + bot.guilds.size + ' servers');
});

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

  let messageParts = incomingMessage.content.split(' ');
  let command = messageParts[0];
  let args = messageParts.slice(1);

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

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

    incomingMessage.reply('Check your user information in your DMs!');
    incomingMessage.channel.send({embed: userEmbed});
  }
});

if(command === `${commandPrefix}help`) {
  let helpEmbed = new Discord.RichEmbed()
    .addField('!help', 'shows you this message')
    .setTitle('Help Menu')
    .setColor('#3498db')
    .addField('!userinfo', 'provides user information')
    .addField('!serverinfo', 'gives details about the server');

  incomingMessage.reply('Here are the commands I can execute');
  incomingMessage.channel.send({embed: helpEmbed});
}

function handleCommand() {
  messageParts = incomingMessage.content.split(' ');
  let command = messageParts[0];

  if(command === `${commandPrefix}serverinfo`) {
    let serverEmbed = new Discord.RichEmbed()
      .setAuthor(incomingMessage.author.username)
      .setColor('#3498db')
      .addField('Server Name', incomingMessage.guild.name)
      .addField('Owner', incomingMessage.guild.owner.user)
      .addField('Server ID', incomingMessage.guild.id)
      .addField('Online Members', `${incomingMessage.guild.members.filter(member => member.presence.status !== 'offline').size} / ${incomingMessage.guild.memberCount}`);

    incomingMessage.channel.send({embed: serverEmbed});
  }
}

bot.login('your_token_here');

I have been trying to improve my JavaScript skills with tutorials, but I keep making simple errors related to variable scope and syntax. The reference error mentions the message variable, and I’m struggling to find out why it’s not recognized. Any assistance would be greatly appreciated!

Your code’s outside the message event handler - that’s the problem. The if(command === '${commandPrefix}help') block sits outside bot.on('message'), so it can’t see the incomingMessage variable. That’s why you’re getting the ReferenceError.

Move the help command inside the message event handler, right after your userinfo command. Your handleCommand() function has the same issue - it’s trying to use incomingMessage without access to it. Either move the serverinfo logic directly into the message handler or pass incomingMessage as a parameter.

I hit the same scope issues when I started with discord.js. Variables in event handlers only work inside those handlers unless you pass them around.

Your commands are scattered inside and outside the message event listener - that’s the problem. Everything after your userinfo command sits outside the bot.on('message') scope, so those parts can’t see the incomingMessage variable. I hit this same issue with my first bot. Fix is simple: move all command logic inside the message event handler. Your help command and handleCommand function are trying to use variables that don’t exist where they’re sitting. Also, you’re using RichEmbed which got deprecated - switch to MessageEmbed if you update discord.js. This scope mess will crash your bot when someone tries those commands.

It looks like your help command and handleCommand function are outside the message event handler, which makes incomingMessage undefined in those areas. To fix this, just move everything inside the bot.on(‘message’) block and you should be all set!