Skip to content Skip to sidebar Skip to footer

Break Loop With Command

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 o

Solution 1:

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.eventasyncdefon_message(message):
    global keepLooping
    if message.content.startswith("!C1"):
        keepLooping = Truewhile 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.

Solution 2:

(Without trying for myself) Try:

while not message.content.startswith("!C2")

For the While clause, followed by the contents of your else clause.

Post a Comment for "Break Loop With Command"