[AuthorGen] Add ratelimit, fix trying to use on authors not array

This commit is contained in:
Ducko 2021-07-01 21:17:41 +01:00 committed by Alyxia Sother
parent ea51962e77
commit c349fd6ccc
No known key found for this signature in database
GPG Key ID: 355968D14144B739
2 changed files with 16 additions and 5 deletions

View File

@ -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);

View File

@ -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);
}