环境准备: 部署完成的Yunzai,未部署请点击这里
点击阅读全文查看完整代码
Windows
复制以下代码,在./Yunzai/plugins/example
中新建文件,命名为XXX.js
将以下代码复制到XXX.js
,并修改该项
// 需要推送消息的群组
let Gruop = [12345, 114514];
完整代码如下
import fetch from "node-fetch";
import schedule from "node-schedule";
import moment from "moment";
// 定义路径和默认回复消息
const _path = process.cwd();
const Textreply = '⭐每日任务推送⭐';
const imgreply = 'https://api.t1qq.com/api/sky/gy/sc/scsky.php';
// 需要推送消息的群组
let Gruop = [12345, 114514];
// 定义睡眠函数,用于控制消息发送频率
async function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
// 定义需要发送消息的小时数组
const sendHours = [1, 6, 9, 11, 13, 15, 17, 19, 21, 23];
// 设置每个小时触发执行任务的定时任务
schedule.scheduleJob('0 0 * * * *', async () => {
let time = moment(Date.now()).format("YYYY-MM-DD HH:mm:ss");
let hours = (new Date(time).getHours());
// 检查当前小时是否在需要发送消息的小时数组中
if (sendHours.includes(hours)) {
// 构建消息
let msg = [
Textreply ? Textreply : "",
imgreply ? segment.image(imgreply) : "",
];
console.log(msg);
// 发送消息给每个群组
for (var key of Gruop) {
Bot.pickGroup(key * 1).sendMsg(msg);
await sleep(10000);
}
}
});
// 添加新的定时任务来发送提醒消息
const reminderTimes = ["07:55", "09:55", "11:55", "15:55", "17:55", "19:55"];
reminderTimes.forEach(time => {
schedule.scheduleJob(`0 55 ${time.split(":")[0]} * * *`, async () => {
// 获取下一个小时的时间
const nextHour = parseInt(time.split(":")[0]) + 1;
const nextTime = `${nextHour < 10 ? "0" + nextHour : nextHour}:00`;
// 构建提醒消息
const reminderMsg = `还有五分钟就到${nextTime}点啦~\n快去老奶奶那干饭呀,\n记得带好饭盆哦~`;
// 发送提醒消息给每个群组
for (var key of Gruop) {
Bot.pickGroup(key * 1).sendMsg(reminderMsg);
await sleep(10000);
}
});
});
Linux云服务器
在Windows上安装FinalSheel(远程链接服务器),并通过百度完成配置
连接成功后打开Yunzai所在的目录,打开./plugin/example
右键空白处,点击新建-文件,命名为XXX.js
将上面的代码复制到XXX.js
保存!