简体   繁体   English

discord.py - 如何为特定用户重置命令的冷却时间?

[英]discord.py - How to reset the cooldown of a command for a specific user?

I have two commands in my bot that use a cooldown: "suggest" and "repentir".我的机器人中有两个使用冷却时间的命令:“suggest”和“repentir”。 I have made another command to reset the cooldown for an user, here is the code of that command:我已经创建了另一个命令来为用户重置冷却时间,这是该命令的代码:

@client.command()
@commands.check(is_owner)
async def reset(ctx, user:discord.Member):
    repentir.reset_cooldown(ctx)
    suggest.reset_cooldown(ctx)
    await ctx.send("Le cooldown pour <@{}> a bien été réinitialisé !".format(ctx.author.id))

I have already tried to put user inside of the reset_cooldown() fonctions, but this command takes a Context argument, and user is Member type, so it raises an error... Is there any way to reset the cooldown for a specific user?我已经尝试将user放在reset_cooldown()函数中,但是这个命令需要一个Context参数,并且userMember类型,所以它会引发一个错误......有没有办法重置特定用户的冷却时间?

I don't believe that there's a command available to reset the cooldown just for a singular specified user.我不相信有一个命令可以仅为单个指定用户重置冷却时间。

If you aren't already, I'd suggest using the command decorator with the user variation of BucketType to make sure that cooldowns are user specific.如果您还没有,我建议使用带有 BucketType 用户变体的命令装饰器,以确保冷却时间是用户特定的。 @commands.cooldown(rate, per, commands.BucketType.user) . @commands.cooldown(rate, per, commands.BucketType.user) Docs for cooldown decorator 冷却装饰器的文档

You can do a function to check which members you want to reset the cooldown and another function "before_invoke" to before the command runs check the first function, and, if it is true, resets the cooldown您可以执行 function 来检查您要重置冷却时间的成员和另一个 function “before_invoke”到命令运行之前检查第一个 ZC1C425268E68385D4F4AB5074C17Z9,如果它是真的,

def check_if_it_is_me(ctx):
    return ctx.message.author.id == 759027058613026827


class ImaCog(commands.Cog, name='Imagens'):
    """Gifs e imagens para você se divertir"""

    def __init__(self, bot):
        self.bot = bot

    async def cog_before_invoke(self, ctx: commands.Context):
        if check_if_it_is_me(ctx):
            return ctx.command.reset_cooldown(ctx)

    @commands.command(brief='Manda uma piscadinha',
                      help='Use o comando para ver uma pisacadinha aleatória de algum anime. Você pode enviar a '
                           'piscadinha para alguem marcando a pessoa no comando')
    @commands.cooldown(1, 5, commands.BucketType.user)
    async def pisque(self, ctx, quem: discord.Member = None):
        gif = await buscar('animu', 'wink')
        await ctx.send(ctx.author.mention,
                       embed=gera_embed(ctx=ctx, link=gif, membro=quem,
                                        acao_s='piscou', acao_c='mandou uma piscada para'))

all commands in this cog have an "@ commands.cooldown (1, 5, commands.BucketType.user)" that limits the use of commands once every 5 seconds per user.此 cog 中的所有命令都有一个“@ commands.cooldown (1, 5, commands.BucketType.user)”,限制每个用户每 5 秒使用一次命令。 But before each cog command, the "async def cog_before_invoke" is called, which in turn calls the "check_if_it_is_me" function, which returns true if the command's author is me, and back to the "async def cog_before_invoke" function which resets the cooldown if the "check_if_it_is_me" function be true.但是在每个 cog 命令之前,会调用“async def cog_before_invoke”,然后调用“check_if_it_is_me”function,如果命令的作者是我,则返回 true,然后返回“async def cog_before_invoke”ZC1C425268E68385D14ZA,它会重置冷却 if4 “check_if_it_is_me” function 是真的。 If the "check_if_it_is_me" function is false (it is not me using the command), the "cog_before_invoke" function will not reset the cooldown, and the author of the command will have to wait for the cooldown normally.如果“check_if_it_is_me”function 为假(不是我使用该命令),“cog_before_invoke”function 将不会重置冷却时间,该命令的作者将必须正常等待冷却时间。

You can add more parameters for the "check_if_it_is_me" function, such as a list of members who will not have to wait for the cooldown or a time when the cooldown will have no effect, but that is up to you您可以为“check_if_it_is_me”function 添加更多参数,例如无需等待冷却时间或冷却时间无效的成员列表,但这取决于您

I hope it was clear and good luck我希望它很清楚,祝你好运

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

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