Jump to content
McKay Development

How to go from Steam64 to Steam username (the name currently used on steam)


Frost Byte

Recommended Posts

Hello, I'm really new to node.js and everything that comes with it and for now I'm really enjoying it!

However, I've run into a problem, which has more to do with cosmetics than anything else.

friends.on("friendMsg", function(steamID, msg, type) {
	var steamName = client.getAliases(steamID, function(err, results) {
			if (err) {
				console.log(err);
			} else {
				console.log(results);
			}
		});
	if (type == Steam.EChatEntryType.ChatMsg) {
		console.log("Friend message from " + steamName + ": " + msg);
		if (msg == "Ping") {
			friends.sendMessage(steamID, "Pong");
			console.log("Send back: Pong");
		} else {
			friends.sendMessage(steamID, config.greetMsg);
			console.log("Send back the standard reply");
		}
	}
});

When someone sends a message to the bot, it returns a standard greeting. Well that works great. Even the fact that when a user types: "Ping" it returns "Pong".

 

The problem is the fact that I want to see this in the console: "Friend message from " + steamName + ": " + msg

Right now, I've got it working that the steam name is shown as the steam64 ID (not in this code), but for aesthetics, I'd like to see the current username of the person the bot is chatting with on Steam.

 

It seems like this can be achieved with .getAliases or .getPersona, but I can't seem to get it to work.

Could someone help me out with this or is it just not possible?

 

Thanks a lot!

Link to comment
Share on other sites

Yes, of course!

 

I've been trying with .getPersonas for a bit, but it unfortunately always give me an error...

friends.on("friendMsg", function(steamID, msg, type) {
	var steamName = client.getPersonas(steamID);
	
	if (type == Steam.EChatEntryType.ChatMsg) {
		console.log("Friend message from " + steamName + ": " + msg);
		if (msg == "Ping") {
			friends.sendMessage(steamID, "Pong");
			console.log("Send back: Pong");
		} else {
			friends.sendMessage(steamID, config.greetMsg);
			console.log("Send back the standard reply");
		}
	}
});

The error it gives back is:

