-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
378 lines (351 loc) · 14 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
const { Client, LimitedCollection, Message } = require('discord.js');
const express = require('express');
const { register } = require('prom-client');
const IPC = require('./IPC.js');
const Metrics = require('./Metrics');
/** @type {{TOPGG_KEY: string, API_URL: string}} */
// @ts-ignore
const { TOPGG_KEY, API_URL } = process.env;
const OWNERS = process.env.OWNERS?.split(',') || [];
class TOD extends Client {
constructor() {
super({
intents: ['GUILDS', 'GUILD_MESSAGES'],
makeCache: () => new LimitedCollection({ maxSize: 0 }),
presence: { activities: [{ name: 'Truth or Dare • /help', type: 'PLAYING' }] },
invalidRequestWarningInterval: 250,
http: { api: API_URL || undefined }
});
this.ipc = new IPC(this);
this.metrics = new Metrics(this);
this.commandStats = {
'memory--stats': 0,
'guild--count': 0,
'shard--guilds': 0,
'cluster--status': 0,
'command--stats': 0,
say: 0,
eval: 0,
restart: 0
};
this.websocketEvents = {};
this.domainHits = {};
/** @type {{current: number, past: number[], lastUpdate: number}} */
this.rollingStats = {
current: 0,
past: [],
lastUpdate: Date.now()
};
this.guildList = [];
setInterval(() => {
this.rollingStats.past.unshift(this.rollingStats.current);
if (this.rollingStats.past.length > 24) this.rollingStats.past.pop();
this.rollingStats.current = 0;
this.rollingStats.lastUpdate = Date.now();
}, 60 * 60 * 1000);
}
/**
*
* @param {import('./IPC').IncomingMessage} message
*/
run(message) {
this.clusterId = message.cluster;
this.options.shardCount = message.totalShards;
const [start, end] = message.shards;
this.options.shards = Array.from({ length: end - start + 1 }, (_, i) => i + start);
/** @type {Set<string>[]} */
this.guildList = Array.from({ length: this.options.shards.length }, () => new Set());
console.log(` -- [CLUSTER START] ${this.clusterId}`);
if (this.clusterId === 1) {
startWebServer();
setInterval(postTopgg, 30 * 60 * 1000);
setInterval(updateMetrics, 60 * 1000);
}
return this.login();
}
shardCalculator(guildId) {
// @ts-ignore
let shard = Math.floor(guildId / 2 ** 22) % this.options.shardCount;
// @ts-ignore
return shard - this.options.shards[0];
}
}
module.exports = TOD;
const client = new TOD();
client.on('raw', async data => {
if (data.t !== 'MESSAGE_CREATE') return;
const message = new Message(client, data.d);
if (message.author.bot) return;
// @ts-ignore
const mentioned = message.content.match(new RegExp(`^<@!?${client.user.id}>`))?.[0] ?? '';
if (!mentioned) return;
const command = message.content
.slice(mentioned.length)
.trim()
.split(' ')[0]
.replace(/[—–]/g, '--');
const rest = message.content.slice(mentioned.length).trim().split(' ').slice(1).join(' ');
switch (command) {
case 'memory--stats': {
const memory = await client.ipc.memoryUsage();
for (const k in memory) {
memory[k] = (memory[k] / 1024 / 1024).toFixed(2) + ' MB';
}
// @ts-ignore
await client.api.channels[message.channelId].messages
.post({
data: {
content: '```js\n' + require('util').inspect(memory) + '\n```'
}
})
.catch(_ => null);
break;
}
case 'guild--count': {
const guildCounts = await client.ipc.broadcastEval(
'this.guildList.reduce((a, c) => a + c.size, 0)'
);
// @ts-ignore
await client.api.channels[message.channelId].messages
.post({
data: {
content: `This cluster: ${client.guildList
.reduce((a, c) => a + c.size, 0)
.toLocaleString()}\nTotal Guilds: ${guildCounts
.reduce((a, c) => a + c, 0)
.toLocaleString()}`
}
})
.catch(_ => null);
break;
}
case 'shard--guilds': {
const guildCounts = (
await client.ipc.broadcastEval('this.guildList.map(s => s.size)')
).flat();
const link = await require('superagent')
.post(`https://haste.unbelievaboat.com/documents`)
.send(
guildCounts.map((c, i) => `${i.toString().padStart(4, ' ')}\t${c}`).join('\n')
)
.then(res => `https://haste.unbelievaboat.com/${res.body.key}`);
// @ts-ignore
await client.api.channels[message.channelId].messages
.post({ data: { content: `${Math.max(...guildCounts)}\n${link}` } })
.catch(_ => null);
break;
}
case 'cluster--status': {
const clusters = await client.ipc.broadcastEval(
'this.isReady() ? "online" : "offline"'
);
// @ts-ignore
await client.api.channels[message.channelId].messages
.post({
data: {
content:
'```js\n' +
require('util').inspect(
Object.fromEntries(
[...clusters.entries()].map(([a, b]) => [a + 1, b])
)
) +
'\n```'
}
})
.catch(_ => null);
break;
}
case 'command--stats': {
const totals = await client.ipc.broadcastEval('[this.rollingStats, this.commandStats]');
const [rollingStats, commandStats] = totals.reduce(([r, c], [r1, c1], i) => [
{
current: r.current + r1.current,
past: Array.from(
{ length: Math.max(r.past.length, r1.past.length) },
(_, k) => (r.past[k] || 0) + (r1.past[k] || 0)
),
lastUpdate: (r.lastUpdate * i + r1.lastUpdate) / (i + 1)
},
Object.fromEntries(
Object.entries(c).map(([name, count]) => [name, c1[name] + count])
)
]);
// @ts-ignore
await client.api.channels[message.channelId].messages
.post({
data: {
content: `Usage since <t:${
(rollingStats.lastUpdate / 1000) | 0
}:R>: ${rollingStats.current.toLocaleString()}\nAverage Usage/h: ${
rollingStats.past.reduce((a, c) => a + c, 0) / rollingStats.past.length
}\n__Totals:__\n${Object.entries(commandStats)
.map(([name, count]) => `${name}: ${count.toLocaleString()}`)
.join('\n')}`
}
})
.catch(_ => null);
break;
}
case 'say': {
if (!OWNERS.includes(message.author.id)) break;
let [_, channel = message.channelId, mess = ''] =
rest.match(/(?:(?:<#)?(\d+)>?\s+)?((?:.|\s)+)/i) ?? [];
// @ts-ignore
const res = await client.api.channels[channel].messages
.post({ data: { content: mess } })
.then(_ => true)
.catch(_ => false);
if (!res) {
// @ts-ignore
await client.api.channels[message.channelId].messages
.post({ data: { content: ':x: failed to send' } })
.catch(_ => null);
}
break;
}
case 'eval': {
if (!OWNERS.includes(message.author.id)) break;
const hide = (str, thing) => str.replaceAll(thing, '-- NOPE --');
let result, type, length;
try {
result = await eval(rest);
type = typeof result;
if (typeof result !== 'string') result = require('util').inspect(result);
result = hide(result, client.token);
length = result.length;
if (result.length > 4080)
result = await require('superagent')
.post(`https://haste.unbelievaboat.com/documents`)
.send(result)
.then(res => `https://haste.unbelievaboat.com/${res.body.key}.js`);
else result = '```js\n' + result + '\n```';
} catch (err) {
type = 'error';
result = '```js\n' + require('util').inspect(err).slice(0, 4070) + '\n```';
length = 'unknown';
}
// @ts-ignore
await client.api.channels[message.channelId].messages
.post({
data: {
embeds: [
{
title: type,
description: result,
color: 0x039dfc,
footer: {
text: `length: ${length}`
},
timestamp: new Date()
}
]
}
})
.catch(_ => null);
break;
}
case 'restart': {
if (!OWNERS.includes(message.author.id)) break;
/** @type {string} */
let result;
if (rest === 'all') {
result = await client.ipc.restartAll(message.author.id);
} else if (rest.includes(':')) {
const [min, max] = rest.split(':');
/** @type {[number, number]} */
const target = [parseInt(min, 10), parseInt(max, 10)];
result = await client.ipc.restartClusters(target, message.author.id);
} else {
result = await client.ipc.restartCluster(parseInt(rest, 10), message.author.id);
}
// @ts-ignore
await client.api.channels[message.channelId].messages
.post({
data: {
content: result
}
})
.catch(_ => null);
break;
}
default: {
return;
}
}
client.commandStats[command.toLowerCase()]++;
client.rollingStats.current++;
});
client.on('ready', () => {
console.log(` -- [CLUSTER ONLINE] ${client.clusterId}`);
});
client.on('shardReady', id => {
console.log(` -- [SHARD READY] ${id}`);
});
client.on('shardDisconnect', (e, id) => {
console.log(` -- [SHARD DISCONNECT] ${id} ${e.code}`);
});
client.on('invalidRequestWarning', ({ count, remainingTime }) => {
console.warn(
` -- [INVALID REQUESTS] ${count} used with ${Math.ceil(remainingTime / 1000)} seconds left`
);
});
client.on('raw', data => {
if (!['GUILD_CREATE', 'GUILD_DELETE'].includes(data.t)) return;
if (data.t === 'GUILD_CREATE')
client.guildList[client.shardCalculator(data.d.id)].add(data.d.id);
if (data.t === 'GUILD_DELETE')
client.guildList[client.shardCalculator(data.d.id)].delete(data.d.id);
});
client.on('raw', data => {
if (!data.t) return;
if (!client.websocketEvents[data.t]) client.websocketEvents[data.t] = 0;
client.websocketEvents[data.t]++;
});
function startWebServer() {
const app = express();
app.get('/metrics', async (req, res) => {
if (req.headers.authorization?.replace('Bearer ', '') !== process.env.PROMETHEUS_AUTH)
return res.sendStatus(401);
const metrics = await register.metrics();
res.send(metrics);
});
app.listen(3000, () => console.log(' -- [METRICS SERVER ONLINE]'));
}
async function postTopgg() {
const guildCounts = await client.ipc.broadcastEval(
'this.guildList.reduce((a, c) => a + c.size, 0)'
);
const shard_count = await client.ipc.masterEval('this.totalShards');
await require('superagent')
.post('https://top.gg/api/bots/692045914436796436/stats')
.set('Authorization', TOPGG_KEY)
.send({ shard_count, server_count: guildCounts.reduce((a, c) => a + c, 0) })
.catch(err => console.log('[TOPGG POST ERROR]', err.message));
}
async function updateMetrics() {
const checkClustersOnline = await client.ipc.broadcastEval('this.isReady()');
if (checkClustersOnline.some(s => !s)) return; // If all clusters aren't ready yet
// Server Count
const guildCounts = await client.ipc.broadcastEval(
'this.guildList.reduce((a, c) => a + c.size, 0)'
);
const guildCount = guildCounts.reduce((a, c) => a + c, 0);
client.metrics.guildCount.set(guildCount);
// Command Usage
const commandStatsArray = await client.ipc.broadcastEval('this.commandStats');
const commandStats = commandStatsArray.reduce((c, c1) =>
Object.fromEntries(Object.entries(c).map(([name, count]) => [name, c1[name] + count]))
);
client.metrics.updateCommandUse(commandStats);
// Websocket Events
const websocketEventsArray = await client.ipc.broadcastEval('this.websocketEvents');
const websocketEvents = websocketEventsArray.reduce((e, e1) =>
Object.fromEntries(
[...Object.keys(e), ...Object.keys(e1)]
.filter((a, i, r) => r.indexOf(a) === i)
.map(type => [type, (e[type] ?? 0) + (e1[type] ?? 0)])
)
);
client.metrics.updateWebsocketEvents(websocketEvents);
}