Jump to content


Photo
Question

Chat messages with some specific input (multiple lines)

node.js node-steam-user

Best Answer SnaBe , 16 July 2019 - 02:42 PM

Yes, it's possible to have "inputs" in a message or command. 

 

To do so, you'll have to learn about regex.

 

Here's an example:

//Act on chat messages & commands
client.on('friendMessage', (steamID, message) => {
  //Command place holder
  var cmd;
  //Does the message match our buy command 
  if(cmd = message.match(/^!buy (\d+) (\D+)/i)) {
    //The amount to buy (input string to number)
    var amount = Number(cmd[1]);
    //The item to buy (input string)
    var item = cmd[2];
    //Log the regex values
    console.log(`User ${steamID} would like to purchase ${amount} of ${item}`);
    //Call your function that takes these two values as arguments.
    sellItem(steamID, amount, item);
  }
});

In the example above we're looking for a buy command match. The string has to start with !buy, contain a number & a set of characters. For numbers only we use 'd' & for characters we use 'D'. The number is the amount of an item to buy, while the charaters is the item's name. We can access these values by using the following notation cmd[1]. Here 1 is the first regex match & cmd[2] is the second one.

 

The (), \ & + all have different uses, escpeially when it comes to matching something from a string using regex. You can read more about those used in the example above here.

 

I hope this helps! 

Go to the full post


  • Please log in to reply
2 replies to this topic

#1 Midmines

Midmines

    Newbie

  • Member
  • Pip
  • 5 posts

Posted 15 July 2019 - 08:58 AM

I am new in node js, i start 2 days ago.
Is that possible to create multiple specific input message?

steam chat
a: hello
bot: !mymoney, !checkname [gameid]
a: !checkname 440
bot: Team Fortress 2
a: !checkname 570
bot: Dota 2
node.js code
.....
client.on("friendMessage", function(steamID, message) {
  if (message == "!mymoney") {
    client.chatMessage(steamID, duitgw)
  }
  else if (message == "!checkname"){
    if (message.line2 == "440"){ //like this
      .....
    }
    else if (message.line2 == notnumber){ //another condition
      ....
    }
    else if (message.line2 == isnt set){ //another condition
      ....
    }
  }
  else{
    client.chatMessage(steamID, "!mymoney, !checkgame [gameid]")
  }
});
.....

edit: i use comment for highlight


Edited by Midmines, 15 July 2019 - 06:17 PM.


#2 SnaBe

SnaBe

    Advanced Member

  • Member
  • PipPipPip
  • 33 posts
  • LocationDenmark

Posted 16 July 2019 - 02:42 PM   Best Answer

Yes, it's possible to have "inputs" in a message or command. 

 

To do so, you'll have to learn about regex.

 

Here's an example:

//Act on chat messages & commands
client.on('friendMessage', (steamID, message) => {
  //Command place holder
  var cmd;
  //Does the message match our buy command 
  if(cmd = message.match(/^!buy (\d+) (\D+)/i)) {
    //The amount to buy (input string to number)
    var amount = Number(cmd[1]);
    //The item to buy (input string)
    var item = cmd[2];
    //Log the regex values
    console.log(`User ${steamID} would like to purchase ${amount} of ${item}`);
    //Call your function that takes these two values as arguments.
    sellItem(steamID, amount, item);
  }
});

In the example above we're looking for a buy command match. The string has to start with !buy, contain a number & a set of characters. For numbers only we use 'd' & for characters we use 'D'. The number is the amount of an item to buy, while the charaters is the item's name. We can access these values by using the following notation cmd[1]. Here 1 is the first regex match & cmd[2] is the second one.

 

The (), \ & + all have different uses, escpeially when it comes to matching something from a string using regex. You can read more about those used in the example above here.

 

I hope this helps! 


Edited by SnaBe, 16 July 2019 - 02:44 PM.

  • Midmines likes this

"Truth can only be found in one place: the code." - Robert C. Martin


#3 Midmines

Midmines

    Newbie

  • Member
  • Pip
  • 5 posts

Posted 17 July 2019 - 04:25 AM

Yes, it's possible to have "inputs" in a message or command. 

 

To do so, you'll have to learn about regex.

 

Here's an example:

//Act on chat messages & commands
client.on('friendMessage', (steamID, message) => {
  //Command place holder
  var cmd;
  //Does the message match our buy command 
  if(cmd = message.match(/^!buy (\d+) (\D+)/i)) {
    //The amount to buy (input string to number)
    var amount = Number(cmd[1]);
    //The item to buy (input string)
    var item = cmd[2];
    //Log the regex values
    console.log(`User ${steamID} would like to purchase ${amount} of ${item}`);
    //Call your function that takes these two values as arguments.
    sellItem(steamID, amount, item);
  }
});

In the example above we're looking for a buy command match. The string has to start with !buy, contain a number & a set of characters. For numbers only we use 'd' & for characters we use 'D'. The number is the amount of an item to buy, while the charaters is the item's name. We can access these values by using the following notation cmd[1]. Here 1 is the first regex match & cmd[2] is the second one.

 

The (), \ & + all have different uses, escpeially when it comes to matching something from a string using regex. You can read more about those used in the example above here.

 

I hope this helps! 

Big thanks to you master







Also tagged with one or more of these keywords: Question, node.js, node-steam-user

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users