Jump to content
McKay Development

Question on when user sends trade offer


Toad

Recommended Posts

Thanks the response.

 

When you say trade offer BBcode, can you elaborate?

I currently have switch statements that listen for specific messages, so idk if instead listening to trade offers and sending a message that way is better? Eh, just really confused on the documentation.

Link to comment
Share on other sites

Have you read the docs on the new chat?

You get a friendMessage event when a new message comes in, which contains an Incoming Friend Message object. For an incoming trade offer, the message is something like: [tradeoffer sender=46143802 id=4087685841][/tradeoffer]

But you can see the parsed BBCode in a more convenient format with message_bbcode_parsed, which looks like:

[
  TagNode {
    tag: 'tradeoffer',
    attrs: { sender: '46143802', id: '4087685841' },
    content: []
  }
]

 

Link to comment
Share on other sites

Thanks again for the response.

For reference, I am using node. I looked at what you linked...so basically, I need to grab hold of the sender id, check if it's a 'tradeoffer', and respond back? If so, I'm not sure how to do that.

 

Friendmessage.bbc_code_parsed, then in that, somehow access if it has the tradeoffer tag?

Edited by Toad
Link to comment
Share on other sites

1 hour ago, Toad said:

Thanks again for the response.

For reference, I am using node. I looked at what you linked...so basically, I need to grab hold of the sender id, check if it's a 'tradeoffer', and respond back? If so, I'm not sure how to do that.

 

Friendmessage.bbc_code_parsed, then in that, somehow access if it has the tradeoffer tag?

Why not use a tradeoffer event listener? I think that would be a much easier route. You can get the sender id, the contents of the trade, the message, a lot more. If by respond back you meant send a message back, you could get the steamid using the partner id and just send the user a message. 

Link to comment
Share on other sites

8 hours ago, What Comes Around said:

Why not use a tradeoffer event listener? I think that would be a much easier route. You can get the sender id, the contents of the trade, the message, a lot more. If by respond back you meant send a message back, you could get the steamid using the partner id and just send the user a message. 

Are you referring to 'tradeOffers' event? I'm not sure how to utilize it as you describe. I can subscribe or listen for the event but it still only gives me an integer 'count' I thought?

 

Off topic question but why don't if/else statements operate like they usually do?

