-
Notifications
You must be signed in to change notification settings - Fork 13
/
plugin.zao.js
97 lines (75 loc) · 2.53 KB
/
plugin.zao.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
96
97
'use strict';
const fs = require('fs');
const config = require('./config');
module.exports = (bot, event, playerEvent, env) => {
const fd = fs.openSync('log.zao', 'a');
const zao = [];
bot.onText(/^\/zao(?!g[au]ys)(\w*)(@\w+)?(?: (.+))?$/, event((msg, match) => {
let text = '起床了!';
if (match[3]) {
text = match[3].slice(0, config.zaoMaxLength);
} else {
const chosen = env.command.tryGet(msg, 'zao' + match[1], [], false, true);
if (chosen) {
text = chosen.text;
}
}
const time = Date.now();
for (const i in zao) {
if (zao[i].from.id === msg.from.id) {
const cstNow = new Date(time + 8 * 3600 * 1000);
const cstTime = new Date(zao[i].time + 8 * 3600 * 1000);
if (
cstNow.getUTCFullYear() === cstTime.getUTCFullYear()
&& cstNow.getUTCMonth() === cstTime.getUTCMonth()
&& cstNow.getUTCDate() === cstTime.getUTCDate()
) {
zao[i].text = text;
return;
}
}
}
if (zao.length >= config.zaoCount) {
zao.shift();
}
const obj = {
from: msg.from,
time: time,
text: text,
};
zao.push(obj);
fs.write(fd, JSON.stringify(obj) + '\n', () => {
// nothing
});
}, -1));
bot.onText(/^\/zaog[au]ys(@\w+)?(?: (.+))?$/, event((msg, match) => {
let resultText = '';
let lastDate = '';
for (const i in zao) {
const cstTime = new Date(zao[i].time + 8 * 3600 * 1000);
const date = cstTime.getUTCMonth() + 1 + '/' + cstTime.getUTCDate();
if (lastDate !== date) {
lastDate = date;
resultText += date + '\n';
}
resultText += (zao[i].from.username || zao[i].from.first_name) + ' '
+ cstTime.getUTCHours() + ':' + cstTime.getUTCMinutes() + '\n'
+ zao[i].text + '\n';
}
if (resultText) {
bot.sendMessage(
msg.chat.id,
resultText,
{
reply_to_message_id: msg.message_id,
}
);
}
}, -1));
env.info.addPluginHelp(
'zao',
'/zao 签到\n'
+ '/zaoguys 列出签到者\n'
+ '/zaogays 列出签到者'
);
};