sametkarapinar
sametkarapinar

Reputation: 19

How can I add cooldown to my discord.py bot?

I need to add cooldown to /chat command. How can I do that? Since I pull the data from ChatGPT, I can have a rate limit on spam usage. To solve this I need to add time per user.

 @client.tree.command(name="chat", description="Have a chat with ChatGPT")
    async def chat(interaction: discord.Interaction, *, message: str):
        if interaction.user == client.user:
            return
        username = str(interaction.user)
        user_message = message
        channel = str(interaction.channel)
        logger.info(
            f"\x1b[31m{username}\x1b[0m : '{user_message}' ({channel})")
        await send_message(interaction, user_message)

I don't know what to do.

Upvotes: -1

Views: 158

Answers (1)

Chrispresso
Chrispresso

Reputation: 4131

You can use commands.cooldown decorator to do just that:

from discord.ext import commands

@commands.cooldown(1, 30, commands.BucketType.user)
async def chat(...):
 ...

this would allow each user 1 chat command per 30 seconds. You can also do it at a server level with BucketType.server instead.

Upvotes: 1

Related Questions