client.on('friendMessage', function(steamid, message){
  if (message == '!Hello){
    client.chatMessage(steamid, 'Hi there friend!');
  }
  else {
    client.chatMessage(steamid, 'Adios amigo!');
  }

}

With the code above, when someone sends me '!Hello', it sends them:

Hi there friend!

Adios amigo!

 

 

 

Edited by Toad
Link to comment
Share on other sites

That code is full of syntax errors, so I suspect you didn't copy and paste the code that's actually running.

10 hours ago, Toad said:

Thanks again for the response.

For reference, I am using node. I looked at what you linked...so basically, I need to grab hold of the sender id, check if it's a 'tradeoffer', and respond back? If so, I'm not sure how to do that.

 

Friendmessage.bbc_code_parsed, then in that, somehow access if it has the tradeoffer tag?

client.chat.on('friendMessage', (msg) => {
	if (!msg.message_bbcode_parsed) {
		return;
	}
	
	let element = msg.message_bbcode_parsed[0];
	if (typeof element == 'object' && element.tag == 'tradeoffer') {
		client.chat.sendFriendMessage(msg.steamid_friend, `You sent me trade offer #${element.attrs.id}.`);
	}
});

 

Edited by Dr. McKay
Link to comment
Share on other sites

I'll have to give that a try when I get a minute, thank you.

Syntax errors? How so? It is running right now. 

 

const client = new SteamUser();

const logOnOptions = {
  accountName: 
  password: 
};

client.logOn(logOnOptions);

client.on('loggedOn', () => {
  console.log('Logged into Steam');

  client.setPersona(SteamUser.EPersonaState.Online);
});

client.on('friendMessage', function(steamid, message){
  if (message == '!Hello'){
    client.chatMessage(steamid, 'Hiya');
  }
  else {
    client.chatMessage(steamid, 'Adios!');
  }

}):

 

This should only send "Hiya" if someone sends me !Hello I thought? Can't figure out why it sends both messages when I get !Hello

Edited by Toad
Link to comment
Share on other sites

You were missing a single-quote after !Hello, and in both you're missing a closing paren.

I don't see any reason why that would send back both messages; you should probably console.log the message to see what it actually is; you might be receiving another message of some type in addition to your !Hello.

Link to comment
Share on other sites

Yeah I didn't copy and paste the code fully, there is a closed paren and semicolon at the very end. Just edited it.

If someone just sends me just !Hello, it sends both messages. Idk.. weird. 

Edited by Toad
Link to comment
Share on other sites

49 minutes ago, Toad said:

Yeah I didn't copy and paste the code fully, there is a closed paren and semicolon at the very end. Just edited it.

If someone just sends me just !Hello, it sends both messages. Idk.. weird. 

No idea why that's happening. I copy and pasted the code and everything seemed fine:

image.png.29c839f47c6f36d0885ecb07c9f6cb8c.png

also, I believe you could improve this by lower casing the message contents to make both the capitalized and non-capitalized versions of the same command work. This is what the code for that looks like:

  client.on('friendMessage'function(steamid, message){
    if (message.toLowerCase() == '!hello'){
      client.chatMessage(steamid, 'Hiya');
    } else {
      client.chatMessage(steamid, 'Adios!');
    }
  })

image.png.c2263efe5424df72aa42656d4242c3c8.png

As for why it's giving you the if and the else result on messages, I would check the rest of the code as if statements haven't ever and will never change. So it must be an issue elsewhere, maybe you call the if statement twice and one if statement is reversed? I mean I don't know but that's all I can do to explain it.

If you would like the full code that I used here it is:

const SteamUser = require('steam-user');
const SteamTotp = require('steam-totp');
const SteamCommunity = require('steamcommunity');
const TradeOfferManager = require('steam-tradeoffer-manager');
const SteamStore = require('steamstore');
const credentials = require('./credentials');
 
var client = new SteamUser();
var community = new SteamCommunity();
var manager= new TradeOfferManager();
var store = new SteamStore();
 
start();
 
function start() {
  var logindetails = {
      accountName: credentials.account.accountName,
      password: credentials.account.password,
  }
  client.logOn(logindetails);
  client.on('loggedOn', () => {
      console.log(`${logindetails.accountName} has successfully logged in`);
  })
  client.on('friendMessage'function(steamid, message){
    if (message.toLowerCase() == '!hello'){
      client.chatMessage(steamid, 'Hiya');
    } else {
      client.chatMessage(steamid, 'Adios!');
    }
  })
  client.on('webSession', (sid, cookies) => {
      manager.setCookies(cookies);
      community.setCookies(cookies);
      community.startConfirmationChecker(20000logindetails.identitySecret);
      community.clearPersonaNameHistory((err) => {
          if (err) {
              console.log(err);
          }
      });
  })
}

You can ignore some things like webSession as you probably won't be confirming trades and overall using community, manager, or store. Where I add my account details you could just replace that with your details. You can use this to compare it to your code to maybe find a mistake. But as far as I know this works completely fine. Also you don't need all the stuff in a function called start. That was just a copy and paste from older code I had so I didn't have to rewrite everything. Also I can send you some trade listening code if you like, but I'll need to go look for some good examples in my projects.

 

P.S: Remember to install all the npms if you don't want to change anything except the log in details. 

Edited by What Comes Around
Link to comment
Share on other sites

Putting more than two if statements causes the behavior I found.

 

client.on('friendMessage', function(steamid, message){
    if (message == '!hello'){
      client.chatMessage(steamid, 'Hiya');
    } 
    if (message == '!why'){
      client.chatMessage(steamid, 'Because you told me to');
    }
    if (message == '!lol'){
      client.chatMessage(steamid, 'You are funny');
    }
    else {
      client.chatMessage(steamid, 'Adios');
    }
});
 

 

Only command that works properly is !lol, all the others work but they also call the else statement. 

Edited by Toad
Link to comment
Share on other sites

21 minutes ago, Toad said:

Putting more than two if statements causes the behavior I found.

 

client.on('friendMessage', function(steamid, message){
    if (message == '!hello'){
      client.chatMessage(steamid, 'Hiya');
    } 
    if (message == '!why'){
      client.chatMessage(steamid, 'Because you told me to');
    }
    if (message == '!lol'){
      client.chatMessage(steamid, 'You are funny');
    }
    else {
      client.chatMessage(steamid, 'Adios');
    }
});
 

 

Only command that works properly is !lol, all the others work but they also call the else statement. 

Normally in this kind of situation it's much better to use a switch statement. Which would look like this:

switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

 

Here is some example code, that produces this result:

image.png.8e6bb8cb88c754faf0637a755cd9eacc.png

const SteamUser = require('steam-user');
const SteamTotp = require('steam-totp');
const SteamCommunity = require('steamcommunity');
const TradeOfferManager = require('steam-tradeoffer-manager');
const SteamStore = require('steamstore');
const credentials = require('./credentials');
 
var client = new SteamUser();
var community = new SteamCommunity();
var manager= new TradeOfferManager();
var store = new SteamStore();

 var logindetails = {
      accountName: credentials.account.accountName,
      password: credentials.account.password,
  }
  client.logOn(logindetails);
  client.on('loggedOn', () => {
      console.log(`${logindetails.accountName} has successfully logged in`);
  })
  client.on('friendMessage'function(steamid, message){
    switch (message.toLowerCase()) {
      case "!hello":
        client.chatMessage(steamid, 'Hiya');
        break;
      case "!why":
        client.chatMessage(steamid, 'Because you told me to');
        break;
      case "!lol":
        client.chatMessage(steamid, 'You are funny');
        break;
      default:
        client.chatMessage(steamid, 'Adios');
        break;
    }
  })
  client.on('friendRelationship', (steamid, relationship) => {
    if (relationship === 2) {
        client.addFriend(steamid);
        console.log("Person Added As Friend!");
    }
  });
  })
