【问题标题】:Daily command cooldown | discord.py每日命令冷却 |不和谐.py
【发布时间】:2020-11-04 04:44:42
【问题描述】:

所以,目前我正在尝试进行冷却,以小时和分钟为单位显示剩余时间。我计算了分钟和秒,但我很难过几个小时。这是我的代码

  @commands.command(aliases=["claim"])
  @commands.cooldown(1, 86400, commands.BucketType.user)

  (some code here)


  @daily.error
  async def daily_error(self, ctx, error):
      if isinstance(error, commands.CommandOnCooldown):
          embed = discord.Embed(
              title="You're on a cooldown!", color=discord.Color.blue())

          cd = round(error.retry_after)
          hours = str(cd // 3600)
          minutes = str(cd % 60)

          embed.add_field(
              name="\u200b",
              value=
              f"Slow down will ya?\n Wait for `{self.leadingZero(hours)}hours{self.leadingZero(minutes)}minutes`"
          )
          await ctx.send(embed=embed)

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    所以@daily.error 可以正常工作。但我更喜欢使用on_command_error,因为它会为每个命令显示一个错误。这样做会更简单

    @client.event
    async def on_command_error(ctx, error):
        if isinstance(error, commands.CommandOnCooldown):
            wait ctx.send("You're on cooldown.")
            return
        else:
            raise error
    

    注意:raise error 将允许在终端中打印所有其他错误。 但是对于这段代码,它只会在几秒钟内显示出来,这不是你要找的。​​p>

    @client.event
    async def on_command_error(self, ctx, error):
        if isinstance(error, commands.CommandOnCooldown):
            embed = discord.Embed(
                title="You're on a cooldown!", color=discord.Color.blue())
    
            cd = round(error.retry_after)
            hours = str(cd // 3600)
            minutes = str(cd % 60)
    
            embed.add_field(
                name="\u200b",
                value=
                f"Slow down will ya?\n Wait for `{hours} hours {minutes} minutes`"
            )
            await ctx.send(embed=embed)
        else:
            raise error
    

    试试看,我知道这是否可行,因为你使用齿轮,而我没有。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-12
      • 2021-07-08
      • 1970-01-01
      • 2021-05-01
      • 2021-10-08
      • 2021-02-16
      • 1970-01-01
      相关资源
      最近更新 更多