How to resolve the rate limit of Discord API in Node.js App

Viewed 26

I have developed Node.js App using Discord api. This is my code with problem.

export const checkUserHasRole = async (guildId: string, roleIDs: string[], tokenType: string, accessToken: string) => {
  try {
    await axios.get(`https://discord.com/api/users/@me/guilds/${guildId}/member`, {
      headers: {
        'Authorization': `${tokenType} ${accessToken}`
      }
    }).then((res: any) => {
      const result = res.data
      console.log('user res: ', result)
      if (result) {
        let temp: boolean = false
        result.roles.map((role: string) => {
          roleIDs.map((roleId: string) => {
            if (role === roleId) {
              temp === true
              return temp
            }
          })
        })
        return temp
      } else {
        return false
      }
    })

  } catch (error) {
    console.log(error)
    return false
  }
}

This function returns true if user with discordId is in discord server with guildID, else returns false. I have to call this fuction the number of Discord servers. Af first, it works well but after 5 times, it doesn't well.

https://discord.com/api/users/@me/guilds/${guildId}/member This api return 429 (Too many requests error) status code in response. I have read this https://blog.xenon.bot/handling-rate-limits-at-scale-fb7b453cb235, here we can use Redis or Twilight HTTP-Proxy but I can't fix the problem. This project is on aaPanel with AWS EC2, so I tried changing nginx config file, but it didn't work well. Please help me. Thank you.

0 Answers
Related