【发布时间】:2020-08-11 11:39:45
【问题描述】:
我有一个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,并且没有出现错误。可能是什么问题?
【问题讨论】:
标签: python discord discord.py discord.py-rewrite