Repository navigation
Expand file tree
/
Copy pathbot.js
More file actions
169 lines (140 loc) · 4.51 KB
/
Copy pathbot.js
File metadata and controls
169 lines (140 loc) · 4.51 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
import Slack from "./slack.js";
import {
bot_port,
bot_token,
vote_channel,
vote_duration,
spark_user,
spark_pw,
} from "../config.js";
import {
animations,
available_strips_message,
strips,
vote_emojis,
vote_prompt,
} from "../led_config.js";
var spark = require('spark');
spark.login({ username: spark_user, password: spark_pw }, function(err, body) {
if (err) {
console.log("Failed to login:", body);
exit(0);
}
});
var api = new Slack(bot_token);
api.listenForCommands("/commands", bot_port);
api.startConnection();
api.on("command", function*(data, res) {
// Check whether the requested strip exists.
if (!strips.hasOwnProperty(data.text)) {
res.send(available_strips_message);
return;
}
// Check whether a vote already exists for the strip.
var strip_name = data.text;
if (strips[strip_name].voting) {
res.send("*" + strip_name + "* is already being voted on.");
return;
}
strips[strip_name].voting = true;
if (data.channel_name != vote_channel.substring(1)) {
res.send("Vote started in " + vote_channel + ".");
}
// Initiate the vote.
var post = yield api.slackApi("chat.postMessage", {
channel: vote_channel,
as_user: true,
text: "Voting has started for *" + strip_name + "*.",
});
var prompt = yield api.slackApi("chat.postMessage", {
channel: vote_channel,
as_user: true,
text: vote_prompt,
});
// Add reactions for each animation.
for (var animation in animations) {
yield api.slackApi("reactions.add", {
channel: prompt.channel,
timestamp: prompt.ts,
name: animations[animation],
});
}
// Update every second for the duration.
var num_reps = vote_duration / 1000;
setIntervalX(function(rep) {
var time_left = num_reps - rep;
update_timer(strip_name, post, time_left);
}, function() {
update_strip(strip_name, post, prompt);
}, 1000, num_reps + 1);
});
function setIntervalX(callback, onEnd, delay, repetitions) {
var x = 0;
var intervalID = setInterval(function () {
callback(x);
if (++x === repetitions) {
clearInterval(intervalID);
onEnd();
}
}, delay);
}
function update_timer(strip_name, post, time_left) {
api.slackApi("chat.update", {
channel: post.channel,
ts: post.ts,
text: "Voting has started for *" + strip_name + "*. [" + time_left + "s]",
});
}
function update_strip(strip_name, post, prompt) {
strips[strip_name].voting = false;
// Retrieve all reactions
api.slackApi("reactions.get", {
channel: prompt.channel,
timestamp: prompt.ts,
full: true,
}).then((res) => {
api.slackApi("chat.delete", {
channel: prompt.channel,
ts: prompt.ts,
});
var reactions = res.message.reactions;
// Find the most popular valid emoji
var high = 0;
for (var i = 1; i < reactions.length; i++) {
if (reactions[i].count > reactions[high].count && vote_emojis.indexOf(reactions[i].name) != -1) {
high = i;
}
}
// Don't make any changes unless somebody voted.
if (reactions[high].count == 1) {
api.slackApi("chat.update", {
channel: post.channel,
ts: post.ts,
text: "[:heart: ENDED] *" + strip_name + "* lives for another day.",
});
return;
}
var emoji = reactions[high].name;
for (var animation in animations) {
if (animations[animation] == emoji) {
// Update post
api.slackApi("chat.update", {
channel: post.channel,
ts: post.ts,
text: "[:" + emoji + ": ENDED] *" + strip_name + "* set to *" + animation + "*",
});
spark.getDevice(strips[strip_name].id, function(err, device) {
if (err) {
console.log("Failed to get device by id: " + strips[strip_name].id);
} else {
device.callFunction("pattern", animation, function(err, data) {
if (err) {
console.log("Failed to call function pattern.");
}
});
}
});
}
}
}).done();
}