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)
以上是我为某人编写的代码,循环执行了我打算执行的操作,但出现了错误。在“wait_for
”超时后,机器人由于某种原因崩溃。我似乎找不到任何原因。
所有使用的变量都定义好了,没有错误 机器人在超时后就会死亡。
知道为什么会这样吗?
机器人崩溃后的终端图像:[编辑]
答案 0 :(得分:1)
根据 wait_for documentation,提供超时时间(默认为 None)将引发 asyncio.TimeoutError
。
在您的代码中:
try:
msg = await self.client.wait_for('message', check=check, timeout = 60)
except asyncio.TimeoutError:
condition = False
break
这会导致您跳出 while 循环(请注意,将条件设置为 false 不会改变任何内容,因为无论如何您都已跳出循环)。看起来不像是机器人崩溃了,它看起来更像是机器人只是简单地完成了程序。
也许您想要的是不设置 timeout
。