added minimum tip setting

This commit is contained in:
unek
2014-02-22 13:32:31 +01:00
parent 953b325711
commit 00565f5759
2 changed files with 32 additions and 21 deletions
+25 -16
View File
@@ -8,7 +8,7 @@ var irc = require('irc')
winston.cli();
// load settings
var settings = yaml.load(fs.readFileSync("./config/config.yml", "utf-8"));
var settings = yaml.load(fs.readFileSync('./config/config.yml', 'utf-8'));
// connect to coin json-rpc
winston.info('Connecting to coind...');
@@ -22,7 +22,7 @@ var coin = coin({
coin.getBalance(function(err, balance) {
if(err) {
winston.error('Could not get %s wallet balance! ', settings.coin.full_name, err);
winston.error('Could not connect to %s RPC API! ', settings.coin.full_name, err);
process.exit();
return;
}
@@ -87,7 +87,10 @@ String.prototype.expand = function(values) {
nick: client.nick
}
return this.replace(/%([a-zA-Z_]+)%/g, function(str, variable) {
return values[variable] || settings.coin[variable] || global[variable]
return typeof(values[variable]) == 'undefined' ?
(typeof(settings.coin[variable]) == 'undefined' ?
(typeof(global[variable]) == 'undefined' ?
str : global[variable]) : settings.coin[variable]) : values[variable];
});
}
@@ -103,21 +106,26 @@ client.addListener('error', function(message) {
client.addListener('message', function(from, channel, message) {
winston.info('[%s] <%s> %s', channel, from, message);
if(/^!tip/.test(message)) {
var match = message.split(' ');
if(match.length < 3) {
var match = message.match(/^!tip (\S+) (\d+)/);
if(match == null || match < 3) {
client.say(channel, 'Usage: !tip <nickname> <amount>')
return;
}
var to = match[1];
var amount = Number(match[2]);
if(amount < settings.coin.min_tip) {
client.say(channel, settings.messages.tip_too_small.expand({from: from, to: to, amount: amount}));
return;
}
client.getLoginStatus(from, function(status) {
// check if the sending user is logged in (identified)
if(status == 3) {
// check balance with min. 5 confirmations
coin.getBalance(from, settings.coin.min_confirmations, function(err, balance) {
if(err) {
winston.error('Error in !tip command: %s', err);
winston.error('Error in !tip command.', err);
client.say(channel, settings.messages.error.expand({name: from}));
return;
}
@@ -126,19 +134,19 @@ client.addListener('message', function(from, channel, message) {
if(balance >= amount) {
client.getAddress(to, function(err, to_address) { // get the address to actually create a new one
if(err) {
winston.error('Error in !tip command: %s', err);
winston.error('Error in !tip command', err);
client.say(channel, settings.messages.error.expand({name: from}));
return;
}
coin.send('move', from.toLowerCase(), to.toLowerCase(), amount, function(err, reply) {
if(err || !reply) {
winston.error('Error in !tip command: %s', err);
winston.error('Error in !tip command', err);
client.say(channel, settings.messages.error.expand({name: from}));
return;
}
winston.info("%s tipped %s %d%s", from, to, amount, settings.coin.short_name)
winston.info('%s tipped %s %d%s', from, to, amount, settings.coin.short_name)
client.say(channel, settings.messages.tipped.expand({from: from, to: to, amount: amount}));
});
})
@@ -157,7 +165,7 @@ client.addListener('message', function(from, channel, message) {
var user = params[1] || from;
client.getAddress(user, function(err, address) {
if(err) {
winston.error('Error in !address command: %s', err);
winston.error('Error in !address command', err);
client.say(channel, settings.messages.error.expand({name: from}));
return;
}
@@ -171,7 +179,7 @@ client.addListener('message', function(from, channel, message) {
var user = (params[1] || from).toLowerCase();
coin.getBalance(user, settings.coin.min_confirmations, function(err, balance) {
if(err) {
winston.error('Error in !balance command: %s', err);
winston.error('Error in !balance command', err);
client.say(channel, settings.messages.error.expand({name: from}));
return;
}
@@ -180,13 +188,14 @@ client.addListener('message', function(from, channel, message) {
coin.getBalance(user, 0, function(err, unconfirmed_balance) {
if(err) {
winston.error('Error in !balance command: %s', err);
winston.error('Error in !balance command', err);
client.say(channel, settings.messages.balance.expand({balance: balance, name: user}));
return;
}
var unconfirmed_balance = typeof(unconfirmed_balance) == 'object' ? unconfirmed_balance.result : unconfirmed_balance;
client.say(channel, settings.messages.balance.expand({balance: balance, name: user, unconfirmed: unconfirmed_balance - balance}));
client.say(channel, settings.messages.balance_unconfirmed.expand({balance: balance, name: user, unconfirmed: unconfirmed_balance - balance}));
})
});
}
@@ -201,7 +210,7 @@ client.addListener('message', function(from, channel, message) {
coin.validateAddress(address, function(err, reply) {
if(err) {
winston.error('Error in !withdraw command: %s', err);
winston.error('Error in !withdraw command', err);
client.say(channel, settings.messages.error.expand({name: from}));
return;
}
@@ -209,7 +218,7 @@ client.addListener('message', function(from, channel, message) {
if(reply.isvalid) {
coin.getBalance(from.toLowerCase(), settings.coin.min_confirmations, function(err, balance) {
if(err) {
winston.error('Error in !withdraw command: %s', err);
winston.error('Error in !withdraw command', err);
client.say(channel, settings.messages.error.expand({name: from}));
return;
}
@@ -223,7 +232,7 @@ client.addListener('message', function(from, channel, message) {
coin.sendFrom(from.toLowerCase(), address, balance - settings.coin.withdrawal_fee, function(err, reply) {
if(err) {
winston.error('Error in !withdraw command: %s', err);
winston.error('Error in !withdraw command', err);
client.say(channel, settings.messages.error.expand({name: from}));
return;
}
+7 -5
View File
@@ -3,8 +3,8 @@ connection:
port: 6667
secure: false
login:
nickname: 'nodetip'
username: 'nodetip'
nickname: nodetip
username: nodetip
realname: node.js dogecoin tipping bot
channels:
- '##tipbots'
@@ -13,16 +13,17 @@ rpc:
port: 22555
user: dogecoinrpc
pass: pass
master_account: unek
master_account: nodetip
coin:
withdrawal_fee: 2
min_withdraw: 5
min_confirmations: 3
min_tip: 5
short_name: ' DOGE'
full_name: dogecoin
messages:
error: 'Sorry %name%, something went wrong.'
no_funds: "Sorry %name%, you don't have enough funds (you're %short% %short_name% short)"
no_funds: "Sorry %name%, you don't have enough funds (you're %short%%short_name% short)"
not_identified: '%name%: You need to be identified with NickServ to tip.'
tipped: 'Wow! %from% tipped %to% %amount%%short_name%! "/msg %nick% help" to claim.'
balance: '%name% has %balance%%short_name%.'
@@ -31,4 +32,5 @@ messages:
withdraw_too_small: 'Sorry %name%, you need to withdraw at least %min_withdraw%%short_name% (you have %balance%%short_name%)'
invalid_address: 'Sorry %name%, the address you specified is invalid (%address%).'
withdraw_success: '%name%: %amount%%short_name% has been withdrawn from your account to %address%'
withdraw_success2: 'You have been charged %withdrawal_fee%%short_name% withdrawal fee. Transaction id: %transaction%.'
withdraw_success2: 'You have been charged %withdrawal_fee%%short_name% withdrawal fee. Transaction id: %transaction%.'
tip_too_small: 'Sorry %from%, your tip to %to% (%amount%%short_name%) is too small (min. %min_tip%%short_name%).'