This repository has been archived by the owner on Feb 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
command.py
856 lines (777 loc) · 24.1 KB
/
command.py
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
bot command
"""
import logging
import pickle
from datetime import datetime, time, timedelta
from typing import List
from telegram import ForceReply
from telegram import ParseMode
from telegram import Update, Bot, MessageEntity
from telegram import User as TgUser
from telegram.chatmember import ChatMember
from telegram.ext import ConversationHandler, Job, JobQueue
from telegram.ext.dispatcher import run_async
from telegram.ext.filters import Filters
from telegram.chat import Chat
from admin import update_admin_list, update_ban_list
from config import CHAT_DATA_FILE, USER_DATA_FILE, CONV_DATA_FILE
from constant import START_MSG, ADD_ADMIN_OK_MSG, BOT_NO_ADMIN_MSG, BOT_IS_ADMIN_MSG, ID_MSG, ADMIN_FORMAT, \
GET_ADMINS_MSG, GROUP_FORMAT, BOT_STOP_MSG, INFO_MSG, GLOBAL_BAN_FORMAT, allow_setting, OK, NO, BANWORD_ERROR, \
BANWORD_FORMAT, GET_BANWORDS_MSG, SET_OK_MSG, BAN_STATE, START_TIME_MSG, STOP_TIME_MSG, UserData, \
ARG_ERROR_MSG, USERID_ERROR_MSG, RunState, NO_INFO_MSG, NUM_ERROR, ChatData, OpenState, OPITON_ERROR, BOT_RUN_MSG, \
CLEANWARN_MSG, NO_RUN_MSG, LINK_FORMAT, GLOBAN_BAN_MSG, UNGLOBAN_BAN_MSG, MAXWARN_MSG, TIMEfLOOD_MSG, FLOOD_MSG, \
SETTING_MSG, BANWORD_MSG, UNBANWORD_MSG, LANG_MSG, KICK_MSG, LOCK_MSG, UNLOCK_MSG, TIMER_MSG, DELETE_TIMER_MSG, \
LISTTIMER_MSG, UNAUTOLOCK_MSG, LANG_DICT
from module import DBSession
from module.group import Group
from module.user import User
from tool import command_wrap, check_admin, word_re, get_user_data, get_chat_data, get_conv_data, kick_user, \
messaage_warp, check_run, time_send_msg, save_jobs
@command_wrap()
@run_async
def start(bot, update):
"""
send start info
"""
bot.send_message(chat_id=update.message.chat_id, text=START_MSG)
session = DBSession()
user = session.query(User).filter_by(id=update.message.from_user['id']).first()
if user is None:
session.add(User(id=update.message.from_user['id']))
session.commit()
session.close()
@command_wrap()
@run_async
def ping(bot, update):
"""
send start info
"""
pingtime = datetime.now().timestamp() - update.message.date.timestamp()
bot.send_message(chat_id=update.message.chat_id, text=pingtime)
@command_wrap(name="add", pass_args=True)
@run_async
@check_admin()
def add_admin(bot, update, args):
"""
add admin
:param bot:
:param update:
:param args:
:return:
"""
if not len(args):
bot.send_message(chat_id=update.message.chat_id, text=ARG_ERROR_MSG)
return
session = DBSession()
for user_id in args:
if not user_id.isdigit():
bot.send_message(chat_id=update.message.chat_id, text=USERID_ERROR_MSG)
return
user = User(id=user_id, isadmin=True)
session.merge(user)
session.commit()
session.close()
update_admin_list()
bot.send_message(chat_id=update.message.chat_id, text=ADD_ADMIN_OK_MSG)
@command_wrap(pass_chat_data=True)
@run_async
@check_admin()
def run(bot, update, chat_data):
"""Run bot filter function
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
if chat_data.get(ChatData.RUN):
bot.send_message(chat_id=update.message.chat_id, text=BOT_RUN_MSG)
return
bot_id = bot.id
group_info = bot.get_chat_member(update.message.chat_id, bot_id)
if group_info['status'] != ChatMember.ADMINISTRATOR:
bot.send_message(chat_id=update.message.chat_id, text=BOT_NO_ADMIN_MSG)
return
session = DBSession()
if update.message.chat.type == Chat.SUPERGROUP:
group_link = bot.export_chat_invite_link(chat_id=update.message.chat_id)
else:
group_link = ""
group = Group(id=update.message.chat_id, title=update.message.chat.title, link=group_link)
session.merge(group)
session.commit()
session.close()
chat_data[ChatData.RUN] = True
bot.send_message(chat_id=update.message.chat_id, text=BOT_IS_ADMIN_MSG)
@command_wrap(pass_args=True)
@check_admin()
@check_run()
@run_async
def clearwarns(bot, update, args):
"""
clearn a user warn
:param bot:
:param update:
:param args:
:type bot: Bot
:type update: Update
:return:
"""
user_list = []
for _ in args:
if _.isdigit():
user_list.append(TgUser(id=_, first_name="temp", is_bot=False))
if update.message.reply_to_message:
user_list.append(update.message.reply_to_message.from_user['id'])
if update.message.entities:
for entity in update.message.entities:
if entity.type == MessageEntity.TEXT_MENTION:
user_list.append(entity.user['id'])
if not len(user_list):
bot.send_message(chat_id=update.message.chat_id, text=ARG_ERROR_MSG)
return
user_data = get_user_data()
for user in user_list:
user_data[user.id][UserData.WARN] = 0
bot.send_message(chat_id=update.message.chat_id, text=CLEANWARN_MSG)
@command_wrap(name='id')
@run_async
def get_id(bot, update):
"""
get user id
:param bot:
:param update:
:return:
"""
bot.send_message(chat_id=update.message.chat_id,
text=ID_MSG.format(user_id=update.message.from_user['id'],
group_id=update.message.chat_id
)
)
@command_wrap()
@run_async
def admins(bot, update):
"""
get group admin info
:param bot:
:type bot: Bot
:param update:
:return:
"""
admin_list = bot.get_chat_administrators(chat_id=update.message.chat_id)
createors = ""
adminors = ""
for admin in admin_list:
if admin['status'] == ChatMember.CREATOR:
createors = createors + ADMIN_FORMAT.format(username=admin.user.full_name, user_id=admin.user.id)
if admin['status'] == ChatMember.ADMINISTRATOR:
adminors = adminors + ADMIN_FORMAT.format(username=admin.user.full_name, user_id=admin.user.id)
bot.send_message(chat_id=update.message.chat_id, text=GET_ADMINS_MSG.format(creators=createors, admins=adminors),
parse_mode=ParseMode.MARKDOWN)
@command_wrap(name="groups")
@check_admin()
@run_async
def get_groups(bot, update):
"""
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
session = DBSession()
groups = session.query(Group).all()
if groups is None:
bot.send_message(chat_id=update.message.chat_id, text=NO_INFO_MSG)
session.close()
return
ret_text = ""
for group in groups:
ret_text = ret_text + GROUP_FORMAT.format(group_title=group.title, group_id=group.id,
group_link=group.link)
bot.send_message(chat_id=update.message.chat_id, text=ret_text, parse_mode=ParseMode.MARKDOWN)
session.close()
@command_wrap(pass_chat_data=True)
@check_admin()
@check_run()
@run_async
def stop(bot, update, chat_data):
"""
Stop bot filer function
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
if not chat_data.get(ChatData.RUN):
bot.send_message(chat_id=update.message.chat_id, text=NO_RUN_MSG)
return
chat_data[ChatData.RUN] = False
bot.send_message(chat_id=update.message.chat_id, text=BOT_STOP_MSG)
@command_wrap()
@check_admin()
@check_run()
@run_async
def link(bot, update):
"""
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
# TODO 更新数据库
if not update.message.chat.type == Chat.SUPERGROUP:
bot.send_message(chat_id=update.message.chat_id, text="not supergroup")
return
group_link = bot.export_chat_invite_link(update.message.chat_id)
bot.send_message(chat_id=update.message.chat_id, parse_mode=ParseMode.MARKDOWN,
text=LINK_FORMAT.format(link=group_link))
@command_wrap()
@run_async
def info(bot, update):
"""
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
send_user = update.message.from_user
bot.send_message(chat_id=update.message.chat_id,
text=INFO_MSG.format(username=send_user.username, user_id=send_user.id))
@command_wrap(pass_args=True)
@check_admin()
@run_async
def globalban(bot, update, args):
"""
globalban a user
:param args:
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
ban_user_list = []
for _ in args:
if _.isdigit():
ban_user_list.append(TgUser(id=_, first_name="not get", is_bot=False))
for entity in update.message.parse_entities(MessageEntity.TEXT_MENTION).keys():
ban_user_list.append(entity.user)
if not len(ban_user_list):
bot.send_message(chat_id=update.message.chat_id, text=ARG_ERROR_MSG)
return
bot.send_message(chat_id=update.message.chat_id, text=GLOBAN_BAN_MSG)
ban_user(user_list=ban_user_list, ban=True)
update_ban_list()
@command_wrap(pass_args=True)
@check_admin()
@run_async
def unglobalban(bot, update, args):
"""
cancel a user globalbanr
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
ban_user_list = []
for _ in args:
if _.isdigit():
ban_user_list.append(TgUser(id=_, first_name="not get", username="not get", is_bot=False))
for entity in update.message.parse_entities(MessageEntity.TEXT_MENTION).keys():
ban_user_list.append(entity.user)
if not len(ban_user_list):
bot.send_message(chat_id=update.message.chat_id, text=ARG_ERROR_MSG)
return
ban_user(user_list=ban_user_list, ban=False)
bot.send_message(chat_id=update.message.chat_id, text=UNGLOBAN_BAN_MSG)
update_ban_list()
@command_wrap()
@check_admin()
@run_async
def globalban_list(bot, update):
"""
show globalban user list
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
session = DBSession()
datas = session.query(User).filter_by(isban=True).all()
ret_text = ""
for data in datas:
ret_text = ret_text + GLOBAL_BAN_FORMAT.format(user_id=data.id)
session.close()
if ret_text == "":
bot.send_message(chat_id=update.message.chat_id, text=NO_INFO_MSG)
return
bot.send_message(chat_id=update.message.chat_id, text=ret_text, parse_mode=ParseMode.MARKDOWN)
@command_wrap(pass_chat_data=True, pass_args=True)
@check_admin()
@check_run()
@run_async
def maxwarns(bot, update, args, chat_data):
"""
set maxwarn num
:param args:
:param chat_data:
:param bot:
:type bot: Bot
:param update:
:type update: Update
:type chat_data: dict
:return:
"""
if len(args) == 0 and chat_data.get(ChatData.MAXWARN):
chat_data.pop(ChatData.MAXWARN)
bot.send_message(chat_id=update.message.chat_id, text=SET_OK_MSG)
return
if not args[0].isdigit():
bot.send_message(update.message.chat_id, text=NUM_ERROR)
chat_data[ChatData.MAXWARN] = int(args[0])
bot.send_message(chat_id=update.message.chat_id, text=MAXWARN_MSG.format(num=args[0]))
@command_wrap(pass_chat_data=True, pass_args=True)
@check_admin()
@check_run()
@run_async
def settimeflood(bot, update, args, chat_data):
"""
set flood limit time
:param args:
:param chat_data:
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
if len(args) == 0 and chat_data.get(ChatData.FLOOD_TIME):
chat_data.pop(ChatData.FLOOD_TIME)
bot.send_message(chat_id=update.message.chat_id, text=SET_OK_MSG)
return
if not args[0].isdigit():
bot.send_message(update.message.chat_id, text=NUM_ERROR)
chat_data[ChatData.FLOOD_TIME] = int(args[0])
bot.send_message(chat_id=update.message.chat_id, text=TIMEfLOOD_MSG.format(num=args[0]))
@command_wrap(pass_chat_data=True, pass_args=True)
@check_admin()
@check_run()
@run_async
def setflood(bot, update, args, chat_data):
"""
:param args:
:param chat_data:
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
if len(args) == 0 and chat_data.get(ChatData.FLOOD_NUM):
chat_data.pop(ChatData.FLOOD_NUM)
bot.send_message(chat_id=update.message.chat_id, text=SET_OK_MSG)
return
if not args[0].isdigit():
bot.send_message(update.message.chat_id, text=NUM_ERROR)
chat_data[ChatData.FLOOD_NUM] = int(args[0])
bot.send_message(chat_id=update.message.chat_id, text=FLOOD_MSG.format(num=args[0]))
@command_wrap(pass_chat_data=True, pass_args=True)
@check_admin()
@check_run()
@run_async
def setmaxmessage(bot, update, args, chat_data):
"""
:param args:
:param chat_data:
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
if len(args) == 0 and chat_data.get(ChatData.MAXFLOOD):
chat_data.pop(ChatData.MAXFLOOD)
bot.send_message(chat_id=update.message.chat_id, text=SET_OK_MSG)
return
if not args[0].isdigit():
bot.send_message(update.message.chat_id, text=NUM_ERROR)
chat_data[ChatData.MAXFLOOD] = int(args[0])
bot.send_message(chat_id=update.message.chat_id, text=SET_OK_MSG)
@command_wrap(pass_chat_data=True)
@check_admin()
@check_run()
@run_async
def settings(bot, update, chat_data):
"""
show group limit setting
:param chat_data:
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
ret_text = ""
limit = chat_data.get(BAN_STATE, {})
for setting in allow_setting:
ret_text = ret_text + SETTING_MSG.format(setting=setting, state=(NO if limit.get(setting) else OK))
bot.send_message(chat_id=update.message.chat_id, text=ret_text)
@command_wrap(pass_chat_data=True, pass_args=True)
@check_admin()
@check_run()
@run_async
def banword(bot, update, args, chat_data):
"""
set banword
:param chat_data:
:type chat_data: dict
:param args:
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
if not len(args):
bot.send_message(chat_id=update.message.chat_id, text=BANWORD_ERROR)
return
group_banwords = chat_data.get(ChatData.BANWORD, [])
group_banwords.extend(args)
chat_data[ChatData.BANWORD] = group_banwords
chat_data[ChatData.BANWORD_RE] = word_re(group_banwords)
bot.send_message(chat_id=update.message.chat_id, text=BANWORD_MSG.format(word=" ".join(args)))
@command_wrap(pass_chat_data=True, pass_args=True)
@check_admin()
@check_run()
@run_async
def unbanword(bot, update, args, chat_data):
"""
cancel banword
:param chat_data:
:type chat_data: dict
:param args:
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
if not len(args):
bot.send_message(chat_id=update.message.chat_id, text=BANWORD_ERROR)
return
group_banwords = chat_data.get(ChatData.BANWORD, [])
for arg in args:
try:
group_banwords.remove(arg)
except ValueError:
continue
chat_data[ChatData.BANWORD] = group_banwords
bot.send_message(chat_id=update.message.chat_id, text=UNBANWORD_MSG.format(word=" ".join(args)))
if not len(group_banwords):
chat_data[ChatData.BANWORD_RE] = None
return
chat_data[ChatData.BANWORD_RE] = word_re(group_banwords)
@command_wrap(pass_chat_data=True)
@check_admin()
@check_run()
@run_async
def banwords(bot, update, chat_data):
"""
show banword list
:param chat_data:
:type chat_data: dict
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
group_banwords = chat_data.get(ChatData.BANWORD, [])
ret_text = ""
for group_banword in group_banwords:
ret_text = ret_text + BANWORD_FORMAT.format(word=group_banword)
ret_text = GET_BANWORDS_MSG.format(banwords=ret_text)
bot.send_message(chat_id=update.message.chat_id, text=ret_text)
@command_wrap(pass_chat_data=True, pass_args=True)
@run_async
@check_admin()
@check_run()
def lang(bot, update, args, chat_data):
"""
set lang limit
:param chat_data:
:param args:
:param args:
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
if len(args) < 2:
bot.send_message(chat_id=update.message.chat_id, text=ARG_ERROR_MSG)
return
if args[0] not in list(LANG_DICT.keys()):
bot.send_message(chat_id=update.message.chat_id, text=ARG_ERROR_MSG)
return
args[0] = LANG_DICT.get(args[0])
ban_list: list = chat_data.get(ChatData.LANG, [])
if args[1] == OpenState.CLODE:
ban_list.append(args[0])
chat_data[ChatData.LANG] = ban_list
elif args[1] == OpenState.OPEN:
if args[0] in ban_list:
ban_list.remove(args[0])
chat_data[ChatData.LANG] = ban_list
else:
bot.send_message(chat_id=update.message.chat_id, text=OPITON_ERROR)
return
bot.send_message(chat_id=update.message.chat_id, text=LANG_MSG.format(lang=args[0], state=args[1]))
@command_wrap()
@check_admin()
@run_async
def save(bot, update):
"""
save cache data
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
save_data()
bot.send_message(chat_id=update.message.chat_id, text=SET_OK_MSG)
@command_wrap(pass_args=True)
@check_admin()
@check_run()
@run_async
def kick(bot, update, args):
"""
:param args:
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
kick_user_list = []
for _ in args:
if _.isdigit():
kick_user_list.append(int(_))
if update.message.reply_to_message:
kick_user_list.append(update.message.reply_to_message.from_user['id'])
ban_users = update.message.parse_entities(types=MessageEntity.TEXT_MENTION)
kick_user_list.extend([user.user['id'] for user in ban_users.keys()])
if not len(kick_user_list):
bot.send_message(chat_id=update.message.chat_id, text=ARG_ERROR_MSG)
return
kick_user(bot, update, user_list=kick_user_list)
bot.send_message(chat_id=update.message.chat_id, text=KICK_MSG.format(ids=" ".join(kick_user_list)))
@command_wrap(pass_args=True, pass_chat_data=True)
@check_admin()
@check_run()
@run_async
def lock(bot, update, args, chat_data):
"""
:param args:
:param chat_data:
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
if not len(args) or not args[0].isdigit():
bot.send_message(chat_id=update.message.chat_id, text=NUM_ERROR)
return
chat_data[ChatData.LOCKTIME] = datetime.now().timestamp() + int(args[0])
bot.send_message(chat_id=update.message.chat_id, text=LOCK_MSG)
@command_wrap(pass_chat_data=True)
@check_admin()
@check_run()
@run_async
def unlock(bot, update, chat_data):
"""
:param chat_data:
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
chat_data[ChatData.LOCKTIME] = None
bot.send_message(chat_id=update.message.chat_id, text=UNLOCK_MSG)
@command_wrap()
@check_admin()
@check_run()
def autolock(bot, update):
"""
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
update.message.reply_text(text=START_TIME_MSG, reply_markup=ForceReply())
return RunState.START_TIME
@command_wrap(pass_job_queue=True, pass_args=True)
@check_admin()
@check_run()
@run_async
def timer(bot, update, job_queue, args):
"""
:param bot:
:param update:
:param job_queue:
:param args:
:type job_queue: JobQueue
:return:
"""
if len(args) < 2:
bot.send_message(chat_id=update.message.chat_id, text=ARG_ERROR_MSG)
return
try:
day = datetime.strptime(args[0], "%H:%M")
time = day.time()
except ValueError:
bot.send_message(chat_id=update.message.chat_id, text=ARG_ERROR_MSG)
return
job_queue.run_repeating(callback=time_send_msg, context=[args[0], args[1], update.message.chat_id],
name=update.message.chat_id, interval=timedelta(days=1), first=time)
bot.send_message(chat_id=update.message.chat_id, text=TIMER_MSG)
@command_wrap(pass_job_queue=True)
@check_admin()
@check_run()
@run_async
def listtimer(bot, update, job_queue):
"""
:param bot:
:param update:
:param job_queue:
:return:
"""
jobs: List[Job] = job_queue.get_jobs_by_name(update.message.chat_id)
if not len(jobs):
bot.send_message(chat_id=update.message.chat_id, text=NO_INFO_MSG)
return
ret_text = ""
for job in jobs:
if not job.removed:
ret_text = ret_text + LISTTIMER_MSG.format(time=job.context[0], msg=job.context[1])
if not ret_text:
bot.send_message(chat_id=update.message.chat_id, text=NO_INFO_MSG)
return
bot.send_message(chat_id=update.message.chat_id, text=ret_text)
@command_wrap(pass_job_queue=True, pass_args=True)
@check_admin()
@check_run()
@run_async
def deletetimer(bot, update, job_queue, args):
if not len(args) or not args[0].isdigit():
bot.send_message(chat_id=update.message.chat_id, text=NUM_ERROR)
return
jobs = job_queue.get_jobs_by_name(update.message.chat_id)
if len(jobs) < int(args[0]):
bot.send_message(chat_id=update.message.chat_id, text=NUM_ERROR)
return
jobs[int(args[0])].schedule_removal()
bot.send_message(chat_id=update.message.chat_id, text=DELETE_TIMER_MSG)
@command_wrap(pass_chat_data=True)
@check_admin()
@check_run()
@run_async
def unautolock(bot, update, chat_data):
"""
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
chat_data[ChatData.AUTO_LOOK_START] = None
chat_data[ChatData.AUTO_LOOK_STOP] = None
bot.send_message(chat_id=update.message.chat_id, text=UNAUTOLOCK_MSG)
@command_wrap()
@check_admin()
def cancel(bot, update):
"""
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
return ConversationHandler.END
@messaage_warp(filters=Filters.all, pass_chat_data=True)
@check_admin()
@check_run()
@run_async
def lockstart(bot, update, chat_data):
"""
:param chat_data:
:param update:
:type update: Update
:return:
"""
if not update.message.text:
update.message.reply_text(text=START_TIME_MSG, reply_markup=ForceReply())
return RunState.START_TIME
try:
time = datetime.strptime(update.message.text, "%H:%M")
except ValueError:
update.message.reply_text(text=START_TIME_MSG, reply_markup=ForceReply())
return RunState.START_TIME
chat_data[ChatData.AUTO_LOOK_START] = time
update.message.reply_text(text=STOP_TIME_MSG, reply_markup=ForceReply())
return RunState.STOP_TIME
@messaage_warp(filters=Filters.all, pass_chat_data=True)
@check_admin()
@check_run()
@run_async
def lockstop(bot, update, chat_data):
"""
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return:
"""
if not update.message.text:
update.message.reply_text(text=START_TIME_MSG, reply_markup=ForceReply())
return STOP_TIME
try:
locktime = datetime.strptime(update.message.text, "%H:%M")
except ValueError:
update.message.reply_text(text=START_TIME_MSG, reply_markup=ForceReply())
return STOP_TIME
chat_data[ChatData.AUTO_LOOK_STOP] = locktime
bot.send_message(chat_id=update.message.chat_id, text=SET_OK_MSG)
return ConversationHandler.END
def save_data(bot=None, job=None):
user_data = get_user_data()
chat_data = get_chat_data()
with open(CHAT_DATA_FILE, 'wb+') as f:
pickle.dump(chat_data, f)
with open(USER_DATA_FILE, 'wb+') as f:
pickle.dump(user_data, f)
if job:
if isinstance(job, JobQueue):
save_jobs(job)
else:
save_jobs(job.job_queue)
logging.info("save data ok")
def ban_user(user_list, ban=True):
session = DBSession()
for user_data in user_list:
session.merge(User(id=user_data.id, isban=ban, username=user_data.username))
session.commit()
session.close()