如何在每天的特定时间发送消息?
How can I send a message every day at a specific hour?
我正在尝试让机器人在特定时间写入消息。示例:
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!
我想它需要不断检查时间变量的东西。
我会使用 cron
:使用此包,您可以设置在日期与给定模式匹配时执行的函数。
构建模式时,您可以使用 *
表示它可以使用该参数的任何值执行,范围仅表示特定值:1-3, 7
表示您接受 1, 2, 3, 7
.
这些是可能的范围:
- 秒:
0-59
- 分钟:
0-59
- 小时数:
0-23
- 每月第几天:
1-31
- 月份:
0-11
(一月至十二月)
- 星期几:
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
正如 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
但还是要感谢费德里科,他救了我的命!
我正在尝试让机器人在特定时间写入消息。示例:
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!
我想它需要不断检查时间变量的东西。
我会使用 cron
:使用此包,您可以设置在日期与给定模式匹配时执行的函数。
构建模式时,您可以使用 *
表示它可以使用该参数的任何值执行,范围仅表示特定值:1-3, 7
表示您接受 1, 2, 3, 7
.
这些是可能的范围:
- 秒:
0-59
- 分钟:
0-59
- 小时数:
0-23
- 每月第几天:
1-31
- 月份:
0-11
(一月至十二月) - 星期几:
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
正如 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
但还是要感谢费德里科,他救了我的命!