开发者社区> 问答> 正文

决定24小时是否已经过去

已解决

非常简单,但是基本上我需要检查自从用户提供了一个特定的命令后是否已经过了24小时。(限速1使用/天)当他们使用该命令时,当前时间被保存和存储,因此没有问题,但是当前系统仅在日期上工作,检查是否lastDaily的日期部分小于当前日期。这导致命令在午夜重置的情况。

我希望这样,如果用户在下午2:25在2019年12月9日他们直到第二天同一时间(或下一分钟)才能使用它。

module.exports = {
  config: {
    // Command Config: Hidden for space's sake.
  },

  run: async (bot, message, args) => {
    let userData = JSON.parse(fs.readFileSync("db/userdata.json", "utf8"));

    let sender = message.author;

    // Initialise missing data if needed
    if (!userData[sender.id]) { // if there is no userdata,
      userData[sender.id] = {}; // Create it as an empty object
    }
    if (!userData[sender.id].money) {
      userData[sender.id].money = 1000;
    }
    if (!userData[sender.id].lastDaily) {
      userData[sender.id].lastDaily = "";
    }

    // Save Changes to File
    fs.writeFile("db/userdata.json", JSON.stringify(userData), err => {
      if (err) console.error(err);
    });

    var cancollect = false;
    let ld = userData[sender.id].lastDaily;

    if (ld != moment().format("L")) { // MM/DD/YY
      userData[sender.id].lastDaily = moment().format("L");
      userData[sender.id].money += 500;
      // Send Success Embed
    } else {
      // Send Failure Embed
    }
    // Save Changes Again
    fs.writeFile("db/userdata.json", JSON.stringify(userData), err => {
      if (err) console.error(err);
    });
  }
};

展开
收起
sossssss 2019-12-09 16:39:44 1541 0
1 条回答
写回答
取消 提交回答
  • 采纳回答

    如果您以以下格式存储日期月/日/年,您不能检查时差。所以,为了得到时差,如果你能以类似这样的格式存储它" 2019-12-10T10:53:53+05:30 "你可以通过moment().format当前时间戳。

    你可以这样做

    lastDate = moment().format()
    > "2019-12-09T10:53:53+05:30"
    nextDay = moment(lastDate).add(1, 'days').format()
    > "2019-12-10T10:53:53+05:30"
    

    一旦得到上述格式的时间戳,就需要将日期转换成unix格式

    lastDate = moment(lastDate).valueOf()
    > 1575869033000
    nextDay = moment(nextDay).valueOf()
    > 1575955433000
    

    现在,你可以通过这样做来获得时间上的差异

    result = moment(nextDay-lastDate).valueOf()
    > 86400000 // which is =24hrs
    

    希望有用

    2019-12-09 16:43:41
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载