Luke Timmons
Luke Timmons

Reputation: 3

Adding cooldowns to discord.py

I've been researching methods to add cooldowns to only specific commands in my bot. I am unable to find anything that works, and I'm unsure how @commands.cooldown(rate=1, per=5, bucket=commands.BucketType.user) would be implemented into this code.

import discord
import random
from pathlib import Path
from discord.ext import commands
from discord.ext.commands.cooldowns import BucketType
client = discord.Client()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('#help'):
        *command goes here*

    if message.content.startswith('#gamble'):
        *command goes here*

client.run('TOKEN')

Upvotes: 0

Views: 466

Answers (1)

Łukasz Kwieciński
Łukasz Kwieciński

Reputation: 15728

I suggest you using commands.Bot as it has all the functionality that discord.Client has + a full command system, your code rewrited would look like this:

import discord
from discord.ext import commands
# You should enable some intents
intents = discord.Intents.default()

bot = commands.Bot(command_prefix="#", intents=intents)
bot.remove_command("help") # Removing the default help command

@bot.event
async def on_ready():
    print(f"Logged in as {bot.user}")

@bot.command()
async def help(ctx): # Commands take a `commands.Context` instance as the first argument
    # ...

@bot.command()
@commands.cooldown(1, 6.0, commands.BucketType.user)
async def gamble(ctx):
    # ...

bot.run("TOKEN")

Take a look at the introduction

Upvotes: 2

Related Questions