Edited by What Comes Around
Link to comment
Share on other sites

Thanks for all the advice, been working at it and learning. 

Curious about the getPersonas function.

Two of my attemps are:

  const myarray = [steamid];
  const response = client.getPersonas(myarray);
  console.log(response);
 
and 
 
client.getPersonas([steamid], function (personas) {
    var persona = personas[steamid.getSteamID64()];
    var name = persona ? persona.player_name : ("[" + steamid.getSteamID64() + "]");
    console.log(name)
  });
 
First one says something about a promise callback waiting, the second one throws 'Cannot read property '76561199066343914' of null'.
Link to comment
Share on other sites

3 hours ago, Toad said:

Thanks for all the advice, been working at it and learning. 

Curious about the getPersonas function.

Two of my attemps are:

  const myarray = [steamid];
  const response = client.getPersonas(myarray);
  console.log(response);
 
and 
 
client.getPersonas([steamid], function (personas) {
    var persona = personas[steamid.getSteamID64()];
    var name = persona ? persona.player_name : ("[" + steamid.getSteamID64() + "]");
    console.log(name)
  });
 
First one says something about a promise callback waiting, the second one throws 'Cannot read property '76561199066343914' of null'.

So I'll start with the second function. Firstly not sure why you have square brackets around steamid. What you need to do is make an array. You can do this like so:

var steamidstolookup = [040230453045, 0342034023402(fake ids but u get the point)] <--- this can also be strings that can be parsed into SteamID objects as stated in the docs.

As for why you are getting the error it's because you are trying to look for an object that doesn't exist in the first parameter of the callback. Here's an example of how you should do it:

client.getPersonas(steamidstolookup, (err, personas) => {
    if (err) {
        console.log(err)
    } else {
        console.log(personas)
    }
})
 
//This uses an arrow function, it's very similar to a normal function (It just can't use "this", arguments and some other things, tbh I don't really know all the differences because I haven't really needed to care lmao, but maybe one day I will)

So as you can see the first callback param is err, when it successfully gets a response from steam, the function returns err = null. That's why u were getting an error of cannot read property ... of null. Not sure if you have grown a fear of if statements since the last posts but really they're great to use. Like in this case you wouldn't need to validate personas because if err is not null (there is an error) it won't run the code in else.

Edited by What Comes Around
Link to comment
Share on other sites

7 hours ago, What Comes Around said:

Firstly not sure why you have square brackets around steamid. What you need to do is make an array. You can do this like so:

var steamidstolookup = [040230453045, 0342034023402(fake ids but u get the point)] <--- this can also be strings that can be parsed into SteamID objects as stated in the docs.

Assuming steamid is a variable holding a valid SteamID, then [steamid] is correct as that's creating an array with one element.

It's also important to note that you cannot pass a SteamID like var steamidstolookup = [76561198006409530];

Numbers in JavaScript are double-precision floating-point numbers, which have a maximum integer precision up to 2^53. A 64-bit SteamID is greater than this, so trying to pass it as a number and not a string will cause it to become some other number entirely. For example:

image.png

Link to comment
Share on other sites

On 6/14/2020 at 4:46 AM, Dr. McKay said:

Assuming steamid is a variable holding a valid SteamID, then [steamid] is correct as that's creating an array with one element.

It's also important to note that you cannot pass a SteamID like var steamidstolookup = [76561198006409530];

Numbers in JavaScript are double-precision floating-point numbers, which have a maximum integer precision up to 2^53. A 64-bit SteamID is greater than this, so trying to pass it as a number and not a string will cause it to become some other number entirely. For example:

image.png

Right, sucks I made that mistake again. Since the last time I did, a bunch of users had their accounts reset on a database. Whoops.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...