-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathplugin.calc.js
76 lines (63 loc) · 1.96 KB
/
plugin.calc.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
'use strict';
const child_process = require('child_process');
const config = require('./config');
const runCalc = (path, expression, onDone, onFail) => {
const process = child_process.spawn('timeout', ['0.2s', path]);
let result = '';
process.stdout.on('data', (data) => {
if (result.length <= config.calcMaxLength) {
result += data;
}
});
process.on('close', (code) => {
if (code === 0 && result && result.length <= config.calcMaxLength) {
onDone(result);
} else {
onFail();
}
});
process.stdin.write(expression + '\n');
};
module.exports = (bot, event, playerEvent, env) => {
bot.onText(/^\/(calc|clac)(@\w+)? ([^\0]+)$/, event((msg, match) => {
if (match[3].length > config.calcMaxLength) {
return;
}
if (match[1] === 'clac') {
match[3] = match[3].split('').reverse().join('');
}
runCalc('calc/eigenmath', match[3], (result) => {
// done
bot.sendMessage(
msg.chat.id,
match[1] === 'clac'
? result.split('').reverse().join('')
: result,
{
reply_to_message_id: msg.message_id,
}
);
}, () => {
// fail
runCalc('calc/eigenmath_format', match[3], (result) => {
// done
bot.sendMessage(
msg.chat.id,
match[1] === 'clac'
? result.split('').reverse().join('')
: result,
{
reply_to_message_id: msg.message_id,
}
);
}, () => {
// fail
});
});
}, 2));
env.info.addPluginHelp(
'calc',
'/calc <expression> 计算\n'
+ '/clac <noisserpxe> 算计'
);
};