2017-10-25 21:12:38 +02:00
'use strict' ;
const bitcoin = require ( 'bitcoin' ) ;
let config = require ( 'config' ) ;
config = config . get ( 'lbrycrd' ) ;
const lbry = new bitcoin . Client ( config ) ;
exports . commands = [
2017-12-25 01:49:45 +01:00
"tip"
2017-10-25 21:12:38 +02:00
]
exports . tip = {
2017-12-25 01:49:45 +01:00
usage : "<subcommand>" ,
description : 'balance: get your balance\n deposit: get address for your deposits\n withdraw ADDRESS AMOUNT: withdraw AMOUNT credits to ADDRESS\n [private] <user> <amount>: mention a user with @ and then the amount to tip them, or put private before the user to tip them privately.\n Key: [] : Optionally include contained keyword, <> : Replace with appropriate value.' ,
process : async function ( bot , msg , suffix ) {
let tipper = msg . author . id . replace ( '!' , '' ) ,
words = msg . content . trim ( ) . split ( ' ' ) . filter ( function ( n ) { return n !== "" ; } ) ,
subcommand = words . length >= 2 ? words [ 1 ] : 'help' ;
2017-12-06 19:39:31 +01:00
switch ( subcommand ) {
2017-12-25 01:49:45 +01:00
case 'help' : doHelp ( msg ) ; break ;
case 'balance' : doBalance ( msg , tipper ) ; break ;
case 'deposit' : doDeposit ( msg , tipper ) ; break ;
case 'withdraw' : doWithdraw ( msg , tipper , words ) ; break ;
default : doTip ( msg , tipper , words ) ;
}
}
2017-12-06 19:39:31 +01:00
}
2017-10-25 21:12:38 +02:00
function doBalance ( message , tipper ) {
2017-12-25 01:49:45 +01:00
lbry . getBalance ( tipper , 1 , function ( err , balance ) {
2017-10-25 21:12:38 +02:00
if ( err ) {
message . reply ( 'Error getting balance' ) ;
}
else {
message . reply ( 'You have *' + balance + '* LBC' ) ;
}
} ) ;
}
function doDeposit ( message , tipper ) {
2017-12-25 01:49:45 +01:00
if ( ! inPrivateOrBotSandbox ( message ) ) {
2017-10-25 21:12:38 +02:00
message . reply ( 'Please use <#369896313082478594> or DMs to talk to bots.' ) ;
return ;
}
2017-12-25 01:49:45 +01:00
getAddress ( tipper , function ( err , address ) {
2017-10-25 21:12:38 +02:00
if ( err ) {
message . reply ( 'Error getting deposit address' ) ;
}
else {
message . reply ( 'Your address is ' + address ) ;
}
} ) ;
}
function doWithdraw ( message , tipper , words ) {
2017-12-25 01:49:45 +01:00
if ( ! inPrivateOrBotSandbox ( message ) ) {
2017-10-25 21:12:38 +02:00
message . reply ( 'Please use <#369896313082478594> or DMs to talk to bots.' ) ;
return ;
}
if ( words . length < 4 ) {
doHelp ( message ) ;
return ;
}
var address = words [ 2 ] ,
2017-12-25 01:49:45 +01:00
amount = getValidatedAmount ( words [ 3 ] ) ;
2017-10-25 21:12:38 +02:00
if ( amount === null ) {
message . reply ( 'I dont know how to withdraw that many credits' ) ;
return ;
}
2017-12-25 01:49:45 +01:00
lbry . sendFrom ( tipper , address , amount , function ( err , txId ) {
2017-10-25 21:12:38 +02:00
if ( err ) {
message . reply ( err . message ) ;
}
else {
message . reply ( 'You withdrew ' + amount + ' to ' + address + ' (' + txLink ( txId ) + ')' ) ;
}
} ) ;
}
function doTip ( message , tipper , words ) {
if ( words . length < 3 || ! words ) {
doHelp ( message ) ;
return ;
}
2017-12-25 01:49:45 +01:00
Added Tipbot Privacy Mode
Allow for Tipbot to be used privately while still notifying the user that has received the tip, via private message.
Updates the Help message for new usage instructions, fixes misspelling of 'address' and adds a Key for the [ ] and < > syntax, as some users may not understand their usage.
Reworks the subcommand parsing by using a more compact Switch Function. Necessitates the use of 'break;' but is far more compact and legible, presumed the old if else block is from early development with fewer features available. Switch statement should allow for easier expansion.
Modifies doTip() to avoid requiring code duplication, includes two new variables used to indicate that the tip should be done in privacy mode and where the tip value is located in the 'words' array.
Significant change to sendLbc and it's function call, requirement for private message for recipient required User object, thus the function now requires the actual GuildMember object, not only the ID. While this marginally increases overhead, it should also allow access to the send() function for that Guild Member, allowing for private message to be sent. Further the addition of a privacy flag (0 for non-private, 1 for private) was added so that the function can determine whether to send a public or private response. this required updating references to the Guild Member's id to 'member.id', but otherwise operation remains the same until the transaction is completed, following which it will check the privacy flag and if it is set, will privately send the successful tip message to both author and recipient, otherwise it will simply reply via the normal '.reply()' function.
2017-11-14 18:33:17 +01:00
let prv = 0 ;
let amountOffset = 2 ;
if ( words . length >= 4 && words [ 1 ] === 'private' ) {
2017-12-25 01:49:45 +01:00
prv = 1 ;
amountOffset = 3 ;
Added Tipbot Privacy Mode
Allow for Tipbot to be used privately while still notifying the user that has received the tip, via private message.
Updates the Help message for new usage instructions, fixes misspelling of 'address' and adds a Key for the [ ] and < > syntax, as some users may not understand their usage.
Reworks the subcommand parsing by using a more compact Switch Function. Necessitates the use of 'break;' but is far more compact and legible, presumed the old if else block is from early development with fewer features available. Switch statement should allow for easier expansion.
Modifies doTip() to avoid requiring code duplication, includes two new variables used to indicate that the tip should be done in privacy mode and where the tip value is located in the 'words' array.
Significant change to sendLbc and it's function call, requirement for private message for recipient required User object, thus the function now requires the actual GuildMember object, not only the ID. While this marginally increases overhead, it should also allow access to the send() function for that Guild Member, allowing for private message to be sent. Further the addition of a privacy flag (0 for non-private, 1 for private) was added so that the function can determine whether to send a public or private response. this required updating references to the Guild Member's id to 'member.id', but otherwise operation remains the same until the transaction is completed, following which it will check the privacy flag and if it is set, will privately send the successful tip message to both author and recipient, otherwise it will simply reply via the normal '.reply()' function.
2017-11-14 18:33:17 +01:00
}
2017-12-25 01:49:45 +01:00
Added Tipbot Privacy Mode
Allow for Tipbot to be used privately while still notifying the user that has received the tip, via private message.
Updates the Help message for new usage instructions, fixes misspelling of 'address' and adds a Key for the [ ] and < > syntax, as some users may not understand their usage.
Reworks the subcommand parsing by using a more compact Switch Function. Necessitates the use of 'break;' but is far more compact and legible, presumed the old if else block is from early development with fewer features available. Switch statement should allow for easier expansion.
Modifies doTip() to avoid requiring code duplication, includes two new variables used to indicate that the tip should be done in privacy mode and where the tip value is located in the 'words' array.
Significant change to sendLbc and it's function call, requirement for private message for recipient required User object, thus the function now requires the actual GuildMember object, not only the ID. While this marginally increases overhead, it should also allow access to the send() function for that Guild Member, allowing for private message to be sent. Further the addition of a privacy flag (0 for non-private, 1 for private) was added so that the function can determine whether to send a public or private response. this required updating references to the Guild Member's id to 'member.id', but otherwise operation remains the same until the transaction is completed, following which it will check the privacy flag and if it is set, will privately send the successful tip message to both author and recipient, otherwise it will simply reply via the normal '.reply()' function.
2017-11-14 18:33:17 +01:00
let amount = getValidatedAmount ( words [ amountOffset ] ) ;
2017-12-25 01:49:45 +01:00
2017-10-25 21:12:38 +02:00
if ( amount === null ) {
message . reply ( 'I dont know how to tip that many credits' ) ;
return ;
}
2017-11-01 20:19:00 +01:00
if ( message . mentions . members . first ( ) . id ) {
2017-12-25 01:49:45 +01:00
let member = message . mentions . members . first ( )
Added Tipbot Privacy Mode
Allow for Tipbot to be used privately while still notifying the user that has received the tip, via private message.
Updates the Help message for new usage instructions, fixes misspelling of 'address' and adds a Key for the [ ] and < > syntax, as some users may not understand their usage.
Reworks the subcommand parsing by using a more compact Switch Function. Necessitates the use of 'break;' but is far more compact and legible, presumed the old if else block is from early development with fewer features available. Switch statement should allow for easier expansion.
Modifies doTip() to avoid requiring code duplication, includes two new variables used to indicate that the tip should be done in privacy mode and where the tip value is located in the 'words' array.
Significant change to sendLbc and it's function call, requirement for private message for recipient required User object, thus the function now requires the actual GuildMember object, not only the ID. While this marginally increases overhead, it should also allow access to the send() function for that Guild Member, allowing for private message to be sent. Further the addition of a privacy flag (0 for non-private, 1 for private) was added so that the function can determine whether to send a public or private response. this required updating references to the Guild Member's id to 'member.id', but otherwise operation remains the same until the transaction is completed, following which it will check the privacy flag and if it is set, will privately send the successful tip message to both author and recipient, otherwise it will simply reply via the normal '.reply()' function.
2017-11-14 18:33:17 +01:00
sendLbc ( message , tipper , member , amount , prv ) ;
2017-10-25 21:12:38 +02:00
}
2017-12-25 01:49:45 +01:00
else {
message . reply ( 'Sorry, I could not find a user in your tip...' ) ;
}
2017-10-25 21:12:38 +02:00
}
2017-10-25 22:14:37 +02:00
function doHelp ( message ) {
2017-12-25 01:49:45 +01:00
if ( ! inPrivateOrBotSandbox ( message ) ) {
2017-10-25 21:12:38 +02:00
message . reply ( 'Please use <#369896313082478594> or DMs to talk to bots.' ) ;
return ;
}
2017-10-25 22:14:37 +02:00
message . reply ( 'Sent you help via DM!' ) ;
2017-10-26 19:46:41 +02:00
message . author . send ( '**!tip**\n !tip balance: get your balance\n !tip deposit: get adress for your deposits\n !tip withdraw ADDRESS AMOUNT: withdraw AMOUNT credits to ADDRESS\n !tip <user> <amount>: send <amount> credits to <user>' ) ;
2017-10-25 21:12:38 +02:00
}
Added Tipbot Privacy Mode
Allow for Tipbot to be used privately while still notifying the user that has received the tip, via private message.
Updates the Help message for new usage instructions, fixes misspelling of 'address' and adds a Key for the [ ] and < > syntax, as some users may not understand their usage.
Reworks the subcommand parsing by using a more compact Switch Function. Necessitates the use of 'break;' but is far more compact and legible, presumed the old if else block is from early development with fewer features available. Switch statement should allow for easier expansion.
Modifies doTip() to avoid requiring code duplication, includes two new variables used to indicate that the tip should be done in privacy mode and where the tip value is located in the 'words' array.
Significant change to sendLbc and it's function call, requirement for private message for recipient required User object, thus the function now requires the actual GuildMember object, not only the ID. While this marginally increases overhead, it should also allow access to the send() function for that Guild Member, allowing for private message to be sent. Further the addition of a privacy flag (0 for non-private, 1 for private) was added so that the function can determine whether to send a public or private response. this required updating references to the Guild Member's id to 'member.id', but otherwise operation remains the same until the transaction is completed, following which it will check the privacy flag and if it is set, will privately send the successful tip message to both author and recipient, otherwise it will simply reply via the normal '.reply()' function.
2017-11-14 18:33:17 +01:00
function sendLbc ( message , tipper , member , amount , privacyFlag ) {
2017-12-25 01:49:45 +01:00
getAddress ( member . id . replace ( '!' , '' ) , function ( err , address ) {
2017-10-25 21:12:38 +02:00
if ( err ) {
message . reply ( err . message ) ;
}
else {
2017-12-25 01:49:45 +01:00
lbry . sendFrom ( tipper , address , amount , 1 , null , null , function ( err , txId ) {
2017-10-25 21:12:38 +02:00
if ( err ) {
message . reply ( err . message ) ;
}
else {
var imessage =
Added Tipbot Privacy Mode
Allow for Tipbot to be used privately while still notifying the user that has received the tip, via private message.
Updates the Help message for new usage instructions, fixes misspelling of 'address' and adds a Key for the [ ] and < > syntax, as some users may not understand their usage.
Reworks the subcommand parsing by using a more compact Switch Function. Necessitates the use of 'break;' but is far more compact and legible, presumed the old if else block is from early development with fewer features available. Switch statement should allow for easier expansion.
Modifies doTip() to avoid requiring code duplication, includes two new variables used to indicate that the tip should be done in privacy mode and where the tip value is located in the 'words' array.
Significant change to sendLbc and it's function call, requirement for private message for recipient required User object, thus the function now requires the actual GuildMember object, not only the ID. While this marginally increases overhead, it should also allow access to the send() function for that Guild Member, allowing for private message to be sent. Further the addition of a privacy flag (0 for non-private, 1 for private) was added so that the function can determine whether to send a public or private response. this required updating references to the Guild Member's id to 'member.id', but otherwise operation remains the same until the transaction is completed, following which it will check the privacy flag and if it is set, will privately send the successful tip message to both author and recipient, otherwise it will simply reply via the normal '.reply()' function.
2017-11-14 18:33:17 +01:00
'Wubba lubba dub dub! <@' + tipper + '> tipped <@' + member . id + '> ' + amount + ' LBC (' + txLink ( txId ) + '). ' +
2017-10-25 21:44:38 +02:00
'DM me `!tip` for tipbot instructions.'
2017-12-25 01:49:45 +01:00
if ( privacyFlag ) {
message . author . send ( imessage ) ;
member . send ( imessage ) ;
} else {
message . reply ( imessage ) ;
}
2017-10-25 21:12:38 +02:00
}
} ) ;
}
} ) ;
} ;
function getAddress ( userId , cb ) {
2017-12-25 01:49:45 +01:00
lbry . getAddressesByAccount ( userId , function ( err , addresses ) {
2017-10-25 21:12:38 +02:00
if ( err ) {
cb ( err ) ;
}
2017-12-25 01:49:45 +01:00
else if ( addresses . length > 0 ) {
2017-10-25 21:12:38 +02:00
cb ( null , addresses [ 0 ] ) ;
}
else {
2017-12-25 01:49:45 +01:00
lbry . getNewAddress ( userId , function ( err , address ) {
2017-10-25 21:12:38 +02:00
if ( err ) {
cb ( err ) ;
}
else {
cb ( null , address ) ;
}
} ) ;
}
} ) ;
}
2017-12-25 01:49:45 +01:00
function inPrivateOrBotSandbox ( msg ) {
if ( ( msg . channel . type == 'dm' ) || ( msg . channel . id === '369896313082478594' ) ) {
2017-10-25 21:12:38 +02:00
return true ;
2017-12-25 01:49:45 +01:00
} else {
2017-10-25 21:12:38 +02:00
return false ;
}
}
function getValidatedAmount ( amount ) {
amount = amount . trim ( ) ;
if ( amount . toLowerCase ( ) . endsWith ( 'lbc' ) ) {
2017-12-25 01:49:45 +01:00
amount = amount . substring ( 0 , amount . length - 3 ) ;
2017-10-25 21:12:38 +02:00
}
return amount . match ( /^[0-9]+(\.[0-9]+)?$/ ) ? amount : null ;
}
function txLink ( txId ) {
return "<https://explorer.lbry.io/tx/" + txId + ">" ;
2017-10-25 21:44:38 +02:00
}