-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
185 lines (154 loc) · 4.31 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const fs = require('fs');
const Discord = require('discord.js');
const { botToken } = require('./auth.json');
const AXIE_INFINITY_SERVER_ID = '410537146672349205';
const SERVER_IDS = [
'401808016887906304',
// AXIE_INFINITY_SERVER_ID
];
const CHANNEL_NAMES = [
'bot-interaction'
];
const EMBED_COLOR = 0x8d65ff;
const HELP_DESCRIPTION = [
'**!leaderboard**, **!top**, **!tops**: Show invite leaderboard.',
'**!invites**: Show your invites.',
'**!help**: Display this help message.'
].join('\n');
const client = new Discord.Client({ sync: true });
const getOnlineMembers = () => {
let aggregatedOnlineMembers;
try {
aggregatedOnlineMembers = require('./online-members.json');
} catch (e) {
aggregatedOnlineMembers = {};
}
const members = client.guilds
.get(AXIE_INFINITY_SERVER_ID)
.members;
members.map(({ user }) => {
if (typeof aggregatedOnlineMembers[user.id] === 'undefined') {
aggregatedOnlineMembers[user.id] = {
id: user.id,
username: user.username,
discriminator: user.discriminator,
count: 0
};
}
aggregatedOnlineMembers[user.id].count++;
});
fs.writeFile(
'./online-members.json',
JSON.stringify(aggregatedOnlineMembers, null, 2),
err => {
if (err) {
console.error(err);
}
}
);
setTimeout(getOnlineMembers, 30000);
};
client.on('ready', () => {
console.log('I am ready!');
getOnlineMembers();
});
const accept = (commands, handler) => {
if (!Array.isArray(commands)) {
commands = [ commands ];
}
client.on('message', message => {
if (commands.indexOf(message.content) >= 0 &&
SERVER_IDS.indexOf(message.channel.guild.id) >= 0 &&
CHANNEL_NAMES.indexOf(message.channel.name) >= 0
) {
handler(message);
}
});
}
const countInvites = message => (
message.channel.guild
.fetchInvites()
.then(invites => {
let invitesByUser = {};
invites
// Count permanent invite codes only.
.filter(({ maxAge, uses }) => maxAge === 0 && uses > 0)
// Too bad Discord.js's collections do not have `.forEach()`.
.map(({ inviter, uses }, inviteCode) => {
if (typeof invitesByUser[inviter.id] === 'undefined') {
invitesByUser[inviter.id] = {
id: inviter.id,
username: inviter.username,
numInvites: 0
};
}
invitesByUser[inviter.id].numInvites += uses;
});
return invitesByUser;
})
);
const formatNoun = (count, singular, plural) => (
count > 1 ? plural : singular
);
const formatInvite = ({ username, numInvites }, index) => {
const noun = formatNoun(numInvites, 'invite', 'invites');
return `${index + 1} – **${username}**: *${numInvites} ${noun}*.`;
};
accept([ '!leaderboard', '!top', '!tops' ], message => {
countInvites(message)
.then(invitesByUser => {
let leaderboard = [];
for (let userId in invitesByUser) {
if (invitesByUser.hasOwnProperty(userId)) {
leaderboard.push([ userId, invitesByUser[userId] ]);
}
}
loaderboard = leaderboard
.sort((a, b) => b[1].numInvites - a[1].numInvites)
.slice(0, 10);
const description = leaderboard
.map(([ _, invite ], index) => formatInvite(invite, index))
.join('\n');
return message.channel.send('Top inviters:', {
embed: {
color: EMBED_COLOR,
description
}
});
})
.catch(err => {
console.error(err);
});
});
accept('!invites', message => {
countInvites(message)
.then(invitesByUser => {
const invite = invitesByUser[message.author.id] || {};
const numInvites = invite.numInvites || 0;
const noun = formatNoun(numInvites, 'invite', 'invites');
return message.channel.send(`<@${invite.id}> has ${numInvites} ${noun}.`);
})
.catch(err => {
console.error(err);
});
});
accept('!invite', message => {
message.channel
.send('Do you mean **!invites** (with an **s**)?')
.catch(err => {
console.error(err);
});
});
accept('!help', message => {
message.channel
.send('Some commands:', {
embed: {
color: EMBED_COLOR,
description: HELP_DESCRIPTION
}
})
.catch(err => {
console.error(err);
});
});
client.login(botToken);