仅在满足条件时才启动discord.py命令冷却 [英] Starting discord.py command cooldown only if condition is met

查看:83
本文介绍了仅在满足条件时才启动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?

EDIT1:通常使用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命令冷却的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