我的冷却时间事件不符合预期的 discord.js

时间:2021-02-25 03:54:17

标签: error-handling discord.js

这是我的代码:https://sourceb.in/PZuJymG20t,它给了我这个错误:

(node:25) UnhandledPromiseRejectionWarning: ReferenceError: Cannot access 'command' before initialization
    at module.exports (/home/container/events/message.js:32:23)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:25) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:25) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. 

这是一个冷却事件。

1 个答案:

答案 0 :(得分:0)

<块引用>
if(!cooldowns.has(command.name)){
    cooldowns.set(comman.name, new Discord.Collection());
}
const current_time = Data.now()
const time_stamps = cooldowns.get(command.name);
const cooldown_amount = (command.cooldown) * 1000;

if(time_stamps.has(message.author.id)){
    const expiration_time = time_stamps.get(message.author.id) + cooldown_amount;
    if(current_time < expiration_time) {
        const time_left = (expiration_time - current_time) / 1000;

        return message.reply(`Please wait ${time_left.toFixed(1)} more seconds using this ${command.name}`);
    }
}

time_stamps.set(message.author.id, current_time);
setTimeout(() => time_stamps.delete(message.author.id), cooldown_amount);

将这些代码块放在 if(command){} 函数中。

为什么会出现错误?

您在第 62 行启动了“命令”,并在第 32 行使用了它。Javascript 读取代码的方式是从上到下。所以基本上你已经在初始化之前调用了命令。

希望你理解?