-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
30 lines (26 loc) · 1.31 KB
/
bot.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
const { DUMMY_BOARD } = require("./configHelper");
const { createSummary } = require("./boardProcessor");
const { createMemberLines } = require("./boardProcessor");
const { boardUrl, getLastLeaderBoard, getNewLeaderBoard } = require('./aocFetch');
const { sendMessage } = require('./discordNotifier');
const POLLING_INTERVAL = DUMMY_BOARD ? 10 * 1000 : 15 * 60 * 1000;
async function runBot() {
console.log('Bot Started!')
const oldLeaderBoardJson = getLastLeaderBoard();
console.log('Leaderboard from last run:', '\n' + (createMemberLines(oldLeaderBoardJson, '{}') || 'EMPTY'));
await postToDiscordIfChanged();
setInterval(postToDiscordIfChanged, POLLING_INTERVAL);
}
async function postToDiscordIfChanged() {
console.log(new Date(), `Checking leaderboard now! Next check in [${ Math.round(POLLING_INTERVAL / 60 / 1000).toFixed(0) } min]`);
const oldLeaderBoardJson = getLastLeaderBoard();
const newLeaderboardJson = await getNewLeaderBoard();
const message = createMemberLines(newLeaderboardJson, oldLeaderBoardJson);
if (message.length) {
const summary = createSummary(newLeaderboardJson, oldLeaderBoardJson);
await sendMessage(summary, message, `Full [Leaderboard](${ boardUrl })`);
} else {
console.log(`No change detected.`)
}
}
module.exports = { runBot };