-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
62 lines (53 loc) · 1.61 KB
/
index.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
const express = require('express');
const Discord = require('discord.js');
const fetch = require('node-fetch');
fetch.Promise = require('bluebird');
const app = express();
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Listening on ${port}`);
});
const bot = new Discord.Client();
const discordToken = process.env.DISCORD_TOKEN;
let started;
setInterval(() => {
console.log('heartbeat', Date.now());
}, 10 * 1000);
bot.on('ready', () => {
console.log(`Logged in as ${bot.user.tag}!`);
started = true;
});
bot.on('message', async msg => {
if (msg.content === '$ping') {
msg.reply('Pong!');
}
if (
msg.content === '$leaderboard'
|| msg.content === '$rank'
) {
const message = await getLeaderboardMessage();
msg.reply(message);
}
});
if (discordToken) {
bot.login(discordToken);
}
const getLeaderboardMessage = async () => {
const objApps = await fetch('https://steem-sincerity.dapptools.info/s/api/most-used-apps/')
.then(res => res.json());
let total = 0;
objApps.data.forEach(app => {
total += app.active_accounts_count;
});
let rank = 0;
let message = 'here is the last 7 days active users leaderboard';
objApps.data.slice(0, 20).forEach(app => {
rank++;
const shares = parseFloat(100 / total * app.active_accounts_count).toFixed(2);
message += app.app === 'busy' || app.app === 'bsteem'
? `\n**${rank} ${app.app}: ${app.active_accounts_count} ${shares}%**`
: `\n${rank} ${app.app}: ${app.active_accounts_count} ${shares}%`;
});
message += '\nhttp://steemreports.com/sincerity-most-used-apps/';
return message;
};