message: settle them promises
diff: @@ -4,8 +4,9 @@ import { delay } from "https://deno.land/std@0.61.0/async/delay.ts";
  import { Errors } from "../types/errors.ts";
  import { HttpResponseCode } from "../types/discord.ts";
  import { logRed } from "../utils/logger.ts";
  +import { baseEndpoints } from "../constants/discord.ts";
  -const queue: QueuedRequest[] = [];
  +const pathQueues: { [key: string]: QueuedRequest[] } = {};
  const ratelimitedPaths = new Map<string, RateLimitedPath>();
  let globallyRateLimited = false;
  let queueInProcess = false;
  @@ -40,9 +41,36 @@ async function processRateLimitedPaths() {
  processRateLimitedPaths();
  }
  +function addToQueue(request: QueuedRequest) {
  + const route = request.url.substring(baseEndpoints.BASE_URL.length + 1);
  + const parts = route.split("/");
  + // Remove the major param
  + parts.shift();
  + const [id] = parts;
  +
  + if (pathQueues[id]) {
  + pathQueues[id].push(request);
  + } else {
  + pathQueues[id] = [request];
  + }
  +}
  +
  +async function cleanupQueues() {
  + Object.entries(pathQueues).map(([key, value]) => {
  + if (!value.length) {
  + // Remove it entirely
  + delete pathQueues[key];
  + }
  + });
  +}
  +
  async function processQueue() {
  - if (queue.length && !globallyRateLimited) {
  - const request = queue.shift();
  + if (
  + (Object.keys(pathQueues).length) && !globallyRateLimited
  + ) {
  + await Promise.allSettled(
  + Object.values(pathQueues).map(async (pathQueue) => {
  + const request = pathQueue.shift();
  if (!request) return;
  const rateLimitedURLResetIn = await checkRatelimits(request.url);
  @@ -51,15 +79,15 @@ async function processQueue() {
  const rateLimitResetIn = await checkRatelimits(request.bucketID);
  if (rateLimitResetIn) {
  // This request is still rate limited readd to queue
  - queue.push(request);
  + addToQueue(request);
  } else if (rateLimitedURLResetIn) {
  // This URL is rate limited readd to queue
  - queue.push(request);
  + addToQueue(request);
  } else {
  // This request is not rate limited so it should be run
  const result = await request.callback();
  if (result && result.rateLimited) {
  - queue.push(
  + addToQueue(
  { ...request, bucketID: result.bucketID || request.bucketID },
  );
  }
  @@ -67,22 +95,25 @@ async function processQueue() {
  } else {
  if (rateLimitedURLResetIn) {
  // This URL is rate limited readd to queue
  - queue.push(request);
  + addToQueue(request);
  } else {
  // This request has no bucket id so it should be processed
  const result = await request.callback();
  if (request && result && result.rateLimited) {
  - queue.push(
  + addToQueue(
  { ...request, bucketID: result.bucketID || request.bucketID },
  );
  }
  }
  }
  + }),
  + );
  }
  - if (queue.length) {
  + if (Object.keys(pathQueues).length) {
  await delay(1000);
  processQueue();
  + cleanupQueues()
  } else queueInProcess = false;
  }
  @@ -231,7 +262,7 @@ async function runMethod(
  }
  };
  - queue.push({
  + addToQueue({
  callback,
  bucketID,
  url,
  