Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
701 views
in Technique[技术] by (71.8m points)

python - How to reset cooldown of a command discord.py

I am trying to reset the cooldown if reacted with ?. I tried _set.reset_cooldown(self, ctx) and commands.Command.reset_cooldown but they didn't work. Am I doing something wrong? Can someone help me?


    @prefix.command(aliases=["set"])
    @commands.has_permissions(manage_messages=True)
    @commands.cooldown(1, 20, commands.BucketType.guild)
    async def _set(self, ctx, prefix):
        embed = discord.Embed(
            title="Prefix Change",
            description=f"Are you sure you want to change the prefix for Umbra to `{prefix}` in {ctx.guild}?",
            colour=ctx.author.color
            )
        message = await ctx.send(embed=embed)

        await message.add_reaction("?")
        await message.add_reaction("?")

        def check(reaction, user):
            if reaction.message.id == message.id:
                return user == ctx.author and str(reaction.emoji) in ["?", "?"]
            else:
                return

        while True:
            try:
                reaction, user = await self.bot.wait_for("reaction_add", timeout=180, check=check)

                if str(reaction.emoji) == "?":
                    client.query(
                        q.update(
                            q.select(['ref'], q.get(
                                q.match(
                                    q.index("prefixByGuildID"), f"{ctx.guild.id}"
                                    )
                                )
                            ), {"data": {"guildID": f"{ctx.guild.id}", "prefix": f"{prefix}"}}
                        )
                    )
                    conftrue = discord.Embed(
                        title="Prefix Change",
                        description=f"Prefix changed to `{prefix}` successfully!",
                        colour=0x55ff55
                    )
                    await message.edit(embed=conftrue)
                    await message.clear_reactions()

                elif str(reaction.emoji) == "?":
                    conffalse = discord.Embed(
                        title="Prefix Change",
                        description=f"Prefix change cancelled.",
                        colour=0xff5555
                    )
                    await message.edit(embed=conffalse)
                    await message.clear_reactions()
                        
                else:
                    await message.remove_reaction(reaction, user)

            except asyncio.TimeoutError:
                break

Also yes I am using my own confirmation system, please don't ask me to use an existing library, I like the way it is.

question from:https://stackoverflow.com/questions/65925043/how-to-reset-cooldown-of-a-command-discord-py

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can simply do

ctx.command.reset_cooldown(ctx)

You don't pass any other arguments than the Context


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...