即使没有冷却时间,为什么我的冷却时间也会发送?不和谐.py

时间:2021-04-18 05:43:24

标签: python discord.py

所以我试图为我的一个命令创建一个冷却时间。

(命令)

@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)

我的问题是,尽管命令没有发送两次,但冷却文本会在消息之后立即发送。

(命令错误事件)

@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

您可能需要的额外信息:

(打开json文件使用)

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

(关闭json文件并保存)

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)

(进口)

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

问题示例:

<块引用>

用户的当前余额

<块引用>

硬币: 689

<块引用>

此命令正在冷却中,您可以在 5.0 秒内使用它。

如果您需要有关该计划的更多信息,请告诉我!

1 个答案:

答案 0 :(得分:0)

所以事实证明问题不在列出的代码中。

on_message() 真的很喜欢打破常规,FAQ (https://discordpy.readthedocs.io/en/latest/faq.html#why-does-on-message-make-my-commands-stop-working) 中有更多信息