首页 > 解决方案 > 显示命令的剩余时间

问题描述

所以我得到了这个代码:

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”而不是分钟。有人知道我该如何解决这个问题吗?它不会在控制台中显示任何错误。我想我必须在这里提到这一点。

标签: javascriptnode.jsdiscord.js

解决方案


它将是,因为除了作者的 ID 之外NaN,您没有向您的任何内容添加任何内容。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());

推荐阅读