Search code examples
pythondiscord.pypython-asyncioaiohttp

Detect failed edit of channel in discord.py


I am making a discord bot which lets users edit a voicechannel name. My code is something like this:

channel = client.get_channel(id)
try:
    await channel.edit(name="new name")
except:
    raise

And it raises fine if the name is invalid but not if the name is valid and the change was not successful due to Discords rate limit of I guess 2 edits every 10 minutes.

I think the problem is that there is no error on my end, Discord is just waiting with the response. Is there a way to give my request a timeout of about 5 seconds and raise if it takes too long?


Solution

  • I would check for both an error and a None response. Better yet, why not just check that the name after the change is what it should be?

    I don't think you can add in a timeout directly into the function call but this is the type of thing you might want to loop and add a asyncio.sleep into. Ignoring the first part, you might have something like this:

    NEW_NAME = "new name"
    while True:
        e = None
        try:
            await channel.edit(name=NEW_NAME)
        except Exception as e: pass
        # "channel.name" is pseudo-code. 
        # Use whatever method to check the name.
        if <channel.name> != NEW_NAME: 
            print ("Exception: %s ...(whatever else you want to print)" % e)
            await asyncio.sleep(60) # or whatever you think reasonable
        else:
            break