【问题标题】:my cooldown event dosent work as intented discord.js我的冷却事件没有按预期工作 discord.js
【发布时间】:2021-05-27 10:30:54
【问题描述】:

这是我的代码: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. 

这是一个冷却事件。

【问题讨论】:

  • 请将您的代码直接发布到问题中,无需添加将来可能会失效的额外网址。
  • 请阅读How to Ask

标签: error-handling discord.js


【解决方案1】:
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 读取代码的方式是从上到下。所以基本上你在初始化之前已经调用了命令。

希望你能理解?

【讨论】:

  • 解释你的答案会很有帮助,谢谢
猜你喜欢
  • 2021-05-20
  • 1970-01-01
  • 2010-10-08
  • 2015-11-17
  • 2010-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多