【问题标题】:How can I send a message every day at a specific hour?如何每天在特定时间发送消息?
【发布时间】:2019-05-18 03:49:15
【问题描述】:

我正在尝试让机器人在特定时间编写消息。示例:

const Discord = require("discord.js");
const client = new Discord.Client();
client.on("ready", () => {
  console.log("Online!");
});
var now = new Date();
var hour = now.getUTCHours();
var minute = now.getUTCMinutes();
client.on("message", (message) => {
  if (hour === 10 && minute === 30) {
    client.channels.get("ChannelID").send("Hello World!");
  }
});

不幸的是,它只有在我触发另一个命令时才有效,例如:

if (message.content.startsWith("!ping")) {
  message.channel.send("pong!");
}
my message: !ping (at 10:10 o'clock)
-> pong!
-> Hello World!

我猜它需要不断检查时间变量的东西。

【问题讨论】:

    标签: javascript discord.js


    【解决方案1】:

    正如 Federico 所说,这是解决这个问题的正确方法,但语法已经改变,现在随着 discord.js (v12) 的新更新,它会是这样的:

        // Getting Discord.js and Cron
        const Discord = require('discord.js');
        const cron = require('cron');
                
        // Creating a discord client
        const client = new Discord.Client();
            
        // We need to run it just one time and when the client is ready
        // Because then it will get undefined if the client isn't ready
        client.once("ready", () => {
          console.log(`Online as ${client.user.tag}`);
            
          let scheduledMessage = new cron.CronJob('00 30 10 * * *', () => {
          // This runs every day at 10:30:00, you can do anything you want
          // Specifing your guild (server) and your channel
             const guild = client.guilds.cache.get('id');
             const channel = guild.channels.cache.get('id');
             channel.send('You message');
            });
                
            // When you want to start it, use:
            scheduledMessage.start()
        };
    
    // You could also make a command to pause and resume the job
    

    但仍然感谢 Federico,他救了我的命!

    【讨论】:

    【解决方案2】:

    我会使用cron:使用此包,您可以设置要在日期与给定模式匹配时执行的函数。
    在构建模式时,您可以使用* 表示可以使用该参数的任何值执行它,并使用范围以仅表示特定值:1-3, 7 表示您接受1, 2, 3, 7

    这些是可能的范围:

    • 秒:0-59
    • 分钟:0-59
    • 小时:0-23
    • 日期:1-31
    • 月份:0-11(1 月至 12 月)
    • 星期几:0-6(周日至周六)

    这是一个例子:

    var cron = require("cron");
    
    function test() {
      console.log("Action executed.");
    }
    
    let job1 = new cron.CronJob('01 05 01,13 * * *', test); // fires every day, at 01:05:01 and 13:05:01
    let job2 = new cron.CronJob('00 00 08-16 * * 1-5', test); // fires from Monday to Friday, every hour from 8 am to 16
    
    // To make a job start, use job.start()
    job1.start();
    // If you want to pause your job, use job.stop()
    job1.stop();
    

    在你的情况下,我会这样做:

    const cron = require('cron');
    
    client.on('message', ...); // You don't need to add anything to the message event listener
    
    let scheduledMessage = new cron.CronJob('00 30 10 * * *', () => {
      // This runs every day at 10:30:00, you can do anything you want
      let channel = yourGuild.channels.get('id');
      channel.send('You message');
    });
    
    // When you want to start it, use:
    scheduledMessage.start()
    // You could also make a command to pause and resume the job
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-25
      • 2019-07-27
      • 2022-01-25
      • 2018-06-25
      • 2020-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多