1. <tfoot id='mmVlx'></tfoot>

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

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

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

    2. 用命令中断循环

      时间:2023-10-19

          • <bdo id='RTK0J'></bdo><ul id='RTK0J'></ul>

              <tbody id='RTK0J'></tbody>
          • <small id='RTK0J'></small><noframes id='RTK0J'>

              • <legend id='RTK0J'><style id='RTK0J'><dir id='RTK0J'><q id='RTK0J'></q></dir></style></legend>
                <tfoot id='RTK0J'></tfoot>
                <i id='RTK0J'><tr id='RTK0J'><dt id='RTK0J'><q id='RTK0J'><span id='RTK0J'><b id='RTK0J'><form id='RTK0J'><ins id='RTK0J'></ins><ul id='RTK0J'></ul><sub id='RTK0J'></sub></form><legend id='RTK0J'></legend><bdo id='RTK0J'><pre id='RTK0J'><center id='RTK0J'></center></pre></bdo></b><th id='RTK0J'></th></span></q></dt></tr></i><div id='RTK0J'><tfoot id='RTK0J'></tfoot><dl id='RTK0J'><fieldset id='RTK0J'></fieldset></dl></div>
                本文介绍了用命令中断循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在我的 Python - Discord Bot 中,我想创建一个命令,它会导致循环运行.当我输入第二个命令时,循环应该停止.这么粗略的说:

                In my Python - Discord Bot, I wanted to create a command, which causes a loop to run. The loop should stop when I enter a second command. So roughly said:

                @client.event
                async def on_message(message):
                    if message.content.startswith("!C1"):
                        while True:
                            if message.content.startswith("!C2"):
                                break
                            else:
                                await client.send_message(client.get_channel(ID), "Loopstuff")
                                await asyncio.sleep(10)
                

                所以它每 10 秒在频道中发布一次Loopstuff",并在我输入 !C2 时停止

                So it posts every 10 seconds "Loopstuff" in a Channel and stops, when I enter !C2

                但我自己无法弄清楚.-.

                But I cant figure it out on my own .-.

                推荐答案

                在你的 on_message 函数中 message 内容不会改变.因此,另一条消息将导致 on_message 再次被调用一次.您需要一种同步方法,即.!C2 消息到达时将改变的全局变量或类成员变量.

                Inside your on_message function message content won't change. So another message will cause on_message to be called one more time. You need a synchronisation method ie. global variable or class member variable which will be changed when !C2 message arrives.

                keepLooping = False
                
                @client.event
                async def on_message(message):
                    global keepLooping
                    if message.content.startswith("!C1"):
                        keepLooping = True
                        while keepLooping:
                            await client.send_message(client.get_channel(ID), "Loopstuff")
                            await asyncio.sleep(10)
                    elif message.content.startswith("!C2"):
                        keepLooping = False
                

                附带说明,最好提供一个独立的示例,而不仅仅是一个函数.

                As a side note it's good to provide a standalone example not just a single function.

                这篇关于用命令中断循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何安排不使用 Windows 任务计划程序运行 bat 文件? 下一篇:如何让 pydoc 命令在 Windows 中工作?

                相关文章

              • <tfoot id='HNn4U'></tfoot>

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

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