-
Notifications
You must be signed in to change notification settings - Fork 13
/
plugin.welcome.js
72 lines (56 loc) · 1.91 KB
/
plugin.welcome.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
'use strict';
const config = require('./config');
module.exports = (bot, event, playerEvent, env) => {
const welcomePairs = {};
const leavePairs = {};
const gen = (command, pairs, msg, user) => {
const name1 = user.username
? '@' + user.username
: user.first_name;
if (pairs[msg.chat.id] && pairs[msg.chat.id] !== name1) {
const name2 = pairs[msg.chat.id];
delete pairs[msg.chat.id];
env.command.get(msg, command, [name1, name2]);
} else {
delete pairs[msg.chat.id];
pairs[msg.chat.id] = name1;
if (Object.keys(pairs).length > config.welcomeMaxEntry) {
for (const i in pairs) {
delete pairs[i];
break;
}
}
env.command.get(msg, command, [name1]);
}
};
const welcomeEvent = event((msg, match) => {
for (const i in msg.new_chat_members) {
if (msg.new_chat_members[i].username === config.threesomeUsername) {
env.info.start(msg);
} else {
gen('welcome', welcomePairs, msg, msg.new_chat_members[i]);
}
}
}, -1);
const leaveEvent = event((msg, match) => {
if (msg.left_chat_member.username !== config.threesomeUsername) {
gen('leave', leavePairs, msg, msg.left_chat_member);
}
}, -1);
bot.on('message', (msg) => {
if (config.ban[msg.from.id] || config.threesomeSilent[msg.chat.id]) {
return;
}
if (msg.new_chat_members) {
welcomeEvent(msg, ['<new chat member>']);
}
if (msg.left_chat_member) {
leaveEvent(msg, ['<left chat member>']);
}
});
env.info.addPluginHelp(
'welcome',
'<new chat member> 欢迎新人\n'
+ '<left chat member> 有人跑了'
);
};