Breaking timer in discord.py

Viewed 22

I create a simple bot for discord based on nextcord.py. I've just written a timer command, what is counting until reaching a limit based on users needs. I'm having a problem, because I don't know how to allow users (mostly user that has set new timer) to finish counting before final time elapsing. Could you help me with it, please? I just need a command for user to stop the timer.

Here's my code:

@client.command()
async def timer(ctx, limit:int=600):
    x = 0
    y = 0
    z = 0
    z1 = 0
    embed = nextcord.Embed(title=f":stopwatch: Timer:   {y}:{z1}{z}", color=0x9966cc)
    message = await ctx.send(embed=embed)
    while True:
        x = x + 1
        z = z + 1
        await asyncio.sleep(1)
        if z == 10:
            z = z - 10
            z1 = z1 + 1
        if z1 == 6:
            z1 = z1 - 6
            y = y+1
        embed = nextcord.Embed(title=f":stopwatch: Timer:   {y}:{z1}{z}", color=0x9966cc)
        await message.edit(embed=embed)
        if x == limit:
            break
    embed = nextcord.Embed(title="<a:exclamationgif:1021558972416786444> Time elapsed! <a:exclamationgif:1021558972416786444>", color=0x9966cc)
    await message.edit(embed=embed)
1 Answers

Would this work?:

@client.command()
async def timer(ctx, limit:int=600):
    x = 0
    y = 0
    z = 0
    z1 = 0
    embed = nextcord.Embed(title=f":stopwatch: Timer:   {y}:{z1}{z}", color=0x9966cc)
    message = await ctx.send(embed=embed)
    while True:
        x = x + 1
        z = z + 1
        await asyncio.sleep(1)
        if z == 10:
            z = z - 10
            z1 = z1 + 1
        if z1 == 6:
            z1 = z1 - 6
            y = y+1
        embed = nextcord.Embed(title=f":stopwatch: Timer:   {y}:{z1}{z}", color=0x9966cc)
        await message.edit(embed=embed)
        if x == limit:
            break
    embed = nextcord.Embed(title="<a:exclamationgif:1021558972416786444> Time elapsed! <a:exclamationgif:1021558972416786444>", color=0x9966cc)
    await message.edit(embed=embed)


@client.command()
async def timer_stop(ctx):
    await ctx.send("Timer stopped")
    return
Related