Harukomaze
Harukomaze

Reputation: 462

While loop crashing bot

while condition:
            try:
                msg = await self.client.wait_for('message', check=check, timeout = 60)
            except asyncio.TimeoutError:
                condition = False
                break
            else:
                my_num = msg.content.split(' ')
                if len(my_num) > 2:
                    await ctx.send("Seems like you added too many arguments... Only use `category` `number`", delete_after=2)
                category = my_num[0]
                try:
                    index = int(my_num[1])
                except ValueError:
                     await ctx.send("The second argument needs to be an integer", delete_after=2)
                except IndexError:
                     await ctx.send("Provide two arguments: `category` `number`  (example: plants 3)")
                if category.lower() == "plants":
                    try:
                        my_query = new_list[index-1]
                    except IndexError:
                        await ctx.send("the number you wrote doesn't exist!", delete_after=2)
                    queried_potion = all_items[my_query]
                    try:
                        signs = queried_potion['sign']
                    except KeyError:
                        signs = []
                    try:
                        symbols = queried_potion['symbol']
                    except KeyError:
                        symbols = []
                    sign = " | ".join(signs) if len(signs) != 0 else "None"
                    symbol = " | ".join(symbols) if len(symbols) != 0 else "None"      
                    em = discord.Embed(title=f"Information for: {my_query}", description= f"**Symbols:** \n{symbol} \n\n**Signs:**\n {sign}", color = self.client.theme)
                    await mess.edit(embed=em)

Above is my code which I wrote for someone, the loop does what I was intending for it to do but there is something wrong. After the "wait_for" times out, the bot crashes for some reason. I can't seem to find any reason as to why.

All variables used are defined, and there are no errors the bot just dies after it times out.

Any idea why that might be?

enter image description here

Image of terminal after bot crashed: [edit]

enter image description here

Upvotes: 0

Views: 121

Answers (1)

Alexandre FERRERA
Alexandre FERRERA

Reputation: 413

According to wait_for documentation, providing a timeout time (default to None) will raise an asyncio.TimeoutError.

In your code:

try:
   msg = await self.client.wait_for('message', check=check, timeout = 60)
except asyncio.TimeoutError:
   condition = False
   break

This leads you to breaking out of the while loop (note that setting the condition to false doesn't change anything since you are out of the loop anyway). It does not look like the bot is crashing, it looks more like the bot simply finished executing the program.

Maybe what you want is to NOT set timeout.

Upvotes: 1

Related Questions