From d910b116c6cf2f2c330354b56eab4925864e215e Mon Sep 17 00:00:00 2001 From: reality Date: Mon, 28 Oct 2013 13:43:35 +0000 Subject: [PATCH] basic steam module --- modules/profile/config.json | 1 + modules/steam/config.json | 3 + modules/steam/steam.js | 124 ++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 modules/steam/config.json create mode 100644 modules/steam/steam.js diff --git a/modules/profile/config.json b/modules/profile/config.json index 75c5d22..0315ee7 100644 --- a/modules/profile/config.json +++ b/modules/profile/config.json @@ -9,6 +9,7 @@ "avatar": null, "bio": null, "lastfm": null, + "steam": null, "favourites": { "colour": null } diff --git a/modules/steam/config.json b/modules/steam/config.json new file mode 100644 index 0000000..60e33b8 --- /dev/null +++ b/modules/steam/config.json @@ -0,0 +1,3 @@ +{ + "api_key": "bleh" +} diff --git a/modules/steam/steam.js b/modules/steam/steam.js new file mode 100644 index 0000000..f32cb2c --- /dev/null +++ b/modules/steam/steam.js @@ -0,0 +1,124 @@ +/** + * Module Name: Steam + * Description: Various steam functionality. + */ + +var _ = require('underscore')._, + request = require('request'); + +var steam = function(dbot) { + this.ApiRoot = 'http://api.steampowered.com/'; + + this.internalAPI = { + 'getSteam': function(server, nick, callback) { + dbot.api.profile.getProfile(server, nick, function(err, user, profile) { + if(user) { + if(profile && _.has(profile.profile, 'steam')) { + callback(user, profile.profile.steam); + } else { + callback(user, null); + } + } else { + callback(null, null); + } + }); + } + }; + + this.api = { + // TODO: Cache this shit yo + 'getSteamID': function(nick, callback) { + request.get(this.ApiRoot + 'ISteamUser/ResolveVanityURL/v0001/', { + 'qs': { + 'key': this.config.api_key, + 'vanityurl': nick, + 'format': 'json' + }, + 'json': true + }, function(err, res, body) { + if(body.response.success == 1) { + callback(null, body.response.steamid); + } else if(body.response.success == 42) { + callback('no_user', body.response.steamid); + } + }); + }, + + 'getRecentlyPlayed': function(steamid, callback) { + request.get(this.ApiRoot + 'IPlayerService/GetRecentlyPlayedGames/v0001/', { + 'qs': { + 'key': this.config.api_key, + 'steamid': steamid, + 'format': 'json' + }, + 'json': true + }, function(err, res, body) { + if(_.has(body, 'response') && _.has(body.response, 'total_count')) { + callback(null, body.response); + } else { + callback(true, null); + } + }); + } + }; + + this.commands = { + '~game': function(event) { + var user = event.rUser, + snick = event.rProfile.steam; + if(event.res[0]) { + user = event.res[0].user; + snick = event.res[0].snick; + } + + this.api.getSteamID(snick, function(err, steamid) { + this.api.getRecentlyPlayed(steamid, function(err, games) { + if(!err) { + if(games.total_count != 0) { + event.reply(user.currentNick + ' last played ' + games.games[0].name); + } else { + event.reply(user.currentNick + ' didn\'t play anything recently'); + } + } else { + event.reply('something went wrong'); + } + }); + }.bind(this)); + } + }; + + _.each(this.commands, function(command) { + command.resolver = function(event, callback) { + if(event.rProfile && _.has(event.rProfile, 'steam')) { + if(event.params[1]) { + this.internalAPI.getSteam(event.server, event.params[1], function(user, snick) { + if(user && snick) { + event.res.push({ + 'user': user, + 'snick': snick + }); + callback(false); + } else { + if(!user) { + event.reply('Unknown user.'); + } else { + event.reply(user.currentNick + ': Set a steam username with "~set steam username"'); + } + callback(true); + } + }); + } else { + callback(false); + } + } else { + event.reply(event.user + ': Set a steam username with "~set steam username"'); + callback(true); + } + }.bind(this); + }, this); + +}; + +exports.fetch = function(dbot) { + return new steam(dbot); +};