-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcommunitybot.js
More file actions
485 lines (382 loc) · 16.3 KB
/
communitybot.js
File metadata and controls
485 lines (382 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
var fs = require("fs");
const steem = require('steem');
var utils = require('./utils');
var account = null;
var last_trans = 0;
var members = [];
var whitelist = [];
var config = null;
var first_load = true;
var last_voted = 0;
var skip = false;
var version = '1.1.0';
steem.api.setOptions({ url: 'https://api.steemit.com' });
utils.log("* START - Version: " + version + " *");
// Load the settings from the config file
loadConfig();
// If the API is enabled, start the web server
if(config.api && config.api.enabled) {
var express = require('express');
var app = express();
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/api/members', (req, res) => res.json({ members: members }));
app.listen(config.api.port, () => utils.log('API running on port ' + config.api.port))
}
// Check if bot state has been saved to disk, in which case load it
if (fs.existsSync('state.json')) {
var state = JSON.parse(fs.readFileSync("state.json"));
if (state.last_trans)
last_trans = state.last_trans;
if (state.last_voted)
last_voted = state.last_voted;
utils.log('Restored saved bot state: ' + JSON.stringify({ last_trans: last_trans, last_voted: last_voted }));
}
// Check if members list has been saved to disk, in which case load it
if (fs.existsSync('members.json')) {
var members_file = JSON.parse(fs.readFileSync("members.json"));
members = members_file.members;
utils.log('Loaded ' + members.length + ' members.');
}
// Schedule to run every minute
setInterval(startProcess, 60 * 1000);
function startProcess() {
// Load the settings from the config file each time so we can pick up any changes
loadConfig();
// Load the bot account info
steem.api.getAccounts([config.account], function (err, result) {
if (err || !result)
console.log(err, result);
else {
account = result[0];
// Check if there are any rewards to claim.
//claimRewards();
}
});
if (account && !skip) {
// Load the current voting power of the account
var vp = utils.getVotingPower(account);
if (config.detailed_logging)
utils.log('Voting Power: ' + utils.format(vp / 100) + '% | Time until next vote: ' + utils.toTimer(utils.timeTilFullPower(vp)));
// We are at 100% voting power - time to vote!
if (vp >= 10000) {
skip = true;
voteNext();
}
getTransactions();
// Save the state of the bot to disk.
saveState();
} else if(skip)
skip = false;
}
function getNextActiveMember(loop_count) {
if(!loop_count)
loop_count = 0;
if(loop_count == members.length)
return null;
if (last_voted >= members.length)
last_voted = 0;
var member = members[last_voted];
if(member == null)
return null;
// If whitelist_only is enabled, check if this member is still on the whitelist, otherwise skip to the next member
if(config.whitelist_only && whitelist.indexOf(member.name) < 0) {
last_voted++;
utils.log('Member @' + member.name + ' is no longer on the whitelist, skipping...');
return getNextActiveMember(loop_count + 1);
}
// Check if this member's membership is active
if(member.full_delegation || new Date(member.valid_thru) > new Date()) {
return member;
} else {
last_voted++;
return getNextActiveMember(loop_count + 1);
}
}
function voteNext() {
var member = getNextActiveMember();
if(member == null)
return;
steem.api.getDiscussionsByAuthorBeforeDate(member.name, null, new Date().toISOString().split('.')[0], 10, function (err, result) {
if (result && !err) {
if(result.length == 0 || !result[0]) {
utils.log('No posts found for this account: ' + member.name);
last_voted++;
return;
}
for(var i = 0; i < result.length; i++) {
var post = result[i];
// Make sure the post is less than 6.5 days old
if((new Date() - new Date(post.created + 'Z')) >= (6.5 * 24 * 60 * 60 * 1000)) {
utils.log('This post is too old for a vote: ' + post.url);
continue;
}
// Check if the bot already voted on this post
if(post.active_votes.find(v => v.voter == account.name)) {
utils.log('Bot already voted on: ' + post.url);
continue;
}
// Check if any tags on this post are blacklisted in the settings
if ((config.blacklisted_tags && config.blacklisted_tags.length > 0) || (config.whitelisted_tags && config.whitelisted_tags.length > 0) && post.json_metadata && post.json_metadata != '') {
var tags = JSON.parse(post.json_metadata).tags;
if((config.blacklisted_tags && config.blacklisted_tags.length > 0) && tags && tags.length > 0 && tags.find(t => config.blacklisted_tags.indexOf(t) >= 0)) {
utils.log('Post contains one or more blacklisted tags.');
continue;
}
if((config.whitelisted_tags && config.whitelisted_tags.length > 0) && tags && tags.length > 0 && !tags.find(t => config.whitelisted_tags.indexOf(t) >= 0)) {
utils.log('Post does not contain a whitelisted tag.');
continue;
}
}
// Check if this post has been flagged by any flag signal accounts
if(config.flag_signal_accounts) {
if(post.active_votes.find(function(v) { return v.percent < 0 && config.flag_signal_accounts.indexOf(v.voter) >= 0; })) {
utils.log('Post was downvoted by a flag signal account.');
continue;
}
}
sendVote(post, 0);
break;
}
last_voted++;
} else
console.log(err, result);
});
}
function sendVote(post, retries) {
utils.log('Voting on: ' + post.url);
steem.broadcast.vote(config.posting_key, account.name, post.author, post.permlink, config.vote_weight, function (err, result) {
if (!err && result) {
utils.log(utils.format(config.vote_weight / 100) + '% vote cast for: ' + post.url);
if(config.comment_location)
sendComment(post.author, post.permlink);
} else {
utils.log(err, result);
// Try again one time on error
if (retries < 1)
sendVote(post, retries + 1);
else {
utils.log('============= Vote transaction failed two times for: ' + post.url + ' ===============');
}
}
});
}
function sendComment(parentAuthor, parentPermlink) {
var content = null;
content = fs.readFileSync(config.comment_location, "utf8");
// If promotion content is specified in the config then use it to comment on the upvoted post
if (content && content != '') {
// Generate the comment permlink via steemit standard convention
var permlink = 're-' + parentAuthor.replace(/\./g, '') + '-' + parentPermlink + '-' + new Date().toISOString().replace(/-|:|\./g, '').toLowerCase();
// Replace variables in the promotion content
content = content.replace(/\{weight\}/g, utils.format(config.vote_weight / 100)).replace(/\{botname\}/g, config.account);
// Broadcast the comment
steem.broadcast.comment(config.posting_key, parentAuthor, parentPermlink, account.name, permlink, permlink, content, '{"app":"communitybot/' + version + '"}', function (err, result) {
if (!err && result) {
utils.log('Posted comment: ' + permlink);
} else {
logError('Error posting comment: ' + permlink);
}
});
}
// Check if the bot should resteem this post
if (config.resteem)
resteem(parentAuthor, parentPermlink);
}
function resteem(author, permlink) {
var json = JSON.stringify(['reblog', {
account: config.account,
author: author,
permlink: permlink
}]);
steem.broadcast.customJson(config.posting_key, [], [config.account], 'follow', json, (err, result) => {
if (!err && result) {
utils.log('Resteemed Post: @' + author + '/' + permlink);
} else {
utils.log('Error resteeming post: @' + author + '/' + permlink);
}
});
}
function getTransactions() {
var num_trans = 50;
// If this is the first time the bot is ever being run, start with just the most recent transaction
if (first_load && last_trans == 0) {
utils.log('First run - starting with last transaction on account.');
num_trans = 1;
}
// If this is the first time the bot is run after a restart get a larger list of transactions to make sure none are missed
if (first_load && last_trans > 0) {
utils.log('First run - loading all transactions since bot was stopped.');
num_trans = 1000;
}
steem.api.getAccountHistory(account.name, -1, num_trans, function (err, result) {
first_load = false;
if (err || !result) {
utils.log(err, result);
return;
}
result.forEach(function (trans) {
var op = trans[1].op;
// Check that this is a new transaction that we haven't processed already
if (trans[0] > last_trans) {
// We only care about transfers to the bot
if (op[0] == 'transfer' && op[1].to == account.name) {
var amount = parseFloat(op[1].amount);
var currency = utils.getCurrency(op[1].amount);
utils.log("Incoming Payment! From: " + op[1].from + ", Amount: " + op[1].amount + ", memo: " + op[1].memo);
if(currency == 'STEEM' && amount >= config.membership.dues_steem) {
// Update member info
updateMember(op[1].from, amount, -1);
}
// Check if a user is sponsoring another user with their delegation
if(op[1].memo.startsWith('$sponsor')) {
var user = op[1].memo.substr(op[1].memo.indexOf('@') + 1);
sponsorMember(op[1].from, user, amount);
}
} else if (op[0] == 'delegate_vesting_shares' && op[1].delegatee == account.name) {
// Update member info
updateMember(op[1].delegator, 0, parseFloat(op[1].vesting_shares));
utils.log('*** Delegation Update - ' + op[1].delegator + ' has delegated ' + op[1].vesting_shares);
}
// Save the ID of the last transaction that was processed.
last_trans = trans[0];
}
});
});
}
function updateMember(name, payment, vesting_shares) {
// If whitelist_only is enabled, check if the new member is on the whitelist
if(config.whitelist_only && whitelist.indexOf(name) < 0) {
sendPayment(name, (payment > 0) ? payment : 0.001, 'STEEM', 'whitelist_only');
return;
}
var member = members.find(m => m.name == name);
// Add a new member if none is found
if (!member) {
member = { name: name, valid_thru: null, vesting_shares: 0, total_dues: 0, joined: new Date(), sponsoring: [], sponsor: null };
members.push(member);
utils.log('Added new member: ' + name);
}
member.total_dues += payment;
if(vesting_shares >= 0)
member.vesting_shares = vesting_shares;
// Has the member delegated the full amount to the bot?
member.full_delegation = member.vesting_shares >= config.membership.full_delegation_vests;
if(!member.full_delegation) {
// Has the member delegated the minimum amount to the bot?
var delegation = member.vesting_shares >= config.membership.delegation_vests;
// Get the date that the membership is currently valid through.
var valid_thru = new Date(Math.max(new Date(member.valid_thru), new Date(config.membership.start_date), new Date()));
// Get the dues amount based on whether or not they are a delegator
var dues = (config.membership.dues_steem_no_delegation == 0 || delegation) ? config.membership.dues_steem : config.membership.dues_steem_no_delegation;
// Calculate how much longer they have paid for.
var extension = payment / dues * config.membership.membership_period_days * 24 * 60 * 60 * 1000;
// Update their membership record.
member.valid_thru = new Date(valid_thru.valueOf() + extension).toISOString();
utils.log('Member ' + name + ' valid through: ' + member.valid_thru);
sendPayment(name, 0.001, 'STEEM', 'member_valid_thru', 0, new Date(valid_thru.valueOf() + extension).toDateString());
} else
sendPayment(name, 0.001, 'STEEM', 'member_full_delegation', 0);
saveMembers();
}
function sponsorMember(sponsor, user, amount) {
var member = members.find(m => m.name == sponsor);
if(member && member.vesting_shares >= config.membership.full_delegation_vests) {
// Subtract the sponsorship amount from the sponsor
updateMember(member.name, 0, member.vesting_shares - config.membership.full_delegation_vests);
member.sponsoring.push(user);
// Add it to the new member
updateMember(user, 0, config.membership.full_delegation_vests);
var new_member = members.find(m => m.name == user);
if(new_member)
new_member.sponsor = sponsor;
}
}
function saveState() {
var state = {
last_trans: last_trans,
last_voted: last_voted
};
// Save the state of the bot to disk
fs.writeFile('state.json', JSON.stringify(state), function (err) {
if (err)
utils.log(err);
});
}
function loadConfig() {
config = JSON.parse(fs.readFileSync("config.json"));
// Load the whitelist from the specified location
utils.loadUserList(config.whitelist_location, function(list) {
if(list)
whitelist = list;
});
}
function saveMembers() {
// Save the members list to disk
fs.writeFile('members.json', JSON.stringify({ members: members }), function (err) {
if (err)
utils.log(err);
});
}
function sendPayment(to, amount, currency, reason, retries, data) {
if(!retries)
retries = 0;
// Make sure the recipient isn't on the no-refund list (for exchanges and things like that).
if (reason != 'forward_payment' && config.no_refund && config.no_refund.indexOf(to) >= 0) {
utils.log("Payment not sent to: @" + to + " for: " + reason + ' because they are on the no_refund list.');
return;
}
// Replace variables in the memo text
var memo = config.transfer_memos[reason];
memo = memo.replace(/{amount}/g, utils.format(amount, 3) + ' ' + currency);
memo = memo.replace(/{currency}/g, currency);
memo = memo.replace(/{account}/g, config.account);
memo = memo.replace(/{to}/g, to);
memo = memo.replace(/{tag}/g, data);
// Issue the payment.
steem.broadcast.transfer(config.active_key, config.account, to, utils.format(amount, 3) + ' ' + currency, memo, function (err, response) {
if (err) {
utils.log('Error sending payment to @' + to + ' for: ' + amount + ' ' + currency + ', Error: ' + err);
// Try again on error
if(retries < 2)
setTimeout(function() { refund(to, amount, currency, reason, retries + 1, data) }, (Math.floor(Math.random() * 10) + 3) * 1000);
else
utils.log('============= Payment failed three times for: @' + to + ' ===============');
} else {
utils.log('Payment of ' + amount + ' ' + currency + ' sent to @' + to + ' for reason: ' + reason);
}
});
}
function claimRewards() {
if (!config.auto_claim_rewards)
return;
// Make api call only if you have actual reward
if (parseFloat(account.reward_steem_balance) > 0 || parseFloat(account.reward_sbd_balance) > 0 || parseFloat(account.reward_vesting_balance) > 0) {
steem.broadcast.claimRewardBalance(config.posting_key, config.account, account.reward_steem_balance, account.reward_sbd_balance, account.reward_vesting_balance, function (err, result) {
if (err) {
utils.log(err);
}
if (result) {
var rewards_message = "$$$ ==> Rewards Claim";
if (parseFloat(account.reward_sbd_balance) > 0) { rewards_message = rewards_message + ' SBD: ' + parseFloat(account.reward_sbd_balance); }
if (parseFloat(account.reward_steem_balance) > 0) { rewards_message = rewards_message + ' STEEM: ' + parseFloat(account.reward_steem_balance); }
if (parseFloat(account.reward_vesting_balance) > 0) { rewards_message = rewards_message + ' VESTS: ' + parseFloat(account.reward_vesting_balance); }
utils.log(rewards_message);
// If there are liquid post rewards, withdraw them to the specified account
if (parseFloat(account.reward_sbd_balance) > 0 && config.post_rewards_withdrawal_account && config.post_rewards_withdrawal_account != '') {
// Send liquid post rewards to the specified account
steem.broadcast.transfer(config.active_key, config.account, config.post_rewards_withdrawal_account, account.reward_sbd_balance, 'Liquid Post Rewards Withdrawal', function (err, response) {
if (err)
utils.log(err, response);
else {
utils.log('$$$ Auto withdrawal - liquid post rewards: ' + account.reward_sbd_balance + ' sent to @' + config.post_rewards_withdrawal_account);
}
});
}
}
});
}
}