D:\Programs\SteamBot\Node.js\node_modules\steam-user\components\friends.js:119

        var ids = steamids.map(function(id) {
                           ^
 
TypeError: steamids.map is not a function
 
I probably made a mistake somewhere, but well that happens being new to node.js
 
If it works, it should give me an user event back, but I'm not sure what I can do with that. Basically the only thing I want is to get the current Steam name, so I hope that is possible.
Furthermore, I'm not really great with callbacks and here it was optional, but if I did include a callback, would it then look like this?
 
friends.on("friendMsg", function(steamID, msg, type) {
	var steamName = client.getPersonas(steamID, function(personas) {
			if (personas) /*<- This is not really a comparison which returns a boolean*/ {
				console.log(personas);
			}
		});
	if (type == Steam.EChatEntryType.ChatMsg) {
		console.log("Friend message from " + steamName + ": " + msg);
		if (msg == "Ping") {
			friends.sendMessage(steamID, "Pong");
			console.log("Send back: Pong");
		} else {
			friends.sendMessage(steamID, config.greetMsg);
			console.log("Send back the standard reply");
		}
	}
});

Thanks for your help in advance! I really appreciate it!

Edited by Frost Byte
Link to comment
Share on other sites

Your problem is that getPersonas expects an array as the first argument, and you aren't passing it an array. What you want to do is client.getPersonas([steamID], function(personas) { /* etc */ });

 

Also in this case, the callback is essentially mandatory for you. getPersonas doesn't return anything. The data is only available inside the callback. In your case, you want something like this:

client.getPersonas([steamID], function(personas) {
    var persona = personas[steamID.getSteamID64()];
    var name = persona ? persona.player_name : ("[" + steamID.getSteamID64() + "]");
    // the player's name is now available as name
});
        
Link to comment
Share on other sites

Thank you very much for your answer! I implemented it in my code and it works like a charm.

 

I do however wish to understand everything regarding your node.js library, so therefore I'd like to know if I analysed your code alright.

client.getPersonas([steamID], function(personas) {
    var persona = personas[steamID.getSteamID64()]; 
    var name = persona ? persona.player_name : ("[" + steamID.getSteamID64() + "]");
    // the player's name is now available as name
});

1. You added [] around steamID to make it an array so the getPersonas method works.

2. You created a persona object whose values are objects identical to those received in the user event. (I don't exactly know what that means... Is it like: You create a persona object and it's properties are objects?)

3. You created a name variable in which the name of the steamuser is put.

But why is there a '?' and where does the player_name come from? Also why is there a ':' ?

 

I just want to learn stuff, so it would be appreciated if you could explain, but if you're busy, I'll just live with it.

 

 Thanks for helping me out in the first place though! It works really well  :)

Link to comment
Share on other sites

For the people who would like to know how I implemented this.

friends.on("friendMsg", function(steamID, msg, type) {
	client.getPersonas([steamID], function(personas) {
		var persona = personas[steamID];
		var name = persona ? persona.player_name : ("[" + steamID + "]");
	
		if (type == Steam.EChatEntryType.ChatMsg) {
			console.log("Friend message from " + name + ": " + msg);
			if (msg == "Ping") {
				friends.sendMessage(steamID, "Pong");
				console.log("Send back: Pong");
			} else {
				friends.sendMessage(steamID, config.greetMsg);
				console.log("Send back the standard reply");
			}
		}
	});
});

I did not use .getSteamID64, because it gave me an error. I don't really know to which library that method belongs, so I left it out, since steamID was already in the 64-format.

Link to comment
Share on other sites

Okay, well getSteamID64 didn't work because it appears that you're using node-steam's friends handler alongside SteamUser, which is unnecessary. All functionality from node-steam is available in SteamUser directly.

 

The getPersonas method requests data from Steam and calls your provided callback with the personas object once the data is received. You're correct, the properties of personas are objects. In this case, since you requested data for only one user, there is only one property. In cases where you request data for multiple users, there will be more properties defined.

 

Each sub-object contains the data we received from Steam, which is where player_name comes from.

 

The ? and : are the ternary operator, which is more or less an "inline if" statement. In this case, it's checking whether the persona data exists in order to determine whether to pull the player_name from the data or to fallback to a default value. In this case, it was unnecessary, I just didn't feel like checking whether it was possible for getPersonas to not return data for the requested user(s) (it's not).

Link to comment
Share on other sites

Hey! Thanks for explaining. I'm slowly but surely getting the hang of it.

 

I'm indeed using node-steam's friends handler alongside SteamUser. The reason for this is that in FirePowered's article, they address the friends handler with 'client.friends' In the comments of that article, you stated that you removed this feature in v2.0.0, but I did not find another way to access it again.

 

Therefore I currently use:

var Steam = require('steam');
var SteamUser = require('steam-user');
var SteamCommunity = require('steamcommunity');

var client = new SteamUser();
var steamClient = new Steam.SteamClient();
var steamUser = new Steam.SteamUser(steamClient);
var community = new SteamCommunity(steamClient);
var friends = new Steam.SteamFriends(client.client);

I know, it is kind of a mess, but it works! However, I know that the 'community' and 'friends' handlers are also present in your node-steam-user library. But how do I access them? 'client.friends' or 'client.community' doesn't work anymore.

 

Furthermore, I initiated steamUser = new Steam.SteamUser(steamClient), but I only use it for the following:

// Decline friend requests when we get back online
friends.on("relationships", function(){
    for (steamID in friends.friends) {
        if (friends.friends[steamID] === SteamUser.Steam.EFriendRelationship.RequestRecipient) {
            friends.removeFriend(steamID);
			console.log("Friend request while offline from: " + steamID);
        }
    }
});

// Decline friend requests when we are online
friends.on("friend", function (steamID, relationship) {
    if (relationship == SteamUser.Steam.EFriendRelationship.RequestRecipient) {
        friends.removeFriend(steamID);
		console.log("Friend added");
    }
});

How can I make sure to only use the EFriendRelationship from your node-steam-user library?

 

These questions are really basic, but your help would as always be really appreciated, because then I can take seishun's node-steam library out of the equation and just continue my project with your steam-user library.

Link to comment
Share on other sites

I found out myself!

 

You already have the properties and methods from the steam handlers in your own steam-user library. So there I don't need a specific handler like 'friends' to send a message. I can just ask your steam-user object (in my case I called it client) and ask for the chatMessage method.

 

Right? Well it works for me now  ;)

 

Yeah, it really works. And I managed to only use your node-steam-user library with all it's functions + your node-steamcommunity library!

Edited by Frost Byte
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...