-
Notifications
You must be signed in to change notification settings - Fork 11
/
voteBot.js
68 lines (57 loc) · 1.78 KB
/
voteBot.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
'use strict';
const logger = require('../../lib/logger');
const _superagent = require('superagent');
const util = require('util');
const superget = util.promisify(_superagent.get);
const WEB_HOST = process.env.WEB_HOST !== undefined ? process.env.WEB_HOST : null;
const TIMEOUT = 1000;
async function vote(webURL, choice) {
let url = `${webURL}/api/vote?choice=${choice}`;
return new Promise((res, rej) => {
setTimeout(() => {
logger.info(`✔ Voting for ${choice}`);
superget(url).then(res).catch(rej);
}, TIMEOUT);
});
}
async function getShortcodes(webURL) {
let url = `${webURL}/api/list`;
let response;
let shortcodes;
try {
response = await superget(url);
shortcodes = JSON.parse(response.text);
} catch (err) {
logger.error(err);
}
return shortcodes;
}
if (WEB_HOST !== null && WEB_HOST !== '') {
const WEB_URL = `http://${WEB_HOST}`;
Promise.resolve().then(async() => {
while(true) {
let response;
let probability = Math.random();
let shortcodes = await getShortcodes(WEB_URL);
if (probability < 0.15) {
response = vote(WEB_URL, ':disney-puppy-puppies-national-day-UTuy9luZStALS:');
} else if (probability < 0.35) {
response = vote(WEB_URL, ':cartoon-halloween-ghost-aTf4PONtSYB1e:');
} else if (shortcodes !== undefined) {
let random = shortcodes[Math.floor(Math.random()*10000) % shortcodes.length].shortcode;
response = vote(WEB_URL, random);
} else {
// Sleep for TIMEOUT till retrying
response = new Promise(res => { setTimeout(res, TIMEOUT); });
}
try {
await response;
} catch (err) {
logger.error(err);
}
}
});
} else {
logger.error('WEB_HOST environment variable must me set');
process.exit(1);
}