Skip to content

Instantly share code, notes, and snippets.

@clone1018
Created October 9, 2013 01:34
Show Gist options
  • Save clone1018/6894765 to your computer and use it in GitHub Desktop.
Save clone1018/6894765 to your computer and use it in GitHub Desktop.
Tip a user on irc!
var needle = require('needle');
var Plugin = (function () {
function Plugin(bot) {
this.commands = {};
this.users = {};
this.bot = bot;
this.commands = {
'gittip': 'onCommandGittip'
};
this.users = {};
}
Plugin.prototype.onCommandGittip = function (from, to, message, args) {
if (args.length < 2) {
this.bot.reply(from, to, '.plugin <action> [namespace]', 'notice');
return;
}
var plugin = this;
switch (args[1]) {
case "add-key":
this.bot.client.whois(from, (function (whos) {
if (whos.account !== undefined) {
this.users[from] = { key: args[2] };
}
}).bind(this));
break;
case "tip":
this.bot.client.whois(from, (function (whos) {
if (whos.account !== undefined) {
this.changeTip(from, this.users[from]['key'], args[2], args[3], function tipResponse(err, body) {
if (err) {
plugin.bot.reply(from, to, 'There was a problem changing users tip.', 'notice');
}
plugin.bot.reply(from, to, 'Tip for ' + args[2] + ' changed to $' + args[3], 'notice');
});
}
}).bind(this));
break;
default:
plugin.bot.reply(from, to, 'Action not found, did you mean: add-key, tip', 'notice');
}
};
Plugin.prototype.changeTip = function (from, fromKey, to, amount, cb) {
var url = 'https://www.gittip.com/' + from + '/tips.json';
var data = [
{
username: to,
platform: 'gittip',
amount: amount
}
];
var ned = needle.request('POST', url, data, {
json: true,
headers: {
"Authorization": "Basic " + new Buffer(fromKey + ':').toString('base64'),
"Content-Type": "application/json"
}
}, function (err, res, body) {
cb(err, body);
});
};
return Plugin;
})();
exports.Plugin = Plugin;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment