【发布时间】:2022-02-21 21:35:55
【问题描述】:
所以,我最近决定在我的 discord 机器人中添加一个计时器命令,该命令需要特定的时间并在此之前倒计时。我设法用一些超级笨拙的东西拼凑在一起。
这是我目前的代码:
async def timer(ctx, days, hours, minutes, seconds):
try:
hoursint = int(hours)
daysint = int(days)
minutesint = int(minutes)
secondint = int(seconds)
if daysint < 0:
await ctx.send("I dont think im allowed to do negatives")
raise BaseException
elif hoursint < 0:
await ctx.send("I dont think im allowed to do negatives")
raise BaseException
elif minutesint < 0:
await ctx.send("I dont think im allowed to do negatives")
raise BaseException
elif secondint < 0:
await ctx.send("I dont think im allowed to do negatives")
raise BaseException
elif secondint <= 0 and hoursint <= 0 and daysint <= 0 and minutesint <= 0:
await ctx.send("That's not a time dummy!")
raise BaseException
if secondint >= 60:
secondint = secondint - 60
minutesint += 1
if minutesint >= 60:
minutesint = minutesint - 60
hoursint +=1
if hoursint >= 24:
hoursint = hoursint - 24
daysint += 1
message = await ctx.send("Timer: {seconds}")
if daysint > 0 and hoursint == 0:
hoursint = 23
daysint -= 1
minutesint = 60
secondint = 60
if hoursint > 0 and minutesint == 0:
minutesint = 60
secondint = 60
hoursint -= 1
if secondint == 0 and minutesint > 0:
secondint = 60
minutesint -= 1
while True:
if secondint > 0:
secondint -= 1
if secondint <= 0 and minutesint > 0:
minutesint -=1
secondint = 60
if minutesint <= 0 and hoursint > 0:
hoursint -= 1
minutesint = 60
if hoursint <= 0 and daysint > 0:
daysint -= 1
hoursint = 24
if secondint == 0 and minutesint == 0 and hoursint == 0 and daysint == 0:
await message.edit(content="Ended!")
break
await message.edit(content=f"**Timer: {daysint}:{hoursint}:{minutesint}:{secondint}**")
await asyncio.sleep(1)
await ctx.send(f"{ctx.author.mention} Your countdown Has ended!")
except ValueError:
await ctx.send("Must be a number!")
但是,我想让它更加直观/用户友好。目前,它只接受“d h m s”格式的输入,但我希望它接受用户的日期输入,即“22-2-22, 11:30pm IST”或类似的东西,并从当前设置一个计时器直到那个给定的时间。
我自己无法解决这个问题,如果有任何帮助,我将不胜感激!
【问题讨论】:
标签: time discord.py