【问题标题】:How do I make cooldown display in mins:seconds | discord.py如何在分钟:秒内显示冷却时间 |不和谐.py
【发布时间】:2020-10-28 18:28:28
【问题描述】:

冷却使机器人在有冷却时间时以秒为单位显示冷却计时器。我想做到这一点,以便机器人在分钟:秒内显示它。不知道怎么做。

此代码的冷却时间为 5 分钟

  @rob.error
  async def rob_error(self, ctx, error):
      if isinstance(error, commands.BadArgument):
          await ctx.send("Member doesnt exist")
      if isinstance(error, commands.CommandOnCooldown):
           embed = discord.Embed(
              title="Your on a cooldown!", color=discord.Color.blue())
      embed.add_field(
          name="\u200b",
          value=
          f"Slow down will ya?\n Wait for {round(error.retry_after)} seconds"
      )
      await ctx.send(embed=embed)

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    如果您有秒数,您可以使用division (//) 轻松计算分钟数,使用modulo (%) 轻松计算剩余秒数。

    >>> x = 83
    >>> minutes = str(x // 60)
    >>> seconds = str(x % 60)
    >>> print(f"Minutes: {minutes} | Seconds: {seconds}")
    Minutes: 1 | Seconds: 23
    

    然后,如果您愿意,可以在必要时为它们添加前导零

    def leadingZero(time: str):
        if len(time) > 1:
            return time
    
        return "0" + time
    

    为了让“1:3”显示为“01:03”。

    这将使您的代码最终看起来像这样:

    @rob.error
    async def rob_error(self, ctx, error):
        if isinstance(error, commands.BadArgument):
            await ctx.send("Member doesnt exist")
        if isinstance(error, commands.CommandOnCooldown):
            embed = discord.Embed(title="You're on a cooldown!", color=discord.Color.blue())
    
            cd = round(error.retry_after)
            minutes = str(cd // 60)
            seconds = str(cd % 60)
    
            embed.add_field(name="\u200b", value=f"Slow down will ya?\nWait for {self.leadingZero(minutes)}:{self.leadingZero(seconds)}.")
            await ctx.send(embed=embed)
    
    def leadingZero(self, time: str):
        if len(time) > 1:
            return time
    
        return "0" + time
    

    另外,我看到你实际上并没有把你的 add_fieldctx.send 放在 is_instance 里面(它在 下面它),可能想把它们放在里面。最后,它应该是You're on a cooldown,而不是Your

    【讨论】:

    • 非常感谢,它真的帮助了我的经济机器人!
    猜你喜欢
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 2021-05-01
    • 2020-06-27
    相关资源
    最近更新 更多