当循环崩溃机器人

2024-04-19 05:54:20 发布

您现在位置:Python中文网/ 问答频道 /正文

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”超时后,由于某种原因,bot崩溃。我似乎找不到任何理由来解释原因

使用的所有变量都已定义,并且没有错误 机器人超时后就死了

知道为什么吗

enter image description here

bot崩溃后的终端图像:[编辑]

enter image description here


Tags: sendnumberforlenifmyawaitelse
1条回答
网友
1楼 · 发布于 2024-04-19 05:54:20

根据wait_for documentation,提供超时时间(默认为无)将引发asyncio.TimeoutError

在代码中:

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

这会导致您中断while循环(请注意,将条件设置为false不会改变任何东西,因为您已经退出了循环)。看起来机器人并没有崩溃,更像是机器人完成了程序的执行

也许你想要的是不要设置timeout

相关问题 更多 >