首页 > 解决方案 > Discord.py while True 循环

问题描述

我尝试在一个真正的循环中更新从机器人发送的多条消息,睡眠时间为 15 分钟

@commands.cooldown(1, 30, commands.BucketType.user)
@bot.command()
async def updater(ctx):
    embed = discord.Embed(title="Title")
    
    msg = await ctx.send(embed=embed)
    all_stock.append(msg)

    while True:
        embed = discord.Embed(title="Title")
        await msg.edit(embed=embed)
        time.sleep(900)

但是一旦我运行该功能,其他命令就不再起作用了。我已经尝试创建一个单独的线程,但由于一些异步问题它没有工作。

标签: pythonpython-3.xdiscord.py

解决方案


其他命令不起作用,因为time.sleep阻塞,您应该asyncio.sleep使用

import asyncio

while True:
    # ...
    await asyncio.sleep(900)

推荐阅读