Discord.py冷却时间装饰器未触发

问题描述

我有一个discord.py机器人程序,该机器人程序具有几个利用cooldown功能的命令,以防止用户向其发送垃圾邮件,或防止其对api进行ping操作。除了添加单个命令外,我没有更改代码中的任何内容,该命令如下(您需要了解的是,它在被调用时会发送本地图像,并且其中不应该有任何影响冷却系统。):

@commands.cooldown(rate=30,per=1,type=commands.BucketType.user)
@commands.command(name='imagereact',aliases=['ir'])
async def image_react(self,ctx,image=None,*,quote=''):
    if image == 'list':  # if user wants list of all available images.
        images = [img for x,y,img in os.walk('./images')][0]  # get all images in the ./images directory.
        images = [img[:img.find('.')] for img in images]  # remove the file extension from the name of each image.

        embed = discord.Embed(
            title="Image reaction list",description="\n".join(images),colour=0xef8b4f
        )

        embed.description += f"\n\n**Run `{ctx.command} image` to check out an image!**"

        return await ctx.send(embed=embed)

    if not image:
        return await ctx.send(f"You must pass in an image to send,{ctx.author.mention}.")

    for roots,dirs,files in os.walk('./images'):
        # get all images in the ./images directory,to be used later
        for file in files:
            ind = file.find('.')  # to take off the file extension

            if image == file[:ind]:
                with open(f"images/{file}",'rb') as img:
                    await ctx.channel.delete_messages([ctx.message])  # clean up the invoke message
                    return await ctx.send(content=quote,file=discord.File(img,image+file[ind:]))

    message = await ctx.send(f"Image `{image}` not found,{ctx.author.mention}.")
    asyncio.sleep(3)
    await ctx.channel.delete_messages([message])

在添加此命令之前,冷却系统的问题为零,并且根据需要它会引发错误。但是,在今天运行代码并添加此部分之后,对于任何命令,冷却似乎都不再起作用。我创建了一个测试命令,如下所示:

@commands.command()
@commands.cooldown(rate=3000,per=2,type=commands.BucketType.user)
async def foo(self,ctx):
    await ctx.send("bar")

,其rate为3000,只是为了测试毫秒或秒是否存在问题。不过,我可以连续快速拨打foo数十次,并且不会出错。可能是什么问题?

解决方法

rate是触发冷却前可以使用命令的次数。

per是触发冷却后等待冷却的秒数。

文档:cooldown

您的速度混合了,而且冷却时间以秒为单位

cooldown装饰器应该在command装饰器之后

所以您需要 @commands.cooldown(rate=1,per=30,type=commands.BucketType.user)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...