ExaNori
ExaNori

Reputation: 15

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}>")

I need to know how to add a cooldown to this event so people dont spam gn or goodnight

Upvotes: 1

Views: 971

Answers (2)

ExaNori
ExaNori

Reputation: 15

    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)

This is a good example of creating the embed in a function to make your code cleaner for error catching, and making sure that this isnt able to be spammed with the cooldown mapping.

Upvotes: 0

Alvis矜
Alvis矜

Reputation: 133

Discord.py (Rewrite) How to get cooldowns working with on_message event?

I would recommend you to check out this post.

Upvotes: 3

Related Questions