我正在尝试使测试不一致机器人的状态每10秒在两条消息之间更改一次。当状态消息更改时,我需要执行脚本的其余部分,但每当我试图使其工作时,就会出现一个错误。我的脚本中有线程,但我不完全确定如何在这种情况下使用它。
@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:\Python\Python36-32\lib\threading.py:1182: RuntimeWarning: coroutine 'change_playing' was never awaited
self.function(*self.args, **self.kwargs)
请参阅:
Discord.py具有内置的后台任务功能
请参阅:
Discord.py带有内置的后台任务功能
discord.pyversion 1.1.0引入了^{} ,它旨在使您描述的后台任务更简单,并在存在连接问题时处理重新连接到不一致的潜在复杂逻辑。
下面是使用
tasks
执行任务的示例:很不幸,线程和异步不能很好地结合在一起。你需要跳过额外的圈来等待线程内的协程。最简单的解决方案就是不要使用线程。
你要做的是等待一段时间,然后进行一次联程。这可以通过后台任务(example)完成
不能使用time.sleep(),因为这将阻止机器人的执行。不过,asyncio.sleep()和其他东西一样是一个协程,因此是非阻塞的。
最后,装饰器
@client.event
应该只用于bot识别为events的函数。例如on_ready和on_消息。相关问题 更多 >
编程相关推荐