-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
236 lines (214 loc) · 6.35 KB
/
main.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { version } from './package.json';
import { CQWebSocket } from 'cq-websocket';
import config from './utils/config';
import CQ from './utils/CQcode';
import minimist from 'minimist';
import bilibili from './plugin/bilibili';
import twitter from './plugin/twitter';
import translate from "./plugin/translate";
import tweBBQ from "./plugin/tweBBQ";
import marshmallow from './plugin/marshmallow';
import {initialise} from "./utils/initialise";
// 常量
const setting = config.bot;
const bot = new CQWebSocket(config.cqws);
// 初始化
initialise();
Object.assign(global, {
bot,
"replyFunc" : replyMsg
});
// 开始
bilibili.bilibiliReply(replyMsg);
twitter.twitterReply(replyMsg);
tweBBQ.cookTweReply(replyMsg);
setTimeout(() => bilibili.checkBiliDynamic(replyMsg), 20000);
setTimeout(() => twitter.checkTwiTimeline(), 40000);
// 好友请求
bot.on('request.friend', context => {
let approve = setting.autoAddFriend;
const answers = setting.addFriendAnswers;
if (approve && answers.length > 0) {
const comments = context.comment.split('\n');
try {
answers.forEach((ans, i) => {
const a = /(?<=回答:).*/.exec(comments[i * 2 + 1])[0];
if (ans != a) approve = false;
});
} catch (e) {
console.error(e);
approve = false;
}
}
if (approve)
bot('set_friend_add_request', {
flag: context.flag,
sub_type: 'invite',
approve: true,
});
});
// 加群请求
const groupAddRequests = {};
bot.on('request.group.invite', context => {
if (setting.autoAddGroup)
bot('set_group_add_request', {
flag: context.flag,
approve: true,
});
else groupAddRequests[context.group_id] = context.flag;
});
// 管理员指令
bot.on('message.private', (e, context) => {
if (context.user_id != setting.admin) return;
const args = parseArgs(context.message);
// 允许加群
const group = args['add-group'];
if (group && typeof group == 'number') {
if (typeof groupAddRequests[context.group_id] == 'undefined') {
replyMsg(context, `将会同意进入群${group}的群邀请`);
// 注册一次性监听器
bot.once('request.group.invite', context2 => {
if (context2.group_id == group) {
bot('set_group_add_request', {
flag: context2.flag,
type: 'invite',
approve: true,
});
replyMsg(context, `已进入群${context2.group_id}`);
return true;
}
return false;
});
} else {
bot('set_group_add_request', {
flag: groupAddRequests[context.group_id],
type: 'invite',
approve: true,
});
replyMsg(context, `已进入群${context2.group_id}`);
delete groupAddRequests[context.group_id];
}
}
// 停止程序(利用pm2重启)
if (args.shutdown) process.exit();
});
if (setting.enablePM) {
// 私聊
bot.on('message.private', privateAndAtMsg);
}
if (setting.enableGM) {
// 群组@
bot.on('[email protected]', privateAndAtMsg);
// 群组
bot.on('message.group', groupMsg);
}
// 连接相关监听
bot
.on('socket.connecting', (wsType, attempts) => console.log(`${getTime()} 连接中[${wsType}]#${attempts}`))
.on('socket.failed', (wsType, attempts) => console.log(`${getTime()} 连接失败[${wsType}]#${attempts}`))
.on('socket.error', (wsType, err) => {
console.error(`${getTime()} 连接错误[${wsType}]`);
console.error(err);
})
.on('socket.connect', (wsType, sock, attempts) => {
console.log(`${getTime()} 连接成功[${wsType}]#${attempts}`);
if (wsType === '/api' && setting.admin > 0) {
setTimeout(() => {
bot('send_private_msg', {
user_id: setting.admin,
message: `已上线#${attempts}`,
});
}, 1000);
}
});
// connect
bot.connect();
// 通用处理
function commonHandle(e, context) {
const args = parseArgs(context.message);
if (args.help) {
replyMsg(context, 'https://github.com/Ninzore/BBQ/wiki');
return true;
}
if (args.version) {
replyMsg(context, version);
return true;
}
if (args.about) {
replyMsg(context, 'https://github.com/Ninzore/BBQ');
return true;
}
return false;
}
// 私聊以及群组@的处理
function privateAndAtMsg(e, context) {
if (commonHandle(e, context)) {
e.stopPropagation();
return;
}
}
// 群组消息处理
function groupMsg(e, context) {
if (commonHandle(e, context)) {
e.stopPropagation();
return;
}
translate.orientedTrans(context);
if (bilibili.bilibiliCheck(context) ||
twitter.twitterAggr(context) ||
marshmallow.prepare(context, replyMsg) ||
tweBBQ.complex(context, replyMsg) ||
translate.transEntry(context)
) {
e.stopPropagation();
return;
}
}
/**
* 回复消息
*
* @param {object} context 消息对象
* @param {string} message 回复内容
* @param {boolean} at 是否at发送者
*/
function replyMsg(context, message, at = false, reply = false) {
if (typeof message !== 'string' || message.length === 0) return;
if (context.message_type !== 'private') {
message = `${reply ? CQ.reply(context.message_id) : ''}${at ? CQ.at(context.user_id) : ''}${message}`;
}
switch (context.message_type) {
case 'private':
return bot('send_private_msg', {
user_id: context.user_id,
message,
});
case 'group':
return bot('send_group_msg', {
group_id: context.group_id,
message,
});
default: return;
}
}
function getTime() {
return new Date().toLocaleString();
}
function parseArgs(str, enableArray = false, _key = null) {
const m = minimist(
str
.replace(/(--\w+)(?:\s*)(\[CQ:)/g, '$1 $2')
.replace(/(\[CQ:[^\]]+\])(?:\s*)(--\w+)/g, '$1 $2')
.split(' '),
{
boolean: true,
}
);
if (!enableArray) {
for (const key in m) {
if (key == '_') continue;
if (Array.isArray(m[key])) m[key] = m[key][0];
}
}
if (_key && typeof m[_key] == 'string' && m._.length > 0) m[_key] += ' ' + m._.join(' ');
return m;
}