python - 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)
但是一旦我运行该功能,其他命令就不再起作用了。我已经尝试创建一个单独的线程,但由于一些异步问题它没有工作。
解决方案
其他命令不起作用,因为time.sleep
阻塞,您应该asyncio.sleep
使用
import asyncio
while True:
# ...
await asyncio.sleep(900)
推荐阅读
- presto - Presto sql 函数 date_parse 在特定日期失败 (1960-01-01)
- javascript - 为什么这不能让我将输入的值作为位置?
- go - Azure 设备预配返回中间状态消息
- javascript - index.js:1 警告:在现有状态转换期间无法更新。渲染方法应该是 props 和 state 的纯函数
- objective-c - 创建了一个自定义类并获取架构 arm64 的未定义符号:
- c++ - 修改同一类型(但不同对象)公共方法中对象的私有指针
- tkinter - 如何在 Tkinter 的 for 循环中绑定按钮?
- python - 为什么当我将 cartpole 环境换成我自己的(更简单的)环境时,TensorFlow Agents 的内置 DQN 教程无法学习?
- azure - AD B2C 忘记密码 - 导航到密码页面自动验证代码,而不是单击继续按钮
- javascript - ReactJS:TypeError:无法读取未定义的属性“纬度”