【问题标题】:Discord.js Bot responds to message or command during specific time onlyDiscord.js Bot 仅在特定时间响应消息或命令
【发布时间】:2021-06-13 19:16:02
【问题描述】:

如果它是 9 之间的工作日,我如何将检查日期应用于此消息,以便 discord.js 机器人不响应 "nio down"东部时区:上午 30 点和下午 4:00? (股市时间)

请原谅我的代码。我是一个完整的菜鸟,并试图通过它的经验和执行来学习。我倾向于通过例子来学习,而不是通过书籍来学习和阅读。一旦我对这一切有了更好的了解,我就会缩小范围并通读书籍,以微调或纠正我的一些坏习惯,如果这有意义的话。

  if (message.content.toLowerCase().includes("nio down")) {
    message.channel.startTyping();
    setTimeout(() => message.channel.send("y’all got duped. nio CEO on the run now"), 56500);
    setTimeout(() => message.channel.send("pookie made more money hustling"), 69500);
    message.channel.stopTyping(true);
  } else

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    您可以使用Date 对象。访问消息的createdTimestamp属性,使用getDay()方法判断是否是工作日:

    const { createdTimestamp } = message;
    const date = new Date(createdTimestamp);
    const day = date.getDay();
    
    // The getDay() method returns the day of the week for the specified date according to local time, where 0 represents Sunday
    if (day > 0 && day < 6) {
      console.log("weekday");
      const UTCHours = date.getUTCHours();
      const ESTHours = UTCHours - 5.0;
    
      if (ESTHours == 9) {
        const minutes = date.getUTCMinutes();
    
        if (minutes < 30) {
          console.log("Not in time", ESTHours, minutes);
        }
      } else if (ESTHours > 10 || ESTHours < 16) {
        console.log("In time", ESTHours);
      } else {
        console.log("Not in time", ESTHours);
      }
    } else {
      console.log("No weekday");
    }
    

    东部时区检查更复杂。 Native Date 对象只知道两个时区,UTC 和用户的区域设置时区(但是您可以提取的有关区域设置时区的信息量是有限的)。您可以在 UTC 中工作并从消息对象中减去/添加必要的小时数。

    或者您查看更适合处理此类问题的日期库之一,例如 Luxon 或 Day.js。

    编辑

    您可以通过getUTCHours 方法获取UTC 时间。然后减去时间差,检查时间是否在9:30 - 16:00的范围内。

    【讨论】:

    • 我不介意 UTC 时区如何将其添加到上面?
    猜你喜欢
    • 2021-04-02
    • 1970-01-01
    • 2021-12-01
    • 2018-08-26
    • 2020-04-07
    • 2022-01-25
    • 1970-01-01
    • 2022-10-23
    • 2020-11-26
    相关资源
    最近更新 更多