如何在 cog 文件中实现 reset_cooldown?
Posted
技术标签:
【中文标题】如何在 cog 文件中实现 reset_cooldown?【英文标题】:How I implement reset_cooldown in a cog file? 【发布时间】:2021-03-19 01:07:33 【问题描述】:我正在尝试执行一个命令,让我的朋友可以通过将他们移动到一个特殊频道来互相拖钓。
我设置了一个冷却时间,这样他们就不会滥用它,但是知道,当我试图将我的代码重写为一个 cog 版本时,我无法处理 @command.after_invoke
所以如果作者是我或某个特别的人,我无法重置冷却时间。
我尝试在@command.error
中执行此操作,但如果我在那里重置它,我将无法重新激活它
我的问题是我无法弄清楚我应该如何将其转换为 cog 版本:
@m.after_invoke
async def reset_cooldown(ctx):
if ctx.message.author.id==1234567890:
m.reset_cooldown(ctx)
我会把 cog 文件放在我的函数中,它可以工作,但是我无法重置冷却时间:
import discord
from datetime import datetime
protected=[1234567890]
class move(commands.Cog):
def __init__(self,bot):
self.bot=bot
@commands.command(pass_context=True,aliases=['m'])
@commands.cooldown(1,300,commands.BucketType.user)
async def move(self,ctx,member:discord.Member):
global protected
message=ctx.message
delete_module=self.bot.get_cog('delete') #this will erase the message so the victim won tknow who s doing teh prank
await delete_module.delete(ctx,ctx.author)
canal_sclav=list(filter(lambda x: x.name=='sclav',ctx.guild.voice_channels))[0] # this is the channel where they llbe sent
reports=list(filter(lambda x: x.id==1234567890,member.guild.channels))[0] # this is a channel in which i track the activity
current_time = datetime.now().strftime("%H:%M:%S")
embed=discord.Embed(description=message.author.mention+'l-a maltrafoxat pe '+member.mention+ ' la ora '+current_time,color=discord.Colour.red()) # this is the sign that someone was abused
######################## daca autorul nu e conectat -- if the author is not connected
if ctx.author.voice==None:
embed=discord.Embed(description=message.author.mention+'Conecteza-te fiti-ar alifia de ras',color=discord.Colour.gold())
await ctx.send(embed=embed)
######################## daca beleste pe cine nu trebuie -- this is for the protected one
elif member.id in protected:
embed=discord.Embed(description=message.author.mention+' altadata, suge-o acum',color=discord.Colour.gold())
await ctx.send(embed=embed)
######################## daca membrul nu e conectat -- if member is not connected
elif member.voice==None:
embed=discord.Embed(description=message.author.mention+' nu poate fi babardit incearca alt fraier',color=discord.Colour.gold())
await ctx.send(embed=embed)
######################## daca membrul e pe sclav -- if he is abused
elif member.voice.channel==canal_sclav:
embed=discord.Embed(description=message.author.mention+' si-o ia in cur schimba prostu',color=discord.Colour.gold())
await ctx.send(embed=embed)
######################## daca e ok -- if it is ok
else:
await reports.send(embed=embed)
await member.move_to(canal_sclav)
embed=discord.Embed(description=member.mention+' este abuzat',color=discord.Colour.gold())
await ctx.send(embed=embed)
# for error handling
@move.error
async def move_error(self,ctx,error):
global protected
retry_after = round(error.retry_after)
text=ctx.message.author.mention+' ti-o pot suge in '+str(round(error.retry_after))+'s'
embed=discord.Embed(description=text,color=discord.Colour.magenta())
await ctx.send(embed=embed)
return await ctx.send(embed=embed
)
# f"\NHOURGLASS Command is on cooldown, try again after retry_after seconds"
# HERE I TRIED TO DO IT BUT I CAN T FIND ENOUGH RESOURCES
@move.afetr_invoke
async def reset_cooldown(ctx):
if ctx.message.author.id in protected:
move.reset_cooldown(ctx)
def setup(bot):
bot.add_cog(move(bot))
【问题讨论】:
【参考方案1】:我终于找到了答案;应该添加的重置功能是这样的:
async def cog_after_invoke(self,ctx):
if ctx.message.author.id in protejati:
ctx.command.reset_cooldown(ctx)
【讨论】:
以上是关于如何在 cog 文件中实现 reset_cooldown?的主要内容,如果未能解决你的问题,请参考以下文章