Skip to content

Instantly share code, notes, and snippets.

@zfbx
Last active October 16, 2017 10:23
Show Gist options
  • Save zfbx/64ffad2203f3aea8e63e0929989cd975 to your computer and use it in GitHub Desktop.
Save zfbx/64ffad2203f3aea8e63e0929989cd975 to your computer and use it in GitHub Desktop.
simple nodejs module for handling basic steam profile information for my discord bot.
const got = require('got');
const parseString = require('xml2js').parseString;
/**
* @typedef {Object} profile
* @property {string} search - The string used to search
* @property {string} id - User's SteamID64
* @property {string} url - User search url
* @property {string} privacy - User's Privacy settings ["public", "private", "friendsonly"]
* @property {string} picturesmall - Small profile picture
* @property {string} picturemedium - Medium profile picture
* @property {string} picturelarge - Large profile picture
* @property {string} name - Visible username
* @property {number} color - color from online state
* @property {string} error - Error was triggered
*
* @property {string} onlinestate - in-game, offline, online
* @property {string} state - Current State [Last online: time, Online, in-game]
* @property {string} visibility - 1 = visible to me (private, friends only) 3 = public
* @property {boolean} vacban - Has a VAC ban on record
* @property {string} tradeban - Is banned from trading
* @property {boolean} limited - having purchased a game worth over $5
* @property {string} customurl - The custom url if set
* @property {string} joined - Date they joined Steam
* @property {string} hours2weeks - Hours played in the last 2 weeks
* @property {string} location - Real world location if set
* @property {boolean} playing - Currently playing a game?
* @property {string} currentgame - Name of current game
* @property {string} currentgamelink - Link to current game
* @property {string} customgamemsg - game link and title in markdown
* @property {string} games - how many games owned
*/
/**
* @param {string} search - The user profile to get the data of.
* @param {string} apikey - Steam API key
* @param {boolean} [debug=false] - optional console output for checking errors
* @return {profile} - Output of the profile Object
*/
module.exports = function (search, apikey, debug = false) {
return new Promise(function (fulfill, reject) {
let key;
if (apikey) {
if (debug)
console.log("API key set.");
key = apikey;
} else {
if (debug)
console.log("No API key provided.");
}
let profile = {};
if(!search) {
if (debug)
console.error('No username provided.');
return fulfill("No username provided.");
}
profile.search = search.replace(/[^A-Za-z0-9_-]/g,'');
if(profile.search === ""){
if (debug)
console.error('Invalid username.');
return fulfill("Invalid username.");
}
let url;
if (search.length < 16 || !/^[0-9]+$/.test(search)) {
url = `http://steamcommunity.com/id/${search}/?xml=1`;
profile.url = `http://steamcommunity.com/id/${search}/`;
} else {
url = `http://steamcommunity.com/profiles/${search}/?xml=1`;
profile.url = `http://steamcommunity.com/profiles/${search}/`;
}
got(url)
.then(res => {
parseString(res.body, function(err, result) {
if (!result.profile) {
if (debug)
console.log("This doesn't seem to be a real Steam Profile..");
return fulfill("This doesn't seem to be a real Steam Profile..");
}
profile.id = result.profile.steamID64.toString();
profile.privacy = result.profile.privacyState.toString();
profile.picturesmall = result.profile.avatarIcon.toString();
profile.picturemedium = result.profile.avatarMedium.toString();
profile.picturelarge = result.profile.avatarFull.toString();
profile.name = result.profile.steamID.toString();
if (profile.privacy === "friendsonly") {
profile.error = "This profile is set to friends only..";
if (debug) {
console.log(profile);
}
profile.color = 0x7a7a7a; //gr(e/a)y
return fulfill(profile);
}
if (profile.privacy === "private") {
profile.error = "This profile is private.";
if (debug) {
console.log("This profile is private.");
console.log(profile);
}
profile.color = 0x7a7a7a; //gr(e/a)y
return fulfill(profile);
}
profile.onlinestate = result.profile.onlineState.toString();
let s = profile.onlinestate;
if(s === "online") {
profile.color = 0x53a4c4; //blue
} else if (s === "in-game") {
profile.color = 0x8fb93b; //green
} else {
profile.color = 0x7a7a7a; //gr(e/a)y
}
profile.state = result.profile.stateMessage.toString();
profile.visibility = result.profile.visibilityState.toString();
profile.vacban = result.profile.vacBanned.toString() == true;
profile.tradeban = result.profile.tradeBanState.toString();
profile.limited = result.profile.isLimitedAccount.toString() == true;
profile.customurl = result.profile.customURL.toString();
profile.joined = result.profile.memberSince.toString();
profile.hours2weeks = result.profile.hoursPlayed2Wk.toString();
profile.location = result.profile.location.toString();
if (result.profile.inGameInfo) {
profile.playing = true;
profile.currentgame = result.profile.inGameInfo.gameName.toString();
profile.currentgamelink = result.profile.inGameInfo.gameLink.toString();
profile.customgamemsg = `Playing [${profile.currentgame}](${profile.currentgamelink})`;
} else {
profile.playing = false;
profile.customgamemsg = `Not playing any games`;
}
let gameurl = `http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=${key}&steamid=${profile.id}&format=json`;
if (key) {
got(gameurl)
.then(res2 => {
let gamesres = JSON.parse(res2.body)
if (!gamesres.response) {
profile.games = "unknown";
if (debug) {
console.log("Game query seems to have failed, bad apikey?");
console.log(profile);
}
return fulfill(profile);
} else {
profile.games = gamesres.response.game_count.toString();
if (debug) {
console.log(profile);
}
return fulfill(profile);
}
});
} else {
profile.games = "unknown";
if (debug) {
console.log("game count is set to unknown from lack of API key.");
console.log(profile);
}
return fulfill(profile);
}
});
}).catch(error => {
console.error(error)
return fulfill("Error grabbing profile, not sure why");
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment