Creating a Discord bot that responds with random messages from predefined answers

I’m working on a Discord bot that should select a random response from a list of possible answers when users ask questions. The bot should also display an error when no arguments are provided or when it can’t recognize the input.

My current implementation picks random responses correctly, but I’m having trouble with argument detection. The bot fails to identify the question from the first argument (params[0]) that users provide.

Here’s my current code:

module.exports.execute = async(discord, msg, params, botInstance, sendMsg) => {
  var responses = ["answer1"];
  var randomIndex = Math.floor(Math.random() * responses.length);

  const userQuery = parseInt(params[0], 10);
  if (!userQuery)
    return msg.reply("please ask a question");

  msg.channel.send("" + responses[randomIndex] + "");
}

What am I doing wrong with the argument parsing?

Yeah, parseInt is definitely the issue like others said, but there’s another gotcha here. Your params array is probably splitting the user’s question into separate words, so params[0] might just be “what” instead of the full question “what should I do today”. I ran into this exact same thing building a similar bot - drove me crazy trying to figure out why longer questions kept breaking. Log what params[0] actually contains first. You’ll probably need to use params.join(’ ') to rebuild the full question or change how your bot handles command parsing.

Yeah, parseInt doesn’t make sense here. Users send text questions, not numbers. Just use if (!params[0]) instead - way simpler and actually works. Also, you’ll want params.join(' ') to handle multi-word questions.

You’re using parseInt() on what’s probably a text question. When users ask stuff through Discord commands, they send strings like “what should I do today” - not numbers. parseInt() returns NaN for text, which is falsy and triggers your error.

Just check if the parameter exists as a string instead:

const userQuery = params[0];
if (!userQuery || userQuery.trim() === "")
    return msg.reply("please ask a question");

Now you’re checking for an actual question instead of trying to turn text into numbers. Had this exact same issue with my first Discord bot - wasted hours debugging before I realized I was treating user input like integers when they’re obviously sending text.