JavaScript Discord Bot Error: Expected Declaration or Statement

I’m getting a “Declaration or statement expected” error when trying to run my Discord bot code. I think there might be a syntax issue but I can’t figure out where exactly. The bot should respond to basic commands and show online status. Here’s my current code:

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

const BOT_TOKEN = "YOUR_TOKEN_HERE";
const CMD_PREFIX = "!";

var client = new discordJS.Client();

client.on("ready", function() {
  console.log("Bot is now online");
  
  client.user.setPresence({
    game: {
      name: "Playing games",
      type: 0
    }
  });
});

client.on("message", function(msg) {
  if (msg.author.equals(client.user)) return;
  
  if (msg.content == "hey") {
    msg.channel.send("Hello there!");
    
    if (!msg.content.startsWith(CMD_PREFIX)) return;
    
    var commands = msg.content.substring(CMD_PREFIX.length).split(" ");
    
    switch (commands[0]) {
      case "test":
        msg.channel.send("Working!");
        break;
    }
  }
});

client.login(BOT_TOKEN);

Can someone help me spot what’s wrong with the syntax?

Your syntax error happens because you’ve got the command logic stuck inside the “hey” response. Commands only work after someone says “hey” since your prefix check and switch statement are trapped in that conditional. You need to restructure your message handler—handle the “hey” greeting separately and put command processing in its own block at the same level. Also, equals() doesn’t work for user comparison in Discord.js. Use msg.author.bot to filter out bot messages instead. This nested mess is probably causing your declaration error too.

Your main problem is the logic flow in your message handler. You’ve got all the command processing code stuck inside the “hey” message check - so commands only work after someone types “hey” first. Move the if (!msg.content.startsWith(CMD_PREFIX)) return; line and everything below it outside the “hey” condition. Also, msg.author.equals(client.user) should be msg.author.id === client.user.id for proper comparison. Your setPresence syntax looks outdated too - newer Discord.js versions use activities instead of game. Just restructure it so commands run separately from the greeting response.

I get the frustration! The problem is your command handling is trapped inside the “hey” response. Check for the prefix first, then handle commands separately. Also, try using msg.author.bot instead of equals for bot detection - it’s more reliable!