also validate codes

master
Ivan K 2019-01-29 16:29:54 -08:00 committed by ivan k
parent 9aa68ad028
commit d17d2d64fe
2 changed files with 56 additions and 1 deletions

View File

@ -3,10 +3,14 @@
"version": "1.0.0",
"main": "communities.json",
"scripts": {
"test": "prettier -c communities.json || (npm run fmt && git diff && exit 1)",
"test": "npm run fmt-check && npm run validate-codes",
"validate-codes": "node ./validate.js",
"fmt-check": "prettier -c communities.json || (npm run fmt && git diff && exit 1)",
"fmt": "prettier --write communities.json"
},
"devDependencies": {
"chalk": "^2.4.2",
"node-fetch": "^2.3.0",
"prettier": "^1.16.1"
}
}

51
validate.js Normal file
View File

@ -0,0 +1,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/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);
});