dbot/modules/steam/steam.js

237 lines
9.3 KiB
JavaScript
Raw Normal View History

2013-10-28 14:43:35 +01:00
/**
* 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);
}
});
2013-10-28 15:38:35 +01:00
},
'getProfile': function(steamid, callback) {
request.get(this.ApiRoot + 'ISteamUser/GetPlayerSummaries/v0002/', {
'qs': {
'key': this.config.api_key,
'steamids': steamid,
'format': 'json'
},
'json': true
}, function(err, res, body) {
2013-10-28 15:53:36 +01:00
if(_.has(body, 'response') && _.has(body.response, 'players') &&
body.response.players != null) {
2013-10-28 15:38:35 +01:00
callback(null, body.response.players[0]);
} else {
callback(true, null);
}
});
2013-10-28 17:11:16 +01:00
},
'getLibrary': function(steamid, callback) {
request.get(this.ApiRoot + 'IPlayerService/GetOwnedGames/v0001/', {
'qs': {
'key': this.config.api_key,
'steamid': steamid,
'format': 'json',
'include_appinfo': 1
},
'json': true
}, function(err, res, body) {
if(_.has(body, 'response') && _.has(body.response, 'game_count')) {
callback(null, body.response);
} else {
callback(true, null);
}
});
2013-10-28 14:43:35 +01:00
}
};
this.commands = {
2013-10-28 17:11:16 +01:00
'~games': function(event) {
var u1 = event.rUser,
s1 = event.rProfile.sid,
u2 = event.res[0].user,
s2 = event.res[0].sid;
this.api.getLibrary(s1, function(err, l1) {
if(!err) {
this.api.getLibrary(s2, function(err, l2) {
if(!err) {
var g1 = _.pluck(l1.games, 'name'),
g2 = _.pluck(l2.games, 'name'),
common = _.filter(g1, function(game) {
return _.include(g2, game);
}),
examples = [];
_.times(5, function() {
var choice = _.random(0, common.length - 1);
if(!_.include(examples, common[choice])) {
examples.push(common[choice]);
}
});
event.reply(dbot.t('steam_games', {
'u1': u1.currentNick,
'u2': u2.currentNick,
'count': common.length,
'examples': examples.join(', ')
}));
} else {
event.reply('Unable to fetch steam library :\'(');
}
});
} else {
event.reply('Unable to fetch steam library :\'(');
}
}.bind(this));
},
2013-11-04 20:29:51 +01:00
'~playing': function(event) {
2013-10-28 14:43:35 +01:00
var user = event.rUser,
2013-10-28 17:11:16 +01:00
snick = event.rProfile.steam,
sid = event.rProfile.sid;
2013-10-28 14:43:35 +01:00
if(event.res[0]) {
user = event.res[0].user;
snick = event.res[0].snick;
2013-10-28 17:11:16 +01:00
sid = event.res[0].sid;
2013-10-28 14:43:35 +01:00
}
2013-10-28 17:11:16 +01:00
this.api.getProfile(sid, function(err, player) {
if(!err && player) {
if(_.has(player, 'gameextrainfo')) {
var output = dbot.t('steam_currently_playing', {
'user': user.currentNick,
'game': player.gameextrainfo
});
if(_.has(player, 'gameserverip')) {
var host = player.gameserverip.split(':');
output += ' (Server: ' + host[0] + ' Port: ' + host[1] + ')';
2013-10-28 14:43:35 +01:00
}
2013-10-28 17:11:16 +01:00
event.reply(output);
2013-10-28 14:43:35 +01:00
} else {
2013-10-28 17:11:16 +01:00
this.api.getRecentlyPlayed(sid, function(err, games) {
if(!err) {
if(games.total_count != 0) {
event.reply(dbot.t('steam_last_played', {
'user': user.currentNick,
'game': games.games[0].name
}));
} else {
event.reply(dbot.t('steam_not_played', {
'user': user.currentNick
}));
}
} else {
event.reply('Unknown Steam Username');
}
});
2013-10-28 14:43:35 +01:00
}
2013-10-28 17:11:16 +01:00
}
2013-10-28 14:43:35 +01:00
}.bind(this));
}
};
2013-10-28 18:03:08 +01:00
this.commands['~games'].regex = [/^~games ([\d\w[\]{}^|\\`_-]+?)/, 2];
2013-10-28 14:43:35 +01:00
_.each(this.commands, function(command) {
command.resolver = function(event, callback) {
if(event.rProfile && _.has(event.rProfile, 'steam')) {
2013-10-28 17:11:16 +01:00
this.api.getSteamID(event.rProfile.steam, function(err, sid) {
if(!err) {
event.rProfile.sid = sid; // oh god ew
if(event.params[1]) {
this.internalAPI.getSteam(event.server, event.params[1], function(user, snick) {
if(user && snick) {
this.api.getSteamID(snick, function(err, sid) {
if(!err) {
event.res.push({
'user': user,
'snick': snick,
'sid': sid
});
callback(false);
} else {
event.reply('Unknown Steam ID');
callback(true);
}
});
} else {
if(!user) {
event.reply('Unknown user.');
} else {
event.reply(user.currentNick + ': Set a steam "vanity url" with "~set steam username"');
}
callback(true);
}
}.bind(this));
2013-10-28 14:43:35 +01:00
} else {
2013-10-28 17:11:16 +01:00
callback(false);
2013-10-28 14:43:35 +01:00
}
2013-10-28 17:11:16 +01:00
} else {
event.reply('Unknown Steam ID');
}
}.bind(this));
2013-10-28 14:43:35 +01:00
} else {
2013-10-28 14:58:58 +01:00
event.reply(event.user + ': Set a steam "vanity url" with "~set steam username"');
2013-10-28 14:43:35 +01:00
callback(true);
}
}.bind(this);
}, this);
};
exports.fetch = function(dbot) {
return new steam(dbot);
};