繁体   English   中英

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

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

我的机器人中有两个使用冷却时间的命令:“suggest”和“repentir”。 我已经创建了另一个命令来为用户重置冷却时间,这是该命令的代码:

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

我已经尝试将user放在reset_cooldown()函数中,但是这个命令需要一个Context参数,并且userMember类型,所以它会引发一个错误......有没有办法重置特定用户的冷却时间?

我不相信有一个命令可以仅为单个指定用户重置冷却时间。

如果您还没有,我建议使用带有 BucketType 用户变体的命令装饰器,以确保冷却时间是用户特定的。 @commands.cooldown(rate, per, commands.BucketType.user) 冷却装饰器的文档

您可以执行 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'))

此 cog 中的所有命令都有一个“@ commands.cooldown (1, 5, commands.BucketType.user)”,限制每个用户每 5 秒使用一次命令。 但是在每个 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 是真的。 如果“check_if_it_is_me”function 为假(不是我使用该命令),“cog_before_invoke”function 将不会重置冷却时间,该命令的作者将必须正常等待冷却时间。

您可以为“check_if_it_is_me”function 添加更多参数,例如无需等待冷却时间或冷却时间无效的成员列表,但这取决于您

我希望它很清楚,祝你好运

暂无
暂无

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

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