【问题标题】:Cooldown For Command On Discord Bot PythonDiscord Bot Python上的命令冷却时间
【发布时间】:2018-02-15 15:43:18
【问题描述】:
@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 秒的冷却时间。

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    你应该用

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

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

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

    注意:在 discord.py rewrite (v1.0+) 中,您必须使用 BucketType.server,而不是 BucketType.guild

    这也会导致on_command_error 中出现CommandOnCooldown 异常

    【讨论】:

    • 非常感谢!我一直在寻找这个!
    • 根据我的代码应该把这段代码放在哪里......它只是一直说未解决的参考“冷却”
    • 我认为你需要在文件中 from discord.ext import commands 来解决它,然后你应该能够将它放在 @bot.commandasync def 之间
    • @discord.cooldown(1, 30, commands.BucketType.user) 类型错误:cooldown() 采用 0 个位置参数,但给出了 3 个未关闭的客户端会话 client_session:
    • 有没有办法让它告诉他们必须等待多长时间才能再次使用它......就像他们再次执行命令一样
    【解决方案2】:

    我知道一种在通道中发送正在冷却的方法。

    @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 ,在此处插入 ACTUAL 命令名称。)

    【讨论】:

      猜你喜欢
      • 2017-11-21
      • 1970-01-01
      • 2017-06-13
      • 2021-09-07
      • 2018-07-07
      • 2018-05-03
      • 2019-02-17
      • 2021-06-17
      • 2019-06-28
      相关资源
      最近更新 更多