如何从错误功能重置命令冷却时间

时间:2021-02-19 01:40:52

标签: discord.py

我有一个问题。似乎我不知道如何重置命令冷却时间,而不是在实际命令本身中。

@bot.command()
@commands.cooldown(1,900,type=commands.BucketType.member)
async def rob(ctx, left: discord.Member):
  guild=ctx.author.guild
  server_members=guild.members 
  if left in server_members:
    print('found user')
    random_chance = random.randint(0,1)
    if random_chance == 1:
      print('robbery successful')
    else:
      await ctx.channel.send('Robbery not successful lol noob')
  else:
    await ctx.channel.send('That person is not in this server... :sob:')

@rob.error
async def roberror(ctx, error):
  if isinstance(error, commands.MissingRequiredArgument):
    await ctx.channel.send('You have to supply me with someone to rob.. noob')
  elif isinstance(error, commands.BadArgument):
    await ctx.channel.send('You have to provide me with a valid person..')
  elif isinstance(error, commands.CommandOnCooldown):
    await ctx.channel.send(f'Chill. Your on cooldown. Try again in {math.ceil(error.retry_after)} seconds')
  else:
    raise error``` I would like to have it reset the command cooldown in the roberror function. Can anyone help me with this?

1 个答案:

答案 0 :(得分:2)

可以使用 reset_cooldown 属性重置命令的冷却时间,然后将命令上下文作为参数传递。这是一个使用它的例子,

rob 是命令标识符,然后 reset_cooldown 是属性。此示例将重置命令的冷却时间

rob.reset_cooldown(ctx)

当“rob”命令不成功时,你似乎想把它放在这里,你可以这样添加:

else:
    await ctx.channel.send('Robbery not successful lol noob')
    rob.reset_cooldown(ctx)

您可以在文档中找到更多信息:https://discordpy.readthedocs.io/en/latest/ext/commands/api.html