From d17d2d64fed3826adfa2501e12667dcd94b91a4b Mon Sep 17 00:00:00 2001 From: Ivan K Date: Tue, 29 Jan 2019 16:29:54 -0800 Subject: [PATCH] also validate codes --- package.json | 6 +++++- validate.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 validate.js diff --git a/package.json b/package.json index 23d8367..a2c95ed 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/validate.js b/validate.js new file mode 100644 index 0000000..71ff690 --- /dev/null +++ b/validate.js @@ -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); +});