forked from discord/discord-open-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.js
51 lines (41 loc) · 1.45 KB
/
validate.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
const util = require('util');
const chalk = require('chalk');
const fetch = require('node-fetch');
const discordCommunities = require('./communities.json');
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
async function validateCommunity(community) {
while (true) {
const req = await fetch(`https://discordapp.com/api/v6/invite/${community.inviteCode}`);
const response = await req.json();
if (response.guild) break;
if (response.retry_after) {
console.log(chalk.yellow(`Rate limited for ${response.retry_after}ms, waiting`));
await delay(response.retry_after);
continue;
}
throw new Error(
`${chalk.yellow.bold(community.title)} (${community.inviteCode}): ${util.inspect(response)}`
);
}
}
async function validate() {
console.log(chalk.underline.bold.white('Validating open source community invite codes'));
const failedCommunities = [];
for (const community of discordCommunities.data) {
console.log(`${community.title} (${community.inviteCode})`);
try {
await validateCommunity(community);
} catch (err) {
failedCommunities.push(err.message);
}
}
if (failedCommunities.length) {
console.error(chalk.red.bold('Could not validate some community codes!\n'));
console.error(failedCommunities.join('\n') + '\n');
throw new Error('Failed to validate invite codes');
}
}
validate().catch(err => {
console.error(err.message);
process.exit(1);
});