繁体   English   中英

我如何为此事件添加冷却时间或速率限制? 不和谐.py

[英]How do i add a cooldown or a ratelimit to this event? discord.py

@commands.Cog.listener()
    async def on_message(self, message):
        user_in  = message.content.lower()
        if "gn" in  user_in.split(" ") or "good night" in user_in :
            if message.author.bot: return
            if not message.guild: return
            await message.channel.send(f"Good Night, <@{message.author.id}>")

我需要知道如何来冷却时间加入到这个活动让人们dont垃圾邮件gn或晚安

    async def create_embed(self, ctx, err):
        embed = discord.Embed(title=f"Error Caught!",color=discord.Colour.red())
        embed.add_field(name=f"Info on the error.", value=f"{err}")
        embed.set_thumbnail(url=self.bot.user.avatar_url_as(static_format="png"))
        message_cooldown = commands.CooldownMapping.from_cooldown(1.0, 60.0, commands.BucketType.user)
        bucket = message_cooldown.get_bucket(ctx.message)
        retry_after = bucket.update_rate_limit()
        if retry_after:
            return
        else:
            await ctx.send(embed=embed)

这是一个很好的例子,它在函数中创建嵌入,使您的代码更清晰以捕获错误,并确保它不会被冷却映射发送垃圾邮件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM