forked from askmike/gekko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
campfire.js
131 lines (103 loc) · 3.48 KB
/
campfire.js
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
var _ = require('lodash');
var Moment = require('moment');
var Ranger = require('ranger');
var config = require('../core/util').getConfig().campfire;
var Actor = function() {
_.bindAll(this);
this.commands = [{
'handler': 'advice',
'callback': this.sayAdvice,
'description': "Advice on what position to take, depending on the current trend"
}, {
'handler': 'price',
'callback': this.sayPrice,
'description': "The current price of the asset in the configured currency"
}, {
'handler': 'donate',
'callback': this.sayDonate,
'description': "Where to send all of that extra coin that's weighing you down"
}, {
'handler': 'help',
'callback': this.sayHelp,
'description': "You are here"
}];
this.advice = null;
this.adviceTime = Moment.utc();
this.price = null;
this.priceTime = Moment.utc();
this.client = Ranger.createClient(config.account, config.apiKey);
this.client.room(config.roomId, this.joinRoom);
this.client.me(this.whoAmI);
};
Actor.prototype = {
processCandle: function(candle, done) {
this.price = candle.close;
this.priceTime = candle.date.start();
done();
},
processAdvice: function(advice) {
this.advice = advice.recommendation;
this.adviceTime = Moment.utc();
if (config.emitUpdates) {
this.sayAdvice();
}
},
sayAdvice: function() {
var message;
if (this.advice !== null) {
message = ["We think you should", this.advice + ".", "(" + this.adviceTime.fromNow() + ")"];
} else {
message = ["We don't have any advice for you quite yet."];
}
this.room.speak(message.join(' '));
},
sayPrice: function() {
var message;
if (this.price !== null) {
message = ["The price at the moment is", this.price + ".", "(" + this.priceTime.fromNow() + ")"];
} else {
message = ["We don't know the price right now."];
}
this.room.speak(message.join(' '));
},
sayDonate: function() {
this.room.speak("If you'd like to donate a few coins, you can send them here: 13r1jyivitShUiv9FJvjLH7Nh1ZZptumwW");
},
sayHelp: function() {
this.room.speak("I listen for the following inquiries...", this.pasteDescriptions);
},
pasteDescriptions: function() {
var descriptions = _.map(this.commands, function(command) {
return [command.handler + ':', command.description].join(' ');
}, this).join('\n');
this.room.paste(descriptions);
},
joinRoom: function(room) {
this.room = room;
this.room.join(this.listenForCommands);
},
listenForCommands: function() {
this.room.listen(this.executeCommands);
},
executeCommands: function(message) {
if (message.userId === this.user.id) return false; // Make the bot ignore itself
if (message.body === null) return false; // Handle weird cases where body is null sometimes
_.each(this.commands, function(command) {
if (this.textHasCommandForBot(message.body, config.nickname, command.handler)) {
command.callback();
}
}, this);
},
textHasCommandForBot: function(text, nickname, handler) {
var normalizedText = text.toLowerCase(),
normalizedNickname = nickname.toLowerCase(),
normalizedHandler = handler.toLowerCase();
var nicknameWasMentioned = normalizedText.indexOf(normalizedNickname) >= 0,
handlerWasMentioned = normalizedText.indexOf(normalizedHandler) >= 0;
return nicknameWasMentioned && handlerWasMentioned;
},
whoAmI: function(user) {
this.user = user;
}
};
module.exports = Actor;