<i id='SeW1b'><tr id='SeW1b'><dt id='SeW1b'><q id='SeW1b'><span id='SeW1b'><b id='SeW1b'><form id='SeW1b'><ins id='SeW1b'></ins><ul id='SeW1b'></ul><sub id='SeW1b'></sub></form><legend id='SeW1b'></legend><bdo id='SeW1b'><pre id='SeW1b'><center id='SeW1b'></center></pre></bdo></b><th id='SeW1b'></th></span></q></dt></tr></i><div id='SeW1b'><tfoot id='SeW1b'></tfoot><dl id='SeW1b'><fieldset id='SeW1b'></fieldset></dl></div>

    <small id='SeW1b'></small><noframes id='SeW1b'>

  1. <legend id='SeW1b'><style id='SeW1b'><dir id='SeW1b'><q id='SeW1b'></q></dir></style></legend>
        <bdo id='SeW1b'></bdo><ul id='SeW1b'></ul>
    1. <tfoot id='SeW1b'></tfoot>

      让不和谐机器人每 10 秒改变一次播放状态

      时间:2023-09-09
          <tbody id='zNEsr'></tbody>

            1. <tfoot id='zNEsr'></tfoot>
              • <bdo id='zNEsr'></bdo><ul id='zNEsr'></ul>

                <small id='zNEsr'></small><noframes id='zNEsr'>

                <legend id='zNEsr'><style id='zNEsr'><dir id='zNEsr'><q id='zNEsr'></q></dir></style></legend>

              • <i id='zNEsr'><tr id='zNEsr'><dt id='zNEsr'><q id='zNEsr'><span id='zNEsr'><b id='zNEsr'><form id='zNEsr'><ins id='zNEsr'></ins><ul id='zNEsr'></ul><sub id='zNEsr'></sub></form><legend id='zNEsr'></legend><bdo id='zNEsr'><pre id='zNEsr'><center id='zNEsr'></center></pre></bdo></b><th id='zNEsr'></th></span></q></dt></tr></i><div id='zNEsr'><tfoot id='zNEsr'></tfoot><dl id='zNEsr'><fieldset id='zNEsr'></fieldset></dl></div>
                本文介绍了让不和谐机器人每 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 秒改变一次播放状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何使用带空格的命令名称? 下一篇:Discord.py 机器人没有证书

                相关文章

                最新文章

                <legend id='my18U'><style id='my18U'><dir id='my18U'><q id='my18U'></q></dir></style></legend>

              • <tfoot id='my18U'></tfoot>
                  • <bdo id='my18U'></bdo><ul id='my18U'></ul>

                  1. <small id='my18U'></small><noframes id='my18U'>

                  2. <i id='my18U'><tr id='my18U'><dt id='my18U'><q id='my18U'><span id='my18U'><b id='my18U'><form id='my18U'><ins id='my18U'></ins><ul id='my18U'></ul><sub id='my18U'></sub></form><legend id='my18U'></legend><bdo id='my18U'><pre id='my18U'><center id='my18U'></center></pre></bdo></b><th id='my18U'></th></span></q></dt></tr></i><div id='my18U'><tfoot id='my18U'></tfoot><dl id='my18U'><fieldset id='my18U'></fieldset></dl></div>