Skip to content
Advertisement

Why is my cooldown sending even when there is no cooldown? discord.py

So I am attempting to create a cooldown for one of my commands.

(The command)

@commands.cooldown(1, 5, commands.BucketType.user)
async def balance(ctx):

    await load_jsons(ctx)

    coins = users_w[str(user.id)]["wallet"]

    em = discord.Embed(title = f"{user.name}'s Current Balance", color = discord.Color.dark_gold())
    em.add_field(name = "Coins:", value = coins)
    await ctx.send(embed = em)

    await close_jsons(ctx)

My issue is that the cooldown text is sent right after the message despite the command not being sent twice.

(The command error event)

@bot.event
async def on_command_error(ctx, error):
    print(error)
    if isinstance(error, commands.CommandOnCooldown):
        await ctx.channel.send(f"This command is on cooldown, you can use it in {round(error.retry_after, 2)} seconds.")
    else:
        return

Extra information you may need:

(Opens json files for use)

async def load_jsons(ctx):
    await open_inventory(ctx.author)
    await open_factories(ctx.author)
    await open_account(ctx.author)

    global users_i
    users_i = await get_inv_data()
    global users_w
    users_w = await get_bank_data()
    global users_f
    users_f = await get_fact_data()

    global user
    user = ctx.author

    print(f"Opened jsons for {ctx.author.name}.")

(Closes json files and saves them)

async def close_jsons(ctx):
    with open("factorydata.json", "w") as f:
        json.dump(users_f, f)
    with open("inv.json", "w") as f:
        json.dump(users_i, f)
    with open("wallet.json", "w") as f:
        json.dump(users_w, f)

(Imports)

import discord
from discord.ext import commands
import datetime
import random

import math

import json

from urllib import parse, request
import re

from dotenv import load_dotenv
import os

Example of the issue:

User’s Current Balance

Coins: 689

This command is on cooldown, you can use it in 5.0 seconds.

If you need any more information about the program please let me know!

Advertisement

Answer

So it turns out the issue was not in the code listed.

on_message() really likes to break things and there is more information on the FAQ (https://discordpy.readthedocs.io/en/latest/faq.html#why-does-on-message-make-my-commands-stop-working)

Advertisement