Discord Bot 命令的冷却时间 Python

Cooldown For Command On Discord Bot Python

@client.command(pass_context = True)
async def getalt(ctx):
    msg = ["gabrielwarren2000@gmail.com:Cyber123", "leandroriveros123@gmail.com:culillo123", "albesi8@msn.com:Albakortoci1", "dryden6@yahoo.ca:toysale22", "nichlas00100@gmail.com:nich918273645", "lodevanveen@gmail.com:Lodelode1", "kylefielding2011@gmail.com:emolover123", "rubbst3in@gmail.com:rube541632789mk", "jasonfryckman@ymail.com:fryckman22", "NickSaya1@hotmail.com:blackout541", "devinquan@yahoo.com:ploopy101"]
    await client.send_message(ctx.message.author, random.choice(msg))
    await client.send_message(ctx.message.channel, "Alt Has Been Seen To Your DMs")
    await client.purge_from(ctx.message.channel, limit=2)
    await client.send_message(ctx.message.author, "Please Wait 30 Seconds Before Using This Command Again.")

我想为此命令设置 30 秒的冷却时间。

你应该用

装饰你的命令
@commands.cooldown(1, 30, commands.BucketType.user)

这将增加每位用户每 30 秒使用 1 次的速率限制。 docs, example

您可以将 BucketType 更改为 defaultchannelserver 以创建全局、通道或服务器速率限制,但您只能对命令进行 1 次冷却。

注意:在 discord.py 重写 (v1.0+) 而不是 BucketType.server,你必须使用 BucketType.guild.

这也会在 on_command_error

中导致 CommandOnCooldown 异常

我知道在频道中发送正在冷却的方法。

@command_name.error
    async def command_name_error(ctx, error):
        if isinstance(error, commands.CommandOnCooldown):
            em = discord.Embed(title=f"Slow it down bro!",description=f"Try again in {error.retry_after:.2f}s.", color=color_code_here)
            await ctx.send(embed=em)

确保您已导入桶类型。如果不是-

from discord.ext.commands import cooldown, BucketType

注意 - 确保命令冷却事件始终具有不同的名称并且必须是 the_command_name_here.error (不要让它成为 the_command_name_here ,在那里插入实际的命令名称。)