Getting declaration or statement expected error in my Discord.js bot code

I’m working on a Discord bot using JavaScript and keep running into a “declaration or statement expected” error. I can’t figure out what’s wrong with my syntax. The bot is supposed to respond to messages and has a ping command, but something in my code structure is causing this error.

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: "with commands",
      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 arguments = msg.content.substring(CMD_PREFIX.length).split(" ");
    
    switch (arguments[0]) {
      case "test":
        msg.channel.send("Working!");
        break;
    }
  }
});

client.login(BOT_TOKEN);

Can someone help me spot what’s causing this syntax error? I’ve been staring at this code for hours and can’t see the issue.

The syntax error stems from the logic structure of your code. You’re checking if the message matches “hey” and then nesting command handling within that condition. This means commands can only execute if someone types “hey” first, which isn’t how it should work. Instead, separate these checks: respond to “hey” in one conditional block and then check for your command prefix in a different condition. This will clarify your logic flow and resolve the issue, similar to the challenge I faced when managing multiple responses.

yeah, your logic flow’s totally broken. you check for “hey” before handling commands, so they won’t work unless “hey” is typed first. just move the prefix check above the “hey” check to fix it, don’t nest it.

Your nested if statements are messed up. You’ve got the prefix check and command logic stuck inside the “hey” message check, which makes no sense. Command handling should be totally separate from greeting responses. Pull that prefix checking and switch statement out of the “hey” condition. Also, your setPresence syntax is old - newer Discord.js versions use activities instead of game. The nested structure is what’s breaking your parser and throwing that syntax error. I had this exact same issue when I started with Discord.js and wasted hours debugging it.