How to avoid to exceed rate-limit by using discord.js api?

Viewed 6288

I am trying to understand how the discord API works. Especially the rate limit policy. By reading the docs . I need to implement a logic that track the rate of invalid request that it sent as a response header when I do my requests. However, when I do it (the request) using postman, the response headers don't include the rate-limit info as showed in this part of the docs . Hence, I don't know how to handle this issue.

So I have two questions :

  1. How to get rate-limit headers in the response ?
  2. How to implement the logic in my code to prevent my backend to send a request if the limit has been reached and set a timeout before the next try in order to avoid my IP to be banned by discord ?

A sample of my expressjs code :

const addnew = async (req, res) => {
  try {
    const { memberId, guildId, type, value, embed } = req.body;
   
        res
          .status(400)
          .send({ error: "error" });
        return;
    
    await client.addnew(memberId, guildId, type, value, embed);

    res.status(200).send(req.body);
  } catch (err) {
    console.log(err);
    res.status(500).send(err);
  }
};
1 Answers

Discord.js has logic that takes care of the rate limit issue for you. If you monitor the Client#rateLimit event, you can see that if you make too many API requests within your code, the event will trigger. This means that your requests have been enqueued and Discord.js will send them once your rate limit duration has been exceeded.

Related