<i id='Iz96P'><tr id='Iz96P'><dt id='Iz96P'><q id='Iz96P'><span id='Iz96P'><b id='Iz96P'><form id='Iz96P'><ins id='Iz96P'></ins><ul id='Iz96P'></ul><sub id='Iz96P'></sub></form><legend id='Iz96P'></legend><bdo id='Iz96P'><pre id='Iz96P'><center id='Iz96P'></center></pre></bdo></b><th id='Iz96P'></th></span></q></dt></tr></i><div id='Iz96P'><tfoot id='Iz96P'></tfoot><dl id='Iz96P'><fieldset id='Iz96P'></fieldset></dl></div>

        <bdo id='Iz96P'></bdo><ul id='Iz96P'></ul>
      <legend id='Iz96P'><style id='Iz96P'><dir id='Iz96P'><q id='Iz96P'></q></dir></style></legend>
    1. <tfoot id='Iz96P'></tfoot>

    2. <small id='Iz96P'></small><noframes id='Iz96P'>

    3. 仅在满足条件时才启动 discord.py 命令冷却

      时间:2023-09-09

        <small id='tCEub'></small><noframes id='tCEub'>

        <legend id='tCEub'><style id='tCEub'><dir id='tCEub'><q id='tCEub'></q></dir></style></legend>

            <tbody id='tCEub'></tbody>
            <bdo id='tCEub'></bdo><ul id='tCEub'></ul>

            <tfoot id='tCEub'></tfoot>

            1. <i id='tCEub'><tr id='tCEub'><dt id='tCEub'><q id='tCEub'><span id='tCEub'><b id='tCEub'><form id='tCEub'><ins id='tCEub'></ins><ul id='tCEub'></ul><sub id='tCEub'></sub></form><legend id='tCEub'></legend><bdo id='tCEub'><pre id='tCEub'><center id='tCEub'></center></pre></bdo></b><th id='tCEub'></th></span></q></dt></tr></i><div id='tCEub'><tfoot id='tCEub'></tfoot><dl id='tCEub'><fieldset id='tCEub'></fieldset></dl></div>
                本文介绍了仅在满足条件时才启动 discord.py 命令冷却的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我希望我的一个命令的冷却只有在满足函数中的条件时才开始,如下所示:

                I want the cooldown of one of my commands to start only if a condition in the function is met, like so:

                @bot.command
                async def move(ctx, destination):
                    destinations=["d1", "d2", "d3"] # List of valid arguments for the command
                    if destination in destinations:
                        movement(destination) # Function to actually move, not important for the question
                        # Start cooldown only here
                    else:
                        await ctx.send("This is not a valid destination")
                
                

                这样,如果用户输入错误的目的地,他们将不会受到冷却时间的惩罚.我怎样才能做到这一点?

                This way, if the user mistypes the destination, they won't be penalized with the cooldown. How can i achieve that?

                通常会使用 discord.py 的内置 @commands.cooldown 装饰器,这里是源代码:

                one would normally use discord.py's built-in @commands.cooldown decorator, here is the source:

                def cooldown(rate, per, type=BucketType.default):
                    def decorator(func):
                        if isinstance(func, Command):
                            func._buckets = CooldownMapping(Cooldown(rate, per, type))
                        else:
                            func.__commands_cooldown__ = Cooldown(rate, per, type)
                        return func
                    return decorator
                

                但这适用于整个命令.(它通常放在@bot.command 装饰器之后)

                However this applies to the whole command.(It is normally placed after the @bot.command decorator)

                推荐答案

                可能有很多方法可以制作自己的冷却时间,这里有一个简单的方法可以解决问题.其背后的想法是让机器人记住"某人上次使用此特定命令的时间,并在允许玩家移动之前检查该时间.

                There could be a lots of ways to craft your own cooldowns, here is a simple one that can do the trick. The idea behind it is for the bot to "remember" the last time someone used this specific command and to check this time before allowing the player to move.

                from datetime import datetime, timedelta    
                
                on_cooldown = {} # Dictionary with user IDs as keys and datetime as values
                destinations=["d1", "d2", "d3"] # List of valid arguments for the command
                move_cooldown = 5 # cooldown of the move command in seconds
                
                @bot.command()
                async def move(ctx, destination):
                
                    if destination in destinations:
                        author = ctx.author.id
                
                        try:
                            # calculate the amount of time since the last (successful) use of the command
                            last_move = datetime.now() - on_cooldown[author] 
                        except KeyError:
                            # the key doesn't exist, the player used the command for the first time
                            # or the bot has been shut down since
                            last_move = None
                            on_cooldown[author] = datetime.now()
                
                        if last_move is None or last_move.seconds > move_cooldown:
                            # move(...)
                            on_cooldown[author] = datetime.now() # the player successfully moved so we start his cooldown again
                            await ctx.send("You moved!")
                        else:
                            await ctx.send("You're still on cooldown.")    
                
                    else:
                        await ctx.send("This is not a valid destination")
                

                注意:您可能需要也可能不需要删除 @bot.command 装饰器后的括号.

                Note : you may or may not need to remove the parentheses after the @bot.command decorator.

                这篇关于仅在满足条件时才启动 discord.py 命令冷却的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Python - 是否可以在 Discord.py-v1.0 中等待一个事件或 下一篇:如何制作不和谐的机器人循环音频?[不和谐.py]

                相关文章

                最新文章

                1. <legend id='ybGq3'><style id='ybGq3'><dir id='ybGq3'><q id='ybGq3'></q></dir></style></legend>

                  <tfoot id='ybGq3'></tfoot>
                  <i id='ybGq3'><tr id='ybGq3'><dt id='ybGq3'><q id='ybGq3'><span id='ybGq3'><b id='ybGq3'><form id='ybGq3'><ins id='ybGq3'></ins><ul id='ybGq3'></ul><sub id='ybGq3'></sub></form><legend id='ybGq3'></legend><bdo id='ybGq3'><pre id='ybGq3'><center id='ybGq3'></center></pre></bdo></b><th id='ybGq3'></th></span></q></dt></tr></i><div id='ybGq3'><tfoot id='ybGq3'></tfoot><dl id='ybGq3'><fieldset id='ybGq3'></fieldset></dl></div>

                    • <bdo id='ybGq3'></bdo><ul id='ybGq3'></ul>

                    <small id='ybGq3'></small><noframes id='ybGq3'>