-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (85 loc) · 2.75 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import * as fs from 'fs'
import * as dotenv from "dotenv";
import schedule from "node-schedule";
import { WebSocket } from "ws";
import { fromCSV, parseTime, toTime } from "./util.js";
dotenv.config();
const { HTTP_HOST, WS_HOST, GROUP_ID } = process.env;
const file = fs.readFileSync("./classes.csv");
const {
data: { times, classes },
schedule: mySchedule,
} = fromCSV(file);
console.log(mySchedule);
const sendMsg = (message) =>
fetch(HTTP_HOST, {
headers: new Headers({
"Content-Type": "application/json",
}),
method: "POST",
body: JSON.stringify({
action: "send_group_msg",
params: {
group_id: +GROUP_ID,
message: message,
},
echo: "test",
}),
});
mySchedule.map(({ time, message }) => {
schedule.scheduleJob(time, function () {
const msg = typeof message === "string" ? message : message();
sendMsg(msg);
});
});
const ws = new WebSocket(WS_HOST);
ws.on("open", () => {
ws.on("message", (d) => {
const data = JSON.parse(d.toString());
if (!(data["post_type"] == "message" && data["group_id"] == 1021463756))
return;
const date = new Date();
const msg = data["raw_message"];
if (msg.match(/今[天日](.*)(什么课|课表)/)) {
sendMsg(`今日课表: ${classes[(date.getDay() - 1) % 7].join(" ")}`);
return;
}
if (msg.match(/明[天日](.*)(什么课|课表)/)) {
sendMsg(`明日课表: ${classes[date.getDay() % 7].join(" ")}`);
return;
}
if (msg.match(/后[天日](.*)(什么课|课表)/)) {
sendMsg(`后天课表: ${classes[(date.getDay() + 1) % 7].join(" ")}`);
return;
}
const idxOfNextClass = times.findIndex(
(t) => parseTime(t).getTime() > date.getTime()
),
idxOfCurrentClass =
idxOfNextClass === -1 ? times.length - 1 : idxOfNextClass - 1;
if (msg.match(/什么课/)) {
if (idxOfNextClass === -1) return sendMsg("已经是最后一节课来了!");
sendMsg(
`这节是${classes[date.getDay() - 1][idxOfNextClass - 1]}(${
times[idxOfNextClass - 1]
})\n下节是${classes[date.getDay() - 1][idxOfNextClass]}(${
times[idxOfNextClass]
})`
);
return;
}
if (msg.match(/^[多好]久下课$/)) {
const timeOfCurrentClass = parseTime(times[idxOfCurrentClass]);
const classOver = new Date();
classOver.setTime(timeOfCurrentClass.getTime() + 40 * 60 * 1000);
sendMsg(toTime(classOver));
return;
}
const match = msg.match(/^叫\s*(.*?)\s+(.*)$/);
if (match) {
sendMsg(`${match[1] || "@" + data["sender"]["card"]} ${match[2]}`);
return;
}
});
});
console.log("start");