-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.js
More file actions
87 lines (78 loc) · 2.97 KB
/
helper.js
File metadata and controls
87 lines (78 loc) · 2.97 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
let sprintf = require(`sprintf-js`).sprintf
, messages = require(`./messages`)
, config = require(`./config`)
, Adapter = require(`./adapter`).Adapter
, tool = require(`./tool`)
;
function updateVotingPowerStatus(bot, username) {
Adapter.instance().processAccountInfo(username, function(account) {
bot.user.setActivity(sprintf(`VP - %s%%.`, tool.calculateVotingPower(account)), { type: `WATCHING` });
});
}
function handleBotCommand(command, params, message) {
switch (command) {
case `hp`:
case `hh`:
handleHelpCommand(message);
break;
case `wp`:
handleUpvoteCommand(message, params);
break;
default:
console.info(sprintf(`Unsupported "%s" command received.`, command));
}
}
function handleHelpCommand(message) {
message.channel.send(sprintf(messages.info, message.author.id, config.username, config.commandPrefix))
}
function handleUpvoteCommand(message, params) {
if (params.length < 1 || !params[0]) {
console.error(`Failed to receive post URL.`, params);
message.channel.send(sprintf(messages.upvotePostUrlError, message.author.id, config.commandPrefix));
return
}
let postParams = tool.parsePostUrl(params[0]);
if (postParams.length < 2 || !postParams.author || !postParams.permlink) {
console.error(`Failed to parse post URL`, postParams);
message.channel.send(sprintf(messages.upvotePostNotFound, message.author.id, config.commandPrefix));
return
}
Adapter.instance().processGetContent(
postParams.author,
postParams.permlink,
function (result) {
if (
`active_votes` in result
&& result.active_votes.length > 0
&& tool.isArrayContainsProperty(result.active_votes, `voter`, config.username)
) {
message.channel.send(sprintf(messages.upvotePostVotedAlready, message.author.id, config.username));
return;
}
Adapter.instance().processVote(
config.postingKey,
config.username,
postParams.author,
postParams.permlink,
config.weight * 100,
function () {
message.channel.send(sprintf(messages.upvoteSuccess, message.author.id, config.username));
},
function () {
message.channel.send(sprintf(messages.systemError, message.author.id));
}
);
},
function (result) {
if (result && result.id === 0) {
message.channel.send(sprintf(messages.upvotePostNotFound, message.author.id));
} else {
message.channel.send(sprintf(messages.systemError, message.author.id));
}
}
);
}
module.exports = {
updateVotingPowerStatus: updateVotingPowerStatus
, handleBotCommand: handleBotCommand
};