From ea51962e777b5da18024ccf0b9f15e62bb355d30 Mon Sep 17 00:00:00 2001 From: Oj Date: Thu, 1 Jul 2021 20:41:16 +0100 Subject: [PATCH] [Various] New env JS file, AuthorGen --- .gitignore | 5 +---- README.md | 11 ++++++++++- src/authorGen.js | 25 +++++++++++++++++++++++++ src/index.js | 20 ++++++++++++++------ 4 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 src/authorGen.js diff --git a/.gitignore b/.gitignore index 645c9d9..02d0cdc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,6 @@ node_modules .cache - clones -src/gh_pat.json -# dist - +src/env.js devDist devTemp diff --git a/README.md b/README.md index e540663..72aeda3 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,13 @@ Builder for Module Store v2. -**To run this yourself, you need to [get your own GitHub Personal Access Token](https://docs.github.com/en/articles/creating-a-personal-access-token-for-the-command-line) and export it in `src/gh_pat.js`.** +To run this yourself, you **need** to: +1. [Get your own GitHub Personal Access Token](https://docs.github.com/en/articles/creating-a-personal-access-token-for-the-command-line) +2. Get a Discord bot token +3. Make a `src/env.js` file like: +```js +export default { + github: '', + discord: ' +} +``` \ No newline at end of file diff --git a/src/authorGen.js b/src/authorGen.js new file mode 100644 index 0000000..33be616 --- /dev/null +++ b/src/authorGen.js @@ -0,0 +1,25 @@ +import Env from './env.js'; + +import axios from 'axios'; + +const userCache = {}; + +const getUser = async (id) => userCache[id] || (userCache[id] = (await axios.get(`https://discord.com/api/v9/users/${id}`, { + headers: { + 'Authorization': `Bot ${Env.discord}` + } +})).data); + +export default async (id) => { + const user = await getUser(id); + + console.log(user); + + return { // Use semi-minified keys as to avoid size + i: id, // id + + n: `${user.username}#${user.discriminator}`, // name + + a: user.avatar // avatar + } +}; \ No newline at end of file diff --git a/src/index.js b/src/index.js index ed162f5..cbbad83 100644 --- a/src/index.js +++ b/src/index.js @@ -1,23 +1,23 @@ import ModuleRepos from './modules/index.js'; + import AutoTag from './autoTag.js'; import WebhookSend from './webhook.js'; import ImageCDN from './imageCdn.js'; +import authorGen from './authorGen.js'; import Parcel from 'parcel-bundler'; import axios from 'axios'; import glob from 'glob'; -import { rmSync, mkdirSync, readFileSync, writeFileSync, copyFileSync, existsSync, rmdirSync } from 'fs'; +import { rmSync, mkdirSync, readFileSync, writeFileSync, copyFileSync, existsSync } from 'fs'; import { createHash } from 'crypto'; import { dirname, sep } from 'path'; import { fileURLToPath } from 'url'; -let file; -let githubPAT; + try { - file = JSON.parse(readFileSync('./gh_pat.json')); - githubPAT = file.token; + import Env from './env.js'; } catch (error) { if (error.code !== 'ENOENT') throw error; githubPAT = process.env.GHTOKEN; @@ -80,7 +80,7 @@ const getGithubInfo = async (repo) => { const info = (await axios.get(`https://api.github.com/repos/${repo}`, { headers: { - 'Authorization': `token ${githubPAT}` + 'Authorization': `token ${Env.github}` } })).data; @@ -222,6 +222,14 @@ for (const parentRepo of ModuleRepos) { manifestJson.images = await ImageCDN(manifestJson); + manifestJson.authors = await Promise.all(manifestJson.authors.map(async (x) => { + if (x.match(/^[0-9]{17,18}$/)) { + return await authorGen(x); + } + + return x; + })); + moduleJson.modules.push(manifestJson); console.timeEnd(repo.slice(0, 2).join(' @ ')+`${repo[2] ? ` ${repo[2]}` : ''}`);