简体   繁体   English

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.")

I want to set a 30 sec cooldown for this command.我想为此命令设置 30 秒的冷却时间。

You should decorate your command with你应该装饰你的命令

@commands.cooldown(1, 30, commands.BucketType.user)

This will add a ratelimit of 1 use per 30 seconds per user.这将增加每位用户每 30 秒使用 1 次的速率限制。 docs , example 文档示例

You can change the BucketType to default , channel or server to create a global, channel or server ratelimit instead, but you can only have 1 cooldown on a command.您可以将 BucketType 更改为defaultchannelserver以创建全局、频道或服务器速率限制,但您只能对命令进行 1 次冷却。

Note: In discord.py rewrite (v1.0+) instead of BucketType.server , you have to use BucketType.guild .注意:在 discord.py rewrite (v1.0+) 而不是BucketType.server ,你必须使用BucketType.guild

This will also cause a CommandOnCooldown exception in on_command_error这也将导致CommandOnCooldown异常on_command_error

I know a method of sending in the channel that cooldown is in process.我知道一种在通道中发送冷却正在进行的方法。

@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)

make sure you have imported bucket type.确保您已导入存储桶类型。 If not -如果不 -

from discord.ext.commands import cooldown, BucketType

NOTE - Make sure the command cooldown event always has a different name and has to be the_command_name_here.error (don't make it the_command_name_here , insert the ACTUAL command name there.)注意 - 确保命令冷却事件始终具有不同的名称,并且必须是the_command_name_here.error (不要将其设为 the_command_name_here,在那里插入 ACTUAL 命令名称。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM