Search code examples
node.jssteamsteambot

NodeJS Logic Error? Program Stops as Soon as part of it is Executed?


I am pretty new to NodeJS , so sorry :( But you gotta start somewhere. Anyway, I was editing a code that I was trying to finish, and I came across a challenge when trying to make have one of my commands (this is a bot) work. Here's the bit of the code that isn't working :/

steamFriends.on('friendMsg', (id, msg) => {
  if (msg == '') {}
  else {
      LOG(id + ' sent me ' + '"' + msg + '"')
  }
  if (config.get('admins').indexOf(id) == -1) return;
  if (msg.indexOf('!price') == 0) {
    var match = msg.match(/!price (director|audition) ([\.0-9]+)/);
    if (!match) steamFriends.sendMessage(id, 'Usage: !price <director or audtion> <price> (e.g. !price director 3.77)');
    else {
      steamFriends.sendMessage(id, 'Successfully set price of '+match[1] + ' reels to '+match[2] + '!');
      PRICES[match[1] == 'audition' ? 0 : 1] = parseFloat(match[2]);
      changePrices();
    }
  }
  else if(msg.indexOf('!changestate') == 0) {
    var match = msg.match(/!changestate (Online|Offline|Away|Busy|LookingToTrade|LookingToPlay|Snooze)/);
    LOG(match);
    LOG(match[1]);
    if (!match)
        steamFriends.sendMessage(id, '!changestate <state> (Online, Offline, Away, LookingToTrade, Busy, LookingToPlay, Snooze)');
    var state = match[1]
    if (match)
        steamFriends.setPersonaState(Steam.EPersonaState.state)
  }
});

So the top part works, but the bottom part does not. Bottom part is the else if statement which should be triggered by the keyword !changestate The top part (the part that works) is the part that is triggered by !price.

EXAMPLE: When I try to do !changestate Away, which should be executed as steamFriends.setPersonaState(Steam.EpersonaState.Away) the code does not return an error, it logs "!changestate Away, Away" and on the next line, just "Away". I'm not sure if this is a logic error or something else in my code that is going wrong :/

I've had it log the match and match[1] for debugging, but nothing seems to help.

Thanks for the help guys, I really appreciate it.


Solution

  • At the end of your code you have

    steamFriends.setPersonaState(Steam.EPersonaState.state)
    

    Your 'state' variable isn't being used here. I suspect you would have more luck with something like

    switch(state.toLowerCase()) {
      case 'away':
      steamFriends.setPersonaState(Steam.EPersonaState.Away);
      break;
    
    case 'online':
      steamFriends.setPersonaState(Steam.EPersonaState.Online);
      break;
    
    // etc...
    }