ChanHY
ChanHY

Reputation: 17

How to Start and Break a While Loop in Different Functions?

@bot.command()
async def start(ctx):
    channel = bot.get_channel(#*****)
    while True:
        if x >= 5:
            await channel.Send("Hi")
            time.Sleep(1)

@bot.command()
async def stop(ctx):
    #Something to stop the while True loop in the 'start' function

I would like to know if there is a way to stop the while loop in the 'start' function by writting something in the 'stop' function.

Upvotes: 0

Views: 81

Answers (1)

PApostol
PApostol

Reputation: 2302

You can try something like this:

run_loop = True

@bot.command()
async def start(ctx):
    global run_loop
    channel = bot.get_channel(#*****)
    while run_loop:
        if x >= 5:
            await channel.Send("Hi")
            time.Sleep(1)

@bot.command()
async def stop(ctx):
    global run_loop
    run_loop = False

Upvotes: 1

Related Questions