-2

I have a bot written in python. Help me deal with HTTP requests. There is a task: rename the channel on command. But discord has a limit on renaming the channel (2 times in 10 minutes). I need to get a timer in seconds from the API and insert it into a condition (if the timer is greater than 0, then send a message with a timer). I can't figure out where I get this timer from... I tried different urls: https://discord.com/api/v9/applications/*app id*, https://discord.com/api/v9/channels/{ctx.channel.id}, https://discord.com/api/v9/users/*bot id*. In the last url, I found the rate-limits headers, but there was some other timer that didn't exactly show the limit for renaming the channel.

Here's my code:

@bot.command()
async def rename(ctx, *, name):
    await ctx.message.delete()
    headers={
        'authorization': 'Bot *Secret token :)*'
        }
    response = requests.head(f'*I do not know which link to insert here :(*', headers=headers)
    limit = response.headers('X-RateLimit-Reset-After') #I need there to be a timer that shows after how many seconds the bot will be able to rename the channel
    if limit == 0:
        await ctx.channel.edit(name=f'{name}')
    if limit > 0:
        await ctx.channel.send(f'Can`t rename. Pls, wait {limit} seconds')

mr bean
  • 62
  • 6
Alex
  • 11
  • 1

1 Answers1

0

The rate limit header shows up when you are trying to do a request to an endpoint, but fail. So what you can do to get the ratelimit timer in seconds is:

import requests, json

try:
   r = request.post("channel rename url", headers={"authorization": "Bot token"}, json={"json"})

except:
   data = r.json()

   limit = data["retry_after"]
   print(limit)


Please correct me if some of this is wrong. I havent used requests in a while.

Vinium
  • 21
  • 3