My discord.py command for a bot is supposed to activate a while loop under the condition that a specific variable has a certain value (with a wait_for() coroutine within it):
cond = True
@bot.command()
async def test(ctx):
global cond
def check(m):
m.author == ctx.message.author and m.channel == ctx.message.channel
while cond is True:
try:
msg = await bot.wait_for('message', check=check, timeout=7200) # 2 hours
print('Message found!')
except asyncio.TimeoutError:
print('No message was found.')
It only executes while cond is True, however cond can change while wait_for() is running (I used a global variable for the example), and since this is a while loop it doesn't terminate during runtime if the condition is no longer met, rather it applies to the next iteration.
I want wait_for() immediately terminate soon as cond is no longer True, and any code following to not execute (including the except block). Alternatively the while loop could be terminated some way while wait_for() is running.
How can I achieve this?