【问题标题】:Discord.py Time Calculation and conversionDiscord.py 时间计算和转换
【发布时间】: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


    【解决方案1】:

    您应该使用datetime.strptime 来处理datetime 对象,您必须为此导入from datetime import datetime

    要将所有用户输入作为字符串使用,请在参数前使用*,这样它将获取参数的其余部分:

    async def timer(ctx, *, time):
    

    那么您可以使用datetime.strptime 与您的格式。您可以找到可用的“变量”here。在您的示例中,为了能够输入22-02-22, 11:30PM,格式为"%y-%m-%d, %I:%M%p"。您还可以使用tryexcept 支持多种格式:

    async def timer(ctx, *, time):
        try: 
            datetime_object = datetime.strptime(time, "%y-%m-%d, %I:%M%p")
        except ValueError:
            try:
                datetime_object = datetime.strptime(time, "second format")
            except ValueError:
                # And so on
    

    现在你有了计时器结束的时间,但看起来你想知道它需要多长时间才能结束,所以你可以用当前时间减去它结束的时间:

    time_until_timer_end = datetime_object - datetime.now()
    

    然后您可以访问天数或秒数(然后您可以将其转换为小时数),直到计时器结束:

    seconds = time_until_timer_end.seconds
    total_seconds = time_until_timer_end.total_seconds()
    days = time_until_timer_end.days
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-11
      • 2022-12-16
      • 2014-12-27
      • 2020-11-20
      • 2021-02-09
      • 2021-06-02
      • 2016-03-18
      相关资源
      最近更新 更多