Making a discord bot change playing status every 10 seconds(让不和谐机器人每 10 秒改变一次播放状态)
问题描述
我正在尝试让测试不和谐机器人的状态每十秒在两条消息之间更改一次.我需要在状态消息更改时执行脚本的其余部分,但是每当我尝试使其工作时都会弹出错误.我的脚本中有线程,但我不完全确定在这种情况下如何使用它.
I'm trying to make the status for a test discord bot change between two messages every ten seconds. I need the rest of the script to execute while the status message changes, but an error keeps popping up whenever I try to make it work. There's threading in my script, but I'm not entirely sure how to use it in this circumstance.
@test_bot.event
async def on_ready():
print('Logged in as')
print(test_bot.user.name)
print(test_bot.user.id)
print('------')
await change_playing()
@test_bot.event
async def change_playing():
threading.Timer(10, change_playing).start()
await test_bot.change_presence(game=discord.Game(name='Currently on ' + str(len(test_bot.servers)) +
' servers'))
threading.Timer(10, change_playing).start()
await test_bot.change_presence(game=discord.Game(name='Say test.help'))
错误信息如下:
C:PythonPython36-32lib hreading.py:1182: RuntimeWarning: coroutine 'change_playing' was never awaited
self.function(*self.args, **self.kwargs)
推荐答案
不幸的是,线程和异步不能很好地结合在一起.您需要跳过额外的障碍来等待线程内的协程.最简单的解决方案是不使用线程.
Threading and asyncio don't play nice together unfortunately. You need to jump through extra hoops to await coroutines inside threads. The simplest solution is to just not use threading.
您要做的是等待一段时间,然后运行协程.这可以通过后台任务来完成(example)
What you are trying to do is wait a duration and then run a coroutine. This can be done with a background task (example)
async def status_task():
while True:
await test_bot.change_presence(...)
await asyncio.sleep(10)
await test_bot.change_presence(...)
await asyncio.sleep(10)
@test_bot.event
async def on_ready():
...
bot.loop.create_task(status_task())
您不能使用 time.sleep(),因为这会阻止机器人的执行.asyncio.sleep() 虽然和其他所有东西一样是一个协程,因此它是非阻塞的.
You cannot use time.sleep() as this will block the execution of the bot. asyncio.sleep() though is a coroutine like everything else and as such is non-blocking.
最后,@client.event 装饰器只能用于机器人识别为 事件.比如 on_ready 和 on_message.
Lastly, the @client.event decorator should only be used on functions the bot recognises as events. Such as on_ready and on_message.
这篇关于让不和谐机器人每 10 秒改变一次播放状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:让不和谐机器人每 10 秒改变一次播放状态
基础教程推荐
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 包装空间模型 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
