Discord.py 冷却装饰器未触发
Discord.py cooldown decorator not triggering
我有一个 discord.py
机器人,它有几个使用冷却功能的命令,以防止用户向它们发送垃圾邮件,或者防止过快地 ping API。我没有更改代码中的任何内容,除了添加一个命令,如下所示(您需要了解的是它在被调用时发送本地图像,并且其中不应该有任何会影响冷却系统。):
@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
你有混合速率和 per maybe 并且冷却时间以秒为单位
cooldown
装饰器应该在command
装饰器
之后
所以你需要@commands.cooldown(rate=1, per=30, type=commands.BucketType.user)
我有一个 discord.py
机器人,它有几个使用冷却功能的命令,以防止用户向它们发送垃圾邮件,或者防止过快地 ping API。我没有更改代码中的任何内容,除了添加一个命令,如下所示(您需要了解的是它在被调用时发送本地图像,并且其中不应该有任何会影响冷却系统。):
@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
你有混合速率和 per maybe 并且冷却时间以秒为单位
cooldown
装饰器应该在command
装饰器
所以你需要@commands.cooldown(rate=1, per=30, type=commands.BucketType.user)