From c349fd6ccc85c99c05b00cbae71f7591cd4c5d52 Mon Sep 17 00:00:00 2001 From: Oj Date: Thu, 1 Jul 2021 21:17:41 +0100 Subject: [PATCH] [AuthorGen] Add ratelimit, fix trying to use on authors not array --- src/authorGen.js | 19 +++++++++++++++---- src/index.js | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/authorGen.js b/src/authorGen.js index 33be616..c5dd525 100644 --- a/src/authorGen.js +++ b/src/authorGen.js @@ -2,13 +2,24 @@ import Env from './env.js'; import axios from 'axios'; +let lastRequest = 0; 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}` +const getUser = async (id) => { + if (userCache[id]) return userCache[id]; + + while (performance.now() - 500 < lastRequest) { // Has been less than 500ms since last request + await new Promise((res) => setTimeout(res, 100)); } -})).data); + + lastRequest = performance.now(); + + return 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); diff --git a/src/index.js b/src/index.js index cbbad83..59b6bde 100644 --- a/src/index.js +++ b/src/index.js @@ -222,7 +222,7 @@ for (const parentRepo of ModuleRepos) { manifestJson.images = await ImageCDN(manifestJson); - manifestJson.authors = await Promise.all(manifestJson.authors.map(async (x) => { + if (Array.isArray(manifestJson.authors)) manifestJson.authors = await Promise.all(manifestJson.authors.map(async (x) => { if (x.match(/^[0-9]{17,18}$/)) { return await authorGen(x); }