2016-07-14 23:34:37 +02:00
|
|
|
var lbry;
|
|
|
|
|
|
|
|
var command = '!tip';
|
|
|
|
|
|
|
|
module.exports={
|
|
|
|
command: command,
|
|
|
|
init: init,
|
|
|
|
respond: respond
|
|
|
|
};
|
|
|
|
|
|
|
|
function init(rpcuser, rpcpassword) {
|
|
|
|
if (lbry) {
|
|
|
|
throw new Error('init was already called once');
|
|
|
|
}
|
|
|
|
|
|
|
|
var bitcoin = require('bitcoin');
|
|
|
|
lbry = new bitcoin.Client({
|
|
|
|
host: 'localhost',
|
|
|
|
'port': 9245,
|
|
|
|
'user': rpcuser,
|
|
|
|
'pass': rpcpassword
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-18 16:07:41 +02:00
|
|
|
var globalSlackParams = {
|
|
|
|
icon_emoji: ':money_with_wings:'
|
|
|
|
};
|
|
|
|
|
2016-07-14 23:34:37 +02:00
|
|
|
function respond(bot, data) {
|
|
|
|
var tipper = data.user,
|
|
|
|
channel = data.channel,
|
2016-08-04 18:33:26 +02:00
|
|
|
words = data.text.trim().split(' ').filter( function(n){return n !== "";} );
|
2016-08-30 21:34:35 +02:00
|
|
|
|
2016-08-30 19:49:41 +02:00
|
|
|
if (words[0] !== command) {
|
|
|
|
// if the received message isn't starting with the trigger -> ignore
|
|
|
|
return;
|
|
|
|
}
|
2016-08-30 21:34:35 +02:00
|
|
|
|
2016-07-14 23:34:37 +02:00
|
|
|
if (!lbry) {
|
2016-07-18 16:07:41 +02:00
|
|
|
bot.postMessage(channel, 'Failed to connect to lbrycrd', {icon_emoji: ':exclamation:'});
|
2016-07-14 23:34:37 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var subcommand = words.length >= 2 ? words[1] : 'help';
|
2016-08-30 21:34:35 +02:00
|
|
|
|
2016-08-31 04:59:01 +02:00
|
|
|
var moveToBotSandbox = /*!tipper.is_admin && !tipper.is_owner && */channel !== 'C1TEEBS2Z' && ['help', 'balance', 'deposit', 'withdraw'].indexOf(subcommand) != -1 && !channel.startsWith("D")
|
2016-08-30 21:34:35 +02:00
|
|
|
if (moveToBotSandbox) {
|
2016-08-30 21:46:59 +02:00
|
|
|
bot.postMessage(channel, 'Please use <#C1TEEBS2Z|bot-sandbox> to talk to bots.', globalSlackParams);
|
2016-08-30 21:34:35 +02:00
|
|
|
return;
|
2016-08-30 21:16:19 +02:00
|
|
|
}
|
2016-07-14 23:34:37 +02:00
|
|
|
|
|
|
|
if (subcommand === 'help') {
|
|
|
|
doHelp(bot, channel);
|
|
|
|
}
|
|
|
|
else if (subcommand === 'balance') {
|
|
|
|
doBalance(bot, channel, tipper);
|
|
|
|
}
|
|
|
|
else if (subcommand === 'deposit') {
|
|
|
|
doDeposit(bot, channel, tipper);
|
|
|
|
}
|
|
|
|
else if (subcommand === 'withdraw') {
|
|
|
|
doWithdraw(bot, channel, tipper, words);
|
|
|
|
}
|
|
|
|
else {
|
2016-08-30 21:34:35 +02:00
|
|
|
doTip(bot, channel, tipper, words);
|
2016-07-14 23:34:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function doBalance(bot, channel, tipper) {
|
|
|
|
lbry.getBalance(tipper, 1, function(err, balance) {
|
|
|
|
if (err) {
|
2016-07-18 16:07:41 +02:00
|
|
|
bot.postMessage(channel, '<@' + tipper + '>: Error getting balance', {icon_emoji: ':exclamation:'});
|
2016-07-14 23:34:37 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-07-18 16:07:41 +02:00
|
|
|
bot.postMessage(channel, '<@' + tipper + '>: You have *' + balance + '* LBC', globalSlackParams);
|
2016-07-14 23:34:37 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function doDeposit(bot, channel, tipper) {
|
|
|
|
getAddress(tipper, function(err, address) {
|
|
|
|
if (err) {
|
2016-07-18 16:07:41 +02:00
|
|
|
bot.postMessage(channel, '<@' + tipper + '>: Error getting deposit address', {icon_emoji: ':exclamation:'});
|
2016-07-14 23:34:37 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-07-18 16:07:41 +02:00
|
|
|
bot.postMessage(channel, '<@' + tipper + '>: Your address is ' + address, globalSlackParams);
|
2016-07-14 23:34:37 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function doWithdraw(bot, channel, tipper, words) {
|
|
|
|
if (words.length < 4) {
|
|
|
|
doHelp(bot, channel);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var address = words[2],
|
2016-08-31 16:14:26 +02:00
|
|
|
amount = getValidatedAmount(words[3]);
|
2016-07-14 23:34:37 +02:00
|
|
|
|
2016-08-31 16:14:26 +02:00
|
|
|
if (amount === null) {
|
2016-07-18 16:07:41 +02:00
|
|
|
bot.postMessage(channel, '<@' + tipper + '>: I dont know how to withdraw that many credits', globalSlackParams);
|
2016-07-14 23:34:37 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lbry.sendFrom(tipper, address, amount, function(err, txId) {
|
|
|
|
if (err) {
|
|
|
|
bot.postMessage(channel, err.message);
|
|
|
|
}
|
|
|
|
else {
|
2016-07-18 16:07:41 +02:00
|
|
|
bot.postMessage(channel, '<@' + tipper + '>: You withdrew ' + amount + ' to ' + address + ' (' + txLink(txId) + ')', globalSlackParams);
|
2016-07-14 23:34:37 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-30 21:34:35 +02:00
|
|
|
function doTip(bot, channel, tipper, words) {
|
2016-07-14 23:34:37 +02:00
|
|
|
if (words.length < 3) {
|
2016-08-30 21:34:35 +02:00
|
|
|
doHelp(bot, channel);
|
2016-07-14 23:34:37 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var user = words[1],
|
2016-08-31 16:14:26 +02:00
|
|
|
amount = getValidatedAmount(words[2]);
|
2016-07-14 23:34:37 +02:00
|
|
|
|
2016-08-31 16:14:26 +02:00
|
|
|
if (amount === null) {
|
2016-07-18 16:07:41 +02:00
|
|
|
bot.postMessage(channel, '<@' + tipper + '>: I dont know how to tip that many credits', globalSlackParams);
|
2016-07-14 23:34:37 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user.match(/^<@U[^>]+>$/)) {
|
|
|
|
var id = user.substr(2,user.length-3);
|
|
|
|
sendLbc(bot, channel, tipper, id, amount);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bot.getUser(user).then(function(data) {
|
|
|
|
if (data.id) {
|
|
|
|
sendLbc(bot, channel, tipper, data.id, amount);
|
|
|
|
} else
|
|
|
|
{
|
2016-07-18 16:07:41 +02:00
|
|
|
bot.postMessage(channel, '<@' + tipper + '>: Sorry, I dont know that person', globalSlackParams);
|
2016-07-14 23:34:37 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function doHelp(bot, channel) {
|
|
|
|
bot.postMessage(channel,
|
2016-08-31 02:13:32 +02:00
|
|
|
'*USE <#C1TEEBS2Z|bot-sandbox> FOR EVERYTHING EXCEPT ACTUAL TIPPING*\n' +
|
2016-07-14 23:34:37 +02:00
|
|
|
'`' + command + ' help`: this message\n' +
|
|
|
|
'`' + command + ' balance`: get your balance\n' +
|
|
|
|
'`' + command + ' deposit`: get address for deposits\n' +
|
|
|
|
'`' + command + ' withdraw ADDRESS AMOUNT`: withdraw AMOUNT credits to ADDRESS\n' +
|
2016-07-18 15:59:21 +02:00
|
|
|
'`' + command + ' USER AMOUNT`: send AMOUNT credits to USER\n' +
|
|
|
|
'\n' +
|
2016-07-18 16:17:39 +02:00
|
|
|
'*Everyone will see what I say. Send me a Direct Message if you want to interact privately.*\n' +
|
|
|
|
'If I\'m not responding in some channel, you can invite me by @mentioning me.\n'
|
2016-07-18 16:07:41 +02:00
|
|
|
, globalSlackParams);
|
2016-07-14 23:34:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function sendLbc(bot, channel, tipper, id, amount) {
|
|
|
|
getAddress(id, function(err, address){
|
|
|
|
if (err){
|
|
|
|
bot.postMessage(channel, err.message);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lbry.sendFrom(tipper, address, amount, 1, null, null, function(err, txId){
|
|
|
|
if (err) {
|
|
|
|
bot.postMessage(channel, err.message);
|
|
|
|
}
|
|
|
|
else {
|
2016-07-18 16:07:41 +02:00
|
|
|
bot.postMessage(channel, 'Wubba lubba dub dub! <@' + tipper + '> tipped <@' + id + '> ' + amount + ' LBC (' + txLink(txId) + ')', globalSlackParams);
|
2016-07-14 23:34:37 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function getAddress(userId, cb) {
|
|
|
|
lbry.getAddressesByAccount(userId, function(err, addresses) {
|
|
|
|
if (err) {
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
else if(addresses.length > 0) {
|
|
|
|
cb(null, addresses[0]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lbry.getNewAddress(userId, function(err, address) {
|
|
|
|
if (err) {
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cb(null, address);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-31 16:14:26 +02:00
|
|
|
function getValidatedAmount(amount) {
|
|
|
|
amount = amount.trim();
|
|
|
|
if (amount.toLowerCase().endsWith('lbc')) {
|
|
|
|
amount = amount.substring(0, amount.length-3);
|
|
|
|
}
|
|
|
|
return amount.match(/^[0-9]+(\.[0-9]+)?$/) ? amount : null;
|
2016-07-14 23:34:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function txLink(txId) {
|
|
|
|
return "<https://explorer.lbry.io/tx/" + txId + "|tx>";
|
2016-07-18 15:59:21 +02:00
|
|
|
}
|