'discord.py Multiple Countdown Timer
I was able to make a countdown timer that edits itself. However When I do multiple timers, one of the timer will either lag, or they all break.
How do i make it so that when someone starts a timer, that timer will start working on its own and any other action won't affect that timer. This way multiple timers can be made.
To start a timer I would just type //timer (time)
import discord
import os
import asyncio
client = discord.Client()
@client.event
async def on_ready(): #Print when The Code Has connected to Discord
print("{0.user} Has Connected To Discord".format(client))
@client.event
async def on_message(message):
Days = 0
Hours = 0
Minutes = 0
Seconds = 0
ClockH = "00"
ClockM = "00"
ClockS = "00"
if message.author == client.user:
if ("⌛COUNTDOWN⌛") in message.content:
timer = message.content.split()
timer = list(map(int,timer[1:]))
Days = timer[0]
Hours = timer[1]
Minutes = timer[2]
Seconds = timer[3]
Finish = False
while Finish == False:
if Hours <10: ClockH = "0"+str(Hours)
else: ClockH = str(Hours)
if Minutes <10: ClockM = "0"+str(Minutes)
else: ClockM = str(Minutes)
if Seconds <10: ClockS = "0"+str(Seconds)
else: ClockS = str(Seconds)
if Days == 0:
await message.edit(content="{}:{}:{}".format(ClockH,ClockM,ClockS))
else:
await message.edit(content="{} Days | {}:{}:{}".format(Days,ClockH,ClockM,ClockS))
if Minutes>0:
if Seconds>0: Seconds-=1
else:
Minutes-=1
Seconds = 59
elif Seconds>0:
Seconds-=1
else: Finish = True
await asyncio.sleep(1)
return
if "//timer" in message.content:
timer = message.content.split()
countdown = []
timer = timer[1:]
for i in range(len(timer)):
if ("day" in timer[i].lower()):
Days = int(timer[i-1])
if ("hour" in timer[i].lower()) or ("hr" in timer[i].lower()):
Hours = int(timer[i-1])
if "min" in timer[i].lower():
Minutes = timer[i-1]
if "sec" in timer[i].lower():
Seconds = int(timer[i-1])
if ":" in timer[i]:
timer = timer[i]
index = 0
for i in range(3):
if ":" in timer:
index = timer.find(":")
if len(timer[:index]) <= 2:
countdown.append(int(timer[:index]))
timer = timer[index+1:]
else:
Error = "Please Retype Timer"
else: countdown.append(int(timer))
Hours,Minutes,Seconds = countdown
await message.channel.send("⌛COUNTDOWN⌛ {} {} {} {}".format(Days,Hours,Minutes,Seconds))
print(Days, Hours,Minutes,Seconds)
client.run(os.environ['Token'])
Solution 1:[1]
It's no longer necessary to edit the timestamp messages yourself, and it's very likely to be considered API abuse when you do this - bots are not supposed to do x action every y time.
Instead, you can use discord's own timer in your messages. To use a timer, you simply put <t:12345:R> with a number there that represents the timestamp. It also looks nicer and can show detailed information when you hover over it.
@client.command()
async def countdown(ctx, hour: int, minute: int, second: int):
total_time = 3600*hour + 60*minute + second
timestamp = int(time.time()) + total_time
await ctx.send(f'<t:{timestamp}:R>')
(To do this without using commands, change ctx.send to message.channel.send, and use your own definition of the total time in seconds.)
Output:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Eric Jin |


