import { delay } from "https://esm.town/v/stevekrouse/delay?v=3";
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
export const discordFetch = async (token, route, method = "GET", body = undefined) => {
let res;
while (true) {
res = await fetchJSON(
`https://discord.com/api${route}`,
{
method,
headers: {
"Authorization": `Bot ${token}`,
},
body,
},
);
if (typeof res?.retry_after === "number") {
await delay(res.retry_after);
continue;
}
break;
}
return res;
};