【发布时间】:2019-08-29 00:15:33
【问题描述】:
我不知道如何使异步函数不阻塞下一行代码/防止循环从顶部重新开始。
异步函数:
async def updateEmbed(self, ctx, obj: discord.Message):
while self.bot.toggle[ctx.guild.id] == 1:
mainembed = discord.Embed(title='Current Servers', colour=discord.Colour.purple(), timestamp=datetime.datetime.utcnow())
result = [[f'{key} | ({len(value)} players)', *value] for key, value in self.bot.scrimmatches[ctx.guild.id].items()]
for x in result:
people = []
for aperson in x[1:]:
person = self.bot.get_user(aperson)
people.append(f'{person.mention}\n')
mainembed.add_field(name=x[0], value=(''.join(people)))
await obj.edit(embed=mainembed)
await asyncio.sleep(5)
主要命令:
self.bot.toggle[ctx.guild.id] = 0
async with async_timeout.timeout(length):
try:
while True:
msg = await self.bot.wait_for('message', check=check)
await tryRemoveUser(self, ctx, user=msg.author)
try:
self.bot.scrimmatches[msg.guild.id][((msg.content).upper())].append(msg.author.id)
except:
self.bot.scrimmatches[msg.guild.id] = ((msg.content).upper())
if self.bot.toggle[ctx.guild.id] == 0:
self.bot.toggle[ctx.guild.id] = 1
await updateEmbed(self, ctx, obj=history)
except:
pass
在主命令上,我希望 while True 循环重新开始,但是循环卡在 updateEmbed 函数的 while 循环中,因此它停止读取消息,因为循环不重复。
【问题讨论】:
标签: python python-3.6 discord.py discord.py-rewrite