Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

getting the substr of a switch case event.

So I am attempting to build a small Discord bot. Its going OK other than I am currently making a switch case of events for the bot to listen too. like so

client.Dispatcher.on(Events.MESSAGE_CREATE, e =>{
  var input = e.message.content;
  switch(input) {
    case '!help':
      e.message.channel.sendMessage("It's dangerous to go alone, take this! ");
      break;
    case '!roll ':
      var num = input.substr(5);  
      e.message.channel.sendMessage(rollDice(num));
      break;
  }
});

my problem is the switch case wants to match the input exactly so if I type !roll 20 it isn't going to do anything because its looking for "!roll " I some how need to take "!roll 20" and exicute the switch case for "!roll " and pass in 20 to the function that is doing my dice roll

function rollDice(value) {
  if (isNaN(value) || value == null || value == '') {
    var result = "You must input a number such as '!roll 20', try again."
  } else {
    var result = Math.floor(Math.random() * value) + 1;
  }
  return result;
}

2 Answers

What if you break down the input before the switch. something like

var input = e.message.content;
if (input.length > 5) {
    var num = parseInt(input.substr(6));
    input = input.substring(0, 5);
}

edit: in your case statement than you need to remove the space after !roll and remove the var num there as well. This will also only work if all of your inputs will follow the same format and be only five characters long but for the !roll.

Hi John,

Maybe you can take a different approach and split the input along the spaces and create an array instead of using substr.

Then, the first element would be the event itself and the optional 2nd element and onward would be the additional inputs needed depending on the event.

Then your switch statement would test against the first element in the array.

This isn't going to work though if the input has extra whitespace. You can use trim() to remove leading and trailing spaces and I found a regex on stackoverflow to reduce extra interior whitespace to a single space.

http://stackoverflow.com/questions/6163169/replace-multiple-whitespaces-with-single-whitespace-in-javascript-string

For example, if the input was

"       !roll              20       "
or
"!roll       20"

, they would both be reduced to "!roll 20"

Revised code:

client.Dispatcher.on(Events.MESSAGE_CREATE, e =>{
  var input = e.message.content;

  var splitInput = input.trim().replace(/\s+/g, ' ').split(" ");

  // test against the first element in the array
  switch(splitInput[0]) {
    case '!help':
      e.message.channel.sendMessage("It's dangerous to go alone, take this! ");
      break;
    // I removed the trailing space you had here
    case '!roll':
      // the number is the second element of the array
      e.message.channel.sendMessage(rollDice(splitInput[1]));
      break;
  }
});