-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathMembershipChecker.js
502 lines (414 loc) · 13.2 KB
/
MembershipChecker.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// 24443 - lib id
const LIB_PREFIX = "MembershipChecker_";
function _setupAdminPanel(){
const panel = {
// Panel title
title: "Membership checker options",
description: "use these options to check your user channel membership",
icon: "person-add",
fields: [
{
name: "chats",
title: "Chats or channels for checking",
description: "must be separated by commas",
type: "string",
placeholder: "@myChannel, @myChat",
icon: "chatbubbles"
},
{
name: "checkTime",
title: "checking delay in minutes",
description: "the bot will check the user membership for all incoming messages once at this time",
type: "integer",
placeholder: "10",
value: 20,
icon: "time"
},
{
name: "onNeedJoining",
title: "onNeedJoining command",
description: "if the user does not have a membership to ANY chat or channel, this command will be executed",
type: "string",
placeholder: "/onNeedJoining",
icon: "warning"
},
{
name: "onNeedAllJoining",
title: "onNeedAllJoining command",
description: "if the user does not have a membership to ALL chats and channels, this command will be executed",
type: "string",
placeholder: "/onNeedAllJoining",
icon: "alert"
},
{
name: "onJoining",
title: "onJoining command",
description: "if the user just received a membership for ANY chat or channel this command will be executed",
type: "string",
placeholder: "/onJoining",
icon: "person-add"
},
{
name: "onAllJoining",
title: "onAllJoining command",
description: "if the user just received a membership for ALL chats and channels this command will be executed",
type: "string",
placeholder: "/onAllJoining",
icon: "happy"
},
{
name: "onStillJoined",
title: "onStillJoined command",
description: "if the user still have membership this command will be executed. Only with check() method",
type: "string",
placeholder: "/onStillJoined",
icon: "checkmark"
},
{
name: "onError",
title: "onError command",
description: "if an error occurs during verification, this command will be executed",
type: "string",
placeholder: "/onCheckingError",
icon: "bug"
},
{
name: "debug",
title: "debug info",
description: "turn on for debug info",
type: "checkbox",
value: false,
icon: "hammer"
}
]
}
AdminPanel.setPanel({
panel_name: "MembershipChecker",
data: panel
});
}
function setup(){
_setupAdminPanel();
Bot.sendMessage("MembershipChecker Panel: Setup - OK");
}
function _getLibOptions(){
return AdminPanel.getPanelValues("MembershipChecker");
}
function _getActualChatsOnly(userData){
// admin can remove chats in any time. We need to remove it from userData too
let chatsInSettings = _getChatsArr();
let actualChats = {};
for(let chat_id in userData.chats){
let chatStillInSettings = chatsInSettings.includes(chat_id)
if(!chatStillInSettings){ continue }
actualChats[chat_id] = userData.chats[chat_id]
}
return actualChats
}
function _getUserData(){
let userData = User.getProperty(LIB_PREFIX + "Data");
if(!userData){ userData = { chats: {} } }
if(!userData.chats){ userData.chats = {} }
userData.chats = _getActualChatsOnly(userData);
return userData;
}
function _saveUserData(userData){
_debugInfo("_saveUserData: " + JSON.stringify(userData));
User.setProperty(LIB_PREFIX + "Data", userData, "json");
}
function _debugInfo(info){
if(!_getLibOptions().debug){ return }
Api.sendMessage({
text: "<b>MCLDebug</b>" +
"\n <i>turn off debug in AdminPanel</i> " +
"\n <b>message:</b> " + message +
"\n\n⚡ " + info,
parse_mode: "HTML"
})
}
function _msgIncludes(subStr){
if(!subStr){ return false }
if(subStr == ""){ return false }
return message.includes(subStr)
}
function _isInternalCommands(opts){
if(!message){ return false }
return (
_msgIncludes(LIB_PREFIX)||
_msgIncludes(opts.onJoining)||
_msgIncludes(opts.onAllJoining)||
_msgIncludes(opts.onNeedJoining)||
_msgIncludes(opts.onNeedAllJoining)||
_msgIncludes(opts.onError)
)
}
function _isHandleNeeded(){
if(!user){ return } // can check only for user
let opts = _getLibOptions();
if(!opts.chats){
throw new Error("MembershipChecker: please install chats for checking in Admin Panel");
}
// ignore internal commands
if(_isInternalCommands(opts)){
_debugInfo("ignore internal commands in handle()")
return
}
if(completed_commands_count > 0){
_debugInfo("handle can not be run on sub commands")
return
}
return true;
}
function handle(passed_options){
if(!_isHandleNeeded()){ return }
_debugInfo("handle()")
let lastCheckTime = _getUserData().lastCheckTime;
const opts = _getLibOptions();
if(_canRunHandleAgain(lastCheckTime, opts)){
return check(passed_options, true);
}
// check is not needed now
_debugInfo(
"Checking is not required since the delay time has not come yet.\nCurrent delay: " +
String(opts.checkTime) + " min"
)
}
function _isItSpamCall(lastCheckTime){
// only 1 check per 2 second for one user
if(lastCheckTime){
let duration = Date.now() - lastCheckTime;
return duration < 2000
}
return false
}
function check(passed_options, noNeedOnStillJoined){
let userData = _getUserData();
_debugInfo("check() for user Data: " + JSON.stringify(userData));
if(_isItSpamCall(userData.lastCheckTime)){ return }
userData.lastCheckTime = Date.now();
_saveUserData(userData);
_debugInfo("create task for checking");
// create task for checking
Bot.run({
command: LIB_PREFIX + "checkMemberships",
options: {
time: userData.lastCheckTime, // current time value for this checking
needStillJoinedCallback: !noNeedOnStillJoined, // if true - we need to call still joined callback
bb_options: passed_options, // customized passed options
},
// TODO: need decrease this time to 0.01
run_after: 1 // just for run in background
})
}
function checkMembership(chat_id){
if(!chat_id){ chat_id = params }
Api.getChatMember({
chat_id: chat_id,
user_id: user.telegramid,
on_result: LIB_PREFIX + "onCheckMembership " + chat_id,
on_error: LIB_PREFIX + "onError " + chat_id,
bb_options: options // here we have all options lib + admin passed_options
})
}
function getChats(){
return _getLibOptions().chats;
}
function getNotJoinedChats(){
return _getNotJoinedChats().join(", ");
}
function checkMemberships(){
let chats = _getChatsArr();
_debugInfo("run checking for " + JSON.stringify(chats));
for(let ind in chats){
// several chats
let chat_id = chats[ind];
Bot.run({
command: LIB_PREFIX + "checkMembership " + chat_id,
options: options, // passed options
// TODO: need decrease this time to 0.01
run_after: 1 // just for run in background
})
}
}
// need remove?
function _isJoined(response){
let status = response.result.status;
return ["member", "administrator", "creator"].includes(status);
}
function _isSameChatsCheckingTime(chats, needNegative){
let lastCheckTime = options.bb_options.time;
if(needNegative){ lastCheckTime = -lastCheckTime }
const sameTime = Object.values(chats).every(
value => value === lastCheckTime
);
return sameTime;
}
function _needStillJoinedCallback(userData) {
// we need callback only with check() method not in handle()
if(!options.bb_options.needStillJoinedCallback){ return false }
// callback must be installed
if(!_getLibOptions().onStillJoined){ return false }
return _isSameChatsCheckingTime(userData.chats);
}
function _runCallback(callbackName, chat_id){
const opts = _getLibOptions();
const command = opts[callbackName];
if(!command){
_debugInfo("callback is not installed: " + callbackName + ". Chat: " + chat_id +
"\n\n> " + JSON.stringify(options));
return false;
}
_debugInfo(
"run callback: " + callbackName + ">" + command + ", for chat: " + chat_id +
"\n\n> " + JSON.stringify(options)
);
Bot.run({
command: command,
options: {
result: options.result,
bb_options: options.bb_options.passed_options,
chat_id: chat_id
}
})
return true;
}
function _proccessOldChat(userData){
// it is still joined chat
_debugInfo("skip old chat");
const needCallback = _needStillJoinedCallback(userData);
if(!needCallback){
_debugInfo("still joined callback is not needed: " + JSON.stringify(options));
return true
}
return _runCallback("onStillJoined");
}
function handleMembership(chat_id, userData){
const checkTime = userData.chats[chat_id];
// we have negative time - it is not joined chat
const isOld = ( checkTime && checkTime > 0 );
// we use same time - because need to track still joined callback
userData.chats[chat_id] = options.bb_options.time;
// skip old chats
if(isOld){
_proccessOldChat(userData)
_saveUserData(userData);
return
}
// we do not need stillJoinedCallback on new chat
// set different time for new chat
userData.chats[chat_id]+= 10;
_saveUserData(userData);
let opts = _getLibOptions();
const needCallback = ( !isOld && opts.onJoining);
if(!needCallback){
_debugInfo(
"on Joining callbacks is not needed: it is old joining in: " + chat_id +
"\n\n> " + JSON.stringify(userData)
);
return
}
_runCallback("onJoining", chat_id);
// is all chats joined?
if(isMember()){
return _runCallback("onAllJoining");
}
}
function _handleNoneMembership(chat_id, userData){
userData.chats[chat_id] = -options.bb_options.time // we use negative time for not joined chats
_saveUserData(userData);
_runCallback("onNeedJoining", chat_id);
if(_needToJoinAll(userData.chats)){
_runCallback("onNeedAllJoining");
}
}
function onCheckMembership(){
let chat_id = params.split(" ")[0];
let userData = _getUserData();
userData.lastCheckTime = options.bb_options.time;
_debugInfo("check response: " + JSON.stringify(options) + "\n\n> " + JSON.stringify(userData));
if(_isJoined(options)){
_debugInfo("user is (still?) joined to " + chat_id + " chat")
return handleMembership(chat_id, userData)
}
return _handleNoneMembership(chat_id, userData)
}
function onError(){
_debugInfo("onError for " + params + " >" + JSON.stringify(options))
let opts = _getLibOptions();
if(!opts.onError){ return } // no action
opts.chat_id = params;
Bot.run({ command: opts.onError, options: options })
}
function _canRunHandleAgain(curTime){
if(!curTime){ return false }
let options = _getLibOptions();
if(!options.checkTime){
throw new Error("MembershipChecker: please install checking delay time in Admin Panel");
}
let duration = Date.now() - curTime; // in ms
duration = duration / 1000 / 60; // in minutes
return duration > parseInt(options.checkTime);
}
function _isActualMembership(chat_id){
if(!chat_id){ return false }
let userData = _getUserData()
return userData.chats[chat_id] > 0
}
function _getNotJoinedChats(){
let result;
let notJoined = [];
const chats = _getChatsArr();
for(let ind in chats){
result = _isActualMembership(chats[ind]);
if(!result){
notJoined.push(chats[ind])
}
}
return notJoined
}
function _needToJoinAll(chats){
let result;
for(let ind in chats){
result = _isActualMembership(chats[ind]);
if(result){
return false;
}
}
// same negative time for all chats
// we use negative time for not joined chats
// it will be in the end checking only
return _isSameChatsCheckingTime(chats, true);
}
function _throwErrorIfNoChats(){
if(_getLibOptions().chats){ return }
throw new Error("MembershipChecker: please install chats for checking in Admin Panel");
}
function _getChatsArr(needError){
let options = _getLibOptions();
if(!options.chats){ return [] }
let chats = options.chats.split(" ").join(""); // remove spaces
chats = chats.split(",");
if(!chats[0]){ throw new Error(error) }
return chats
}
// is member of all chats?
function isMember(chat_id){
if(chat_id){
return _isActualMembership(chat_id);
}
_throwErrorIfNoChats();
// for all chats
return ( _getNotJoinedChats().length == 0 )
}
publish({
setup: setup, // setup admin panel
check: check, // manual checking without time delay
handle: handle, // use on @ command - checking with time delay
isMember: isMember, // is member for all chats?
getChats: getChats, // get all chats for checking
getNotJoinedChats: getNotJoinedChats // get not joined chats for this user
})
on(LIB_PREFIX + "checkMemberships", checkMemberships);
on(LIB_PREFIX + "checkMembership", checkMembership);
on(LIB_PREFIX + "onCheckMembership", onCheckMembership);
on(LIB_PREFIX + "onError", onError);