显示命令剩余时间

时间:2020-01-24 14:19:36

标签: javascript node.js discord.js

所以我有以下代码:

const Discord = require("discord.js");
const config = require("../config.json");
const talkedRecently = new Set();
let coins = require("../coins.json");

module.exports.run = async (bot, message, args) => {

  if (message.channel.id === '669657436789014550') {
    if (message.content.indexOf(config.prefix) !== 0) return;

    var minute = 60000;

    if (talkedRecently.has(message.author.id)) {
      message.channel.send(`Ai fost deja într-o explorare. Mai ai de așteptat ` + Math.round((talkedRecently[message.author.id] - Date.now)/minute) + ` minute până să poți merge din nou.`);
    } else {   

      if(!coins[message.author.id]){
        coins[message.author.id] = {
          coins: 0
        };
      }


    let coinAmt = Math.floor(Math.random() * 450) + 1;
    let sCoins = coins[message.author.id].coins;

    var caut = [ //something

    ];

    coins[message.author.id] = {
      coins: coins[message.author.id].coins + coinAmt
    };

    message.channel.send(`**${message.author.tag}**,` +  (caut[Math.floor(Math.random() * caut.length)]))
      .then(() => {
        console.log(`Hopa! Cuiva îi place să meargă-n explorări!`);
      })
    }

    talkedRecently.add(message.author.id);
    setTimeout(() => {
      talkedRecently.delete(message.author.id);
    }, 18000000);
  }
}

module.exports.help = {
    name: 'caut',
    aliases: []
};

问题是它不显示用户还有多少时间,而是显示“ NaN”而不是分钟。有人知道我该如何解决吗?它不会在控制台中显示任何错误。我以为我不得不在这里提到这一点。

1 个答案:

答案 0 :(得分:0)

应该是NaN,因为除了作者的ID之外,您没有向Set添加任何内容。

我的建议是使用Map

,而不是添加一个既包含ID又包含时间戳的对象。
const talkedRecently = new Map();

if (talkedRecently.has(message.author.id)) {
  // talkedRecently.get(message.author.id) returns the Date object
}

talkedRecently.set(message.author.id, Date.now());