So I am attempting to create a cooldown for one of my commands.
(The command)
JavaScript
x
13
13
1
@commands.cooldown(1, 5, commands.BucketType.user)
2
async def balance(ctx):
3
4
await load_jsons(ctx)
5
6
coins = users_w[str(user.id)]["wallet"]
7
8
em = discord.Embed(title = f"{user.name}'s Current Balance", color = discord.Color.dark_gold())
9
em.add_field(name = "Coins:", value = coins)
10
await ctx.send(embed = em)
11
12
await close_jsons(ctx)
13
My issue is that the cooldown text is sent right after the message despite the command not being sent twice.
(The command error event)
JavaScript
1
8
1
@bot.event
2
async def on_command_error(ctx, error):
3
print(error)
4
if isinstance(error, commands.CommandOnCooldown):
5
await ctx.channel.send(f"This command is on cooldown, you can use it in {round(error.retry_after, 2)} seconds.")
6
else:
7
return
8
Extra information you may need:
(Opens json files for use)
JavaScript
1
17
17
1
async def load_jsons(ctx):
2
await open_inventory(ctx.author)
3
await open_factories(ctx.author)
4
await open_account(ctx.author)
5
6
global users_i
7
users_i = await get_inv_data()
8
global users_w
9
users_w = await get_bank_data()
10
global users_f
11
users_f = await get_fact_data()
12
13
global user
14
user = ctx.author
15
16
print(f"Opened jsons for {ctx.author.name}.")
17
(Closes json files and saves them)
JavaScript
1
8
1
async def close_jsons(ctx):
2
with open("factorydata.json", "w") as f:
3
json.dump(users_f, f)
4
with open("inv.json", "w") as f:
5
json.dump(users_i, f)
6
with open("wallet.json", "w") as f:
7
json.dump(users_w, f)
8
(Imports)
JavaScript
1
15
15
1
import discord
2
from discord.ext import commands
3
import datetime
4
import random
5
6
import math
7
8
import json
9
10
from urllib import parse, request
11
import re
12
13
from dotenv import load_dotenv
14
import os
15
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)