discord.py's client.user.edit method generating error

Question:

I have created a discord price bot. It gets the price from the API and updates the nickname of my discord bot with that value. But I keep getting the error

discord.errors.HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body In username: You are changing your username or
Discord Tag too fast. Try again later.

I am waiting for around 5 minutes between each call. Can anyone help me with this? I have tried googling how many client calls I can make to Discord and it seemed like I can make 50 calls in a minute to discord api whereas I am only making 1 call per 5 minutes still getting this error. I have given change nickname bot permission to the bot. I also looked at discord.py documentation but did not find anything that can help me.

@client.event
async def on_ready():
    while True:
        response = await getPrice()
        priceChange = response['priceChange']['h24']
        if(priceChange > 0):
            await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=f'24H u2197 {priceChange}%'))
        else:
            await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=f'24H u2199 {priceChange}%'))
        await client.user.edit(username = '$' + str(response['priceUsd']))
        print(response)
        await asyncio.sleep(300)

Any help would be much appreciated.

Asked By: Utsav Patel

||

Answers:

As a username follows a user at all times, Discord only allows them to be changed twice per hour. Basically, a Discord user can only change their username once every thirty minutes. In contrast, nicknames are always in a changing state and there are no major limits on how often they can be changed (read more).

So I recommend you to just change the status of the bot or, if it’s important to change the name of the bot, just change its nickname on your server.
For example, if you want to change the nickname on your server:

MY_GUILD_ID = 100000000000000000000

@client.event
async def on_ready():
    while True:
        response = await getPrice()
        priceChange = response['priceChange']['h24']
        if(priceChange > 0):
            await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=f'24H u2197 {priceChange}%'))
        else:
            await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=f'24H u2199 {priceChange}%'))
        guild = client.get_guild(MY_GUILD_ID)
        await guild.me.edit(nick='$' + str(response['priceUsd']))
        print(response)
        await asyncio.sleep(300)
Answered By: Hazzu