-
Notifications
You must be signed in to change notification settings - Fork 13
/
plugin.one.js
171 lines (139 loc) · 4.31 KB
/
plugin.one.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'use strict';
const fs = require('fs');
const readline = require('readline');
const GraphemeSplitter = require('grapheme-splitter');
const config = require('./config');
const splitter = new GraphemeSplitter();
module.exports = (bot, event, playerEvent, env) => {
const chars = [];
const findChar = (obj) => {
for (const i in chars) {
if (
obj.text === chars[i].text
&& obj.from_id === chars[i].from_id
) {
return i;
}
}
return null;
};
readline.createInterface({
input: fs.createReadStream(config.onePathChars),
}).on('line', (line) => {
const obj = JSON.parse(line);
if (obj.del) {
chars.splice(findChar(obj), 1);
} else {
chars.push(obj);
}
}).on('close', () => {
// TODO: pause bot?
});
const fdChars = fs.openSync(config.onePathChars, 'a');
const addChar = (obj) => {
const found = findChar(obj);
if (obj.del) {
if (found === null) {
return;
}
chars.splice(found, 1);
} else {
if (found !== null) {
return;
}
chars.push(obj);
}
fs.write(fdChars, JSON.stringify(obj) + '\n', () => {
// nothing
});
};
bot.onText(/^.{1,4}$/, (msg, match) => {
if (splitter.splitGraphemes(msg.text).length !== 1) {
return;
}
if (msg.forward_from) {
addChar({
text: msg.text,
from_id: msg.forward_from.id,
chat_id: msg.chat.id,
message_id: msg.message_id,
del: false,
});
} else {
addChar({
text: msg.text,
from_id: msg.from.id,
chat_id: msg.chat.id,
message_id: msg.message_id,
del: false,
});
}
});
bot.onText(/^\/one(@\w+)?(?: ([^\0]+))?$/, event((msg, match) => {
const text = splitter.splitGraphemes(match[2] || msg.reply_to_message && msg.reply_to_message.text || '');
const selected = [];
if (text.length > config.oneMaxLength) {
return;
}
for (const i in text) {
if (!text[i].match(/\s/)) {
const options = [];
for (const j in chars) {
if (chars[j].text === text[i]) {
options.push(chars[j]);
}
}
if (options.length) {
selected.push(options[Math.floor(Math.random() * options.length)]);
}
}
}
const send = () => {
if (selected.length) {
const option = selected.shift();
bot.forwardMessage(
msg.chat.id,
option.chat_id,
option.message_id
).catch((err) => {
option.del = true;
addChar(option);
send();
}).then(send);
}
};
send();
}, 1));
bot.onText(/^\/one((?!_)\w+)(@\w+)?(?: ([^\0]+))?$/, event((msg, match) => {
const text = splitter.splitGraphemes(match[3] || msg.reply_to_message && msg.reply_to_message.text || '');
const selected = [];
if (text.length > config.oneMaxLength) {
return;
}
for (const i in text) {
if (!text[i].match(/\s/)) {
const chosen = env.command.tryGet(msg, match[1], [text[i]], false, true);
if (chosen) {
selected.push(chosen);
}
}
}
const send = () => {
if (selected.length) {
const option = selected.shift();
bot.sendMessage(
msg.chat.id,
option.text
).catch((err) => {
send();
}).then(send);
}
};
send();
}, 2));
env.info.addPluginHelp(
'one',
'/one <content> 使用语录进行拆字\n'
+ '/one<trigger> <content> 使用 trigger 进行拆字'
);
};