-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdnd.py
1068 lines (975 loc) · 41.3 KB
/
dnd.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
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
__version__ = (0, 3, 17)
# ▄▀█ █▄ █ █▀█ █▄ █ █▀█ ▀▀█ █▀█ █ █ █▀
# █▀█ █ ▀█ █▄█ █ ▀█ ▀▀█ █ ▀▀█ ▀▀█ ▄█
#
# © Copyright 2024
#
# developed by @anon97945
#
# https://t.me/apodiktum_modules
# https://github.com/anon97945
#
# 🔒 Licensed under the GNU AGPLv3
# 🌐 https://www.gnu.org/licenses/agpl-3.0.html
# █ █ ▀ █▄▀ ▄▀█ █▀█ ▀ ▄▀█ ▀█▀ ▄▀█ █▀▄▀█ ▄▀█
# █▀█ █ █ █ █▀█ █▀▄ █ ▄ █▀█ █ █▀█ █ ▀ █ █▀█
#
# © Copyright 2022
#
# https://t.me/hikariatama
# meta developer: @apodiktum_modules
# meta banner: https://t.me/apodiktum_dumpster/11
# meta pic: https://t.me/apodiktum_dumpster/13
# scope: hikka_only
# scope: hikka_min 1.3.3
import asyncio
import contextlib
import datetime
import logging
import re
import time
from typing import Union
from telethon.tl.functions.account import UpdateProfileRequest
from telethon.tl.functions.contacts import BlockRequest, UnblockRequest
from telethon.tl.functions.messages import DeleteHistoryRequest, ReportSpamRequest
from telethon.tl.functions.users import GetFullUserRequest
from telethon.tl.types import Channel, Message, PeerUser, User
from telethon.utils import get_display_name, get_peer_id
from .. import loader, utils
logger = logging.getLogger(__name__)
def format_(state: Union[bool, None]) -> str:
if state is None:
return "❔"
return "✅" if state else "🚫 Not"
@loader.tds
class ApodiktumDNDMod(loader.Module):
"""
-> Prevents people sending you unsolicited private messages.
-> Prevents disturbing when you are unavailable.
Check `.cdnd`.
"""
strings = {
"name": "Apo-DND",
"developer": "@anon97945",
"_cfg_active_threshold": (
"What number of your messages is required to trust peer."
),
"_cfg_afk_show_duration": (
"If set to true, AFK message will include the the automatic removal time."
),
"_cfg_cst_auto_migrate": "Wheather to auto migrate defined changes on startup.",
"_cfg_custom_msg": (
"Custom message to notify untrusted peers. Leave empty for default one."
),
"_cfg_delete_dialog": "If set to true, dialog will be deleted after banning.",
"_cfg_doc_afk_group_list": "React to Tags from chats in this list.",
"_cfg_doc_whitelist": (
"Whether the `afk_group_list`-list is for included(True) or"
" excluded(False) chats."
),
"_cfg_gone": (
"If set to true, the AFK message will include the time you were gone."
),
"_cfg_ignore_active": "If set to true, ignore peers, where you participated.",
"_cfg_ignore_contacts": "If set to true, ignore contacts.",
"_cfg_photo": "Photo, which is sent along with banned notification.",
"_cfg_pmbl": "If set to true, PMBL is active.",
"_cfg_report_spam": "If set to true, user will be reported after banning.",
"_cfg_use_bio": "Show AFK message in bio.",
"_log_msg_approved": "User approved in pm {}, filter: {}",
"_log_msg_punished": "Intruder punished: {}",
"_log_msg_unapproved": "User unapproved in pm {}.",
"afk_message": "{}\n",
"afk_message_duration": "\n<b><u>Duration:</u></b>\n<code>{}</code>",
"afk_message_further": "\n<b><u>Further:</u></b>\n<code>{}</code>",
"afk_message_gone": "\n<b><u>Gone since:</u></b>\n<code>{}</code>",
"approved": '😶🌫️ <b><a href="tg://user?id={}">{}</a> approved in pm.</b>',
"args_incorrect": "<b>🚫 Args are incorrect.</b>",
"args_pmban": "ℹ️ <b>Example usage: </b><code>.pmbanlast 5</code>",
"available_statuses": "<b>🦊 Available statuses:</b>\n\n",
"banned": (
"😊 <b>Hey there •ᴗ•</b>\n<b>i am Unit «SIGMA»<b>, the"
" <b>guardian</b> of this account. You are <b>not approved</b>! You"
" can contact my owner <b>in a groupchat</b>, if you need"
" help.\n<b>I need to ban you in terms of security.</b>"
),
"banned_log": (
"👮 <b>I banned {}.</b>\n\n<b>{} Contact</b>\n<b>{} Started by"
" you</b>\n<b>{} Active conversation</b>\n\n<b>✊"
" Actions</b>\n\n<b>{} Reported spam</b>\n<b>{} Deleted"
" dialog</b>\n<b>{} Blocked</b>\n\n<b>ℹ️"
" Message</b>\n<code>{}</code>"
),
"blocked": '😶🌫️ <b><a href="tg://user?id={}">{}</a> blocked.</b>',
"hello": (
"🔏 <b>Unit «SIGMA»</b> protects your personal messages from"
" intrusions. It will block everyone, who's trying to invade"
" you.\n\nUse <code>.pmbanlast</code> if you've already been"
" pm-raided."
),
"no_pchat": "<b>This command is only available in private chats.</b>",
"no_reply": "ℹ️ <b>Reply to a message to block the user.</b>",
"no_status": "<b>🚫 No status is active.</b>",
"pm_reported": "⚠️ <b>You just got reported to spam !</b>",
"removed": "😶🌫️ <b>Removed {} last dialogs!</b>",
"removing": "😶🌫️ <b>Removing {} last dialogs...</b>",
"status_created": "<b>✅ Status {} created.</b>\n<code>{}</code>\nNotify: {}",
"status_not_found": "<b>🚫 Status not found.</b>",
"status_removed": "<b>✅ Status {} deleted.</b>",
"status_set": "<b>✅ Status set\n</b><code>{}</code>\nNotify: <code>{}</code>",
"status_set_duration": "\nDuration: <code>{}</code>",
"status_set_further": "\nFurther: <code>{}</code>",
"status_unset": "<b>✅ Status removed.</b>",
"unapproved": '😶🌫️ <b><a href="tg://user?id={}">{}</a> unapproved in pm.</b>',
"unblocked": '😶🌫️ <b><a href="tg://user?id={}">{}</a> unblocked.</b>',
"user_not_specified": "🚫 <b>You haven't specified user.</b>",
}
strings_en = {}
strings_de = {}
strings_ru = {
"_cfg_active_threshold": (
"Какое количество Ваших сообщений необходимо, чтобы доверять пользователю."
),
"_cfg_afk_show_duration": (
"Если включено, сообщение AFK будет содержать время его окончания"
),
"_cfg_custom_msg": (
"Кастомное оповещение неодобренных пользователей. Оставьте пустым,"
" чтобы оставить по умолчанию."
),
"_cfg_delete_dialog": (
"Если установлено true, диалог будет удалён после блокировки."
),
"_cfg_gone": (
"Если установлено true, AFK сообщение будет включать время, когда вы ушли."
),
"_cfg_ignore_active": (
"Если установлено true, игнорирует диалоги, где вы участвовали."
),
"_cfg_ignore_contacts": "Если установлено true, игнорирует контакты.",
"_cfg_photo": "Фото, которое отправляется вместе с уведомлением о блокировке",
"_cfg_pmbl": "Если установлено true, PMBL активирован.",
"_cfg_report_spam": (
"Если установлено true, после блокировки на пользователя будет"
" отправлена жалоба о спаме."
),
"_cfg_use_bio": "Показывать сообщение об отсутствии в профиле.",
"_cls_doc": (
" \n"
"-> Запрещает людям отправлять вам нежелательные личные сообщения."
"-> Избавляет от беспокойства, когда вы недоступны."
"Смотрите `.config apodiktum dnd`."
),
"_cmd_doc_allowpm": (
"<ответ или username> - Разрешает пользователю писать вам в ЛС."
),
"_cmd_doc_block": "<ответ> - Блокирует этого пользователя без предупреждения.",
"_cmd_doc_cdnd": "Это откроет конфиг для модуля.",
"_cmd_doc_delstatus": "<короткое_название> - Удаляет статус.",
"_cmd_doc_denypm": (
"<ответ или username> - Запрещает пользователю писать вам в ЛС."
),
"_cmd_doc_newstatus": (
"<короткое_название> <notif|0/1> <text>\n"
" - Новый статус\n"
" - Пример: .newstatus test 1 Привет!"
),
"_cmd_doc_pmbanlast": (
"<число> - Блокирует и удаляет диалоги с большим кол-вом новых"
" пользователей."
),
"_cmd_doc_report": (
"<ответ> - Отправляет жалобу на пользователя на СПАМ. Использовать"
" только в ЛС."
),
"_cmd_doc_status": (
"<короткое название> [необязательно длительность|1s/m/h/d] [необязательно"
" дополнительная информация] - Установить статус"
),
"_cmd_doc_statuses": " - Показывает доступные статусы.",
"_cmd_doc_unblock": "<ответ> - Разблокировать этого пользователя.",
"_cmd_doc_unstatus": " - Удаляет статус.",
"_log_msg_approved": "Пользователь {} допущен в ЛС, фильтр: {}",
"_log_msg_punished": "Нарушитель наказан: {}",
"_log_msg_unapproved": "Пользователь {} не допущен к ЛС.",
"afk_message": "{}\n",
"afk_message_duration": "\n<b><u>Буду AFK:</u></b>\n<code>{}</code>",
"afk_message_further": "\n<b><u>Подробнее:</u></b>\n<code>{}</code>",
"afk_message_gone": "\n<b><u>Отсутствую:</u></b>\n<code>{}</code>",
"approved": '😶🌫️ <b><a href="tg://user?id={}">{}</a> допущен к ЛС.</b>',
"args_incorrect": "<b>🚫 Аргументы некорректны.</b>",
"args_pmban": "ℹ️ <b>Пример использования: </b><code>.pmbanlast 5</code>",
"available_statuses": "<b>🦊 Доступные статусы:</b>\n\n",
"banned": (
"😊 <b>Привет •ᴗ•</b>\n<b>«SIGMA»<b>, <b>защитник</b> этого"
" аккаунта. Вы <b>не допущены к ЛС</b>! Вы можете связаться с моим"
" владельцем<b>в чате</b>, если Вам нужна помощь.\n<b>По правилам"
" безопасности, я должен заблокировать Вас.</b>"
),
"banned_log": (
"👮 <b>Я заблокировал {}.</b>\n\n<b>{} Контакт</b>\n<b>{} Начатый"
" тобой</b>\n<b>{} Активный диалог</b>\n\n<b>✊"
" Действия</b>\n\n<b>{} Сообщить о спаме</b>\n<b>{} Удалить"
" диалог</b>\n<b>{} Заблокировать</b>\n\n<b>ℹ️"
" Сообщение</b>\n<code>{}</code>"
),
"blocked": '😶🌫️ <b><a href="tg://user?id={}">{}</a> заблокирован.</b>',
"hello": (
"🔏 <b>«SIGMA»</b> защищает ваши личные сообщения от нежелательного"
" контакта. Это будет блокировать всех, кто попытается связаться с"
" Вами..\n\nИспользуй <code>.pmbanlast</code> если уже были попытки"
" нежелательного вторжения."
),
"no_pchat": "<b>Эта команда работает только в ЛС.</b>",
"no_reply": (
"ℹ️ <b>Ответьте на сообщение, чтобы заблокировать пользователя.</b>"
),
"no_status": "<b>🚫 Нет активного статуса.</b>",
"pm_reported": "⚠️ <b>Отправил жалобу на спам!</b>",
"removed": "😶🌫️ <b>Удалил {} последних диалогов!</b>",
"removing": "😶🌫️ <b>Удаляю {} последних диалогов...</b>",
"status_created": "<b>✅ Статус {} установлен.</b>\n<code>{}</code>\nNotify: {}",
"status_not_found": "<b>🚫 Статус не найден.</b>",
"status_removed": "<b>✅ Статус {} удалён.</b>",
"status_set": (
"<b>✅ Статус установлен\n</b><code>{}</code>\nУведомления: <code>{}</code>"
),
"status_set_duration": "\nПродолжительность: <code>{}</code>",
"status_set_further": "\nПодробнее: <code>{}</code>",
"status_unset": "<b>✅ Статус удалён.</b>",
"unapproved": '😶🌫️ <b><a href="tg://user?id={}">{}</a> не допущен к ЛС.</b>',
"unblocked": '😶🌫️ <b><a href="tg://user?id={}">{}</a> разблокирован.</b>',
"user_not_specified": "🚫 <b>Вы не указали пользователя.</b>",
}
all_strings = {
"strings": strings,
"strings_en": strings,
"strings_de": strings_de,
"strings_ru": strings_ru,
}
changes = {
"migration1": {
"name": {
"old": "Apo DND",
"new": "Apo-DND",
},
},
}
def __init__(self):
self.config = loader.ModuleConfig(
loader.ConfigValue(
"PMBL_Active",
True,
doc=lambda: self.strings("_cfg_pmbl"),
validator=loader.validators.Boolean(),
),
loader.ConfigValue(
"active_threshold",
5,
doc=lambda: self.strings("_cfg_active_threshold"),
validator=loader.validators.Integer(minimum=1),
),
loader.ConfigValue(
"afk_gone_time",
True,
doc=lambda: self.strings("_cfg_gone"),
validator=loader.validators.Boolean(),
),
loader.ConfigValue(
"afk_group_list",
doc=lambda: self.strings("_cfg_doc_afk_group_list"),
validator=loader.validators.Series(
loader.validators.TelegramID(),
),
),
loader.ConfigValue(
"afk_show_duration",
True,
doc=lambda: self.strings("_cfg_afk_show_duration"),
validator=loader.validators.Boolean(),
),
loader.ConfigValue(
"afk_tag_whitelist",
True,
doc=lambda: self.strings("_cfg_doc_whitelist"),
validator=loader.validators.Boolean(),
),
loader.ConfigValue(
"custom_message",
doc=lambda: self.strings("_cfg_custom_msg"),
),
loader.ConfigValue(
"delete_dialog",
False,
doc=lambda: self.strings("_cfg_delete_dialog"),
validator=loader.validators.Boolean(),
),
loader.ConfigValue(
"ignore_active",
True,
doc=lambda: self.strings("_cfg_ignore_active"),
validator=loader.validators.Boolean(),
),
loader.ConfigValue(
"ignore_contacts",
True,
doc=lambda: self.strings("_cfg_ignore_contacts"),
validator=loader.validators.Boolean(),
),
loader.ConfigValue(
"photo",
"https://github.com/hikariatama/assets/raw/master/unit_sigma.png",
doc=lambda: self.strings("_cfg_photo"),
validator=loader.validators.Link(),
),
loader.ConfigValue(
"report_spam",
False,
doc=lambda: self.strings("_cfg_report_spam"),
validator=loader.validators.Boolean(),
),
loader.ConfigValue(
"use_bio",
True,
doc=lambda: self.strings("_cfg_use_bio"),
validator=loader.validators.Boolean(),
),
loader.ConfigValue(
"auto_migrate",
True,
doc=lambda: self.strings("_cfg_cst_auto_migrate"),
validator=loader.validators.Boolean(),
), # for MigratorClass
)
async def client_ready(self):
self.apo_lib = await self.import_lib(
"https://raw.githubusercontent.com/anon97945/hikka-libs/master/apodiktum_library.py",
suspend_on_error=True,
)
await self.apo_lib.migrator.auto_migrate_handler(
self.__class__.__name__,
self.strings("name"),
self.changes,
self.config["auto_migrate"],
)
self._ratelimit_afk = []
self._ratelimit_pmbl = []
self._ratelimit_pmbl_threshold = 10
self._ratelimit_pmbl_timeout = 5 * 60
self._sent_messages = []
self._whitelist = self.get("whitelist", [])
if not self.get("ignore_hello", False):
await self.inline.bot.send_photo(
self.tg_id,
photo=(
r"https://github.com/hikariatama/assets/raw/master/unit_sigma.png"
),
caption=self.strings("hello"),
parse_mode="HTML",
)
self.set("ignore_hello", True)
def _approve(self, user: int, reason: str = "unknown"):
self._whitelist += [user]
self._whitelist = list(set(self._whitelist))
self.set("whitelist", self._whitelist)
if reason != "blocked":
self.apo_lib.utils.log(
logging.INFO,
__name__,
self.strings("_log_msg_approved").format(user, reason),
)
def _unapprove(self, user: int):
self._whitelist = list(set(self._whitelist))
self._whitelist = list(filter(lambda x: x != user, self._whitelist))
self.set("whitelist", self._whitelist)
self.apo_lib.utils.log(
logging.INFO, __name__, self.strings("_log_msg_unapproved").format(user)
)
async def _send_pmbl_message(
self, message, peer, contact, started_by_you, active_peer, self_id
):
if len(self._ratelimit_pmbl) < self._ratelimit_pmbl_threshold:
try:
await self._client.send_file(
peer,
self.config["photo"],
caption=self.config["custom_message"]
or self.apo_lib.utils.get_str("banned", self.all_strings, message),
)
except Exception:
await utils.answer(
message,
self.config["custom_message"]
or self.apo_lib.utils.get_str("banned", self.all_strings, message),
)
self._ratelimit_pmbl += [round(time.time())]
try:
peer = await self._client.get_entity(peer)
except ValueError:
await asyncio.sleep(1)
peer = await self._client.get_entity(peer)
await self.inline.bot.send_message(
self_id,
self.apo_lib.utils.get_str(
"banned_log", self.all_strings, message
).format(
await self.apo_lib.utils.get_tag(peer, True),
format_(contact),
format_(started_by_you),
format_(active_peer),
format_(self.config["report_spam"]),
format_(self.config["delete_dialog"]),
format_(True),
self.apo_lib.utils.raw_text(message)[:3000],
),
parse_mode="HTML",
disable_web_page_preview=True,
)
async def _active_peer(self, cid, peer):
if self.config["ignore_active"]:
q = 0
async for msg in self._client.iter_messages(peer, limit=200):
if msg.sender_id == self.tg_id:
q += 1
if q >= self.config["active_threshold"]:
self._approve(cid, "active_threshold")
return True
return False
async def _punish_handler(self, cid):
await self._client(BlockRequest(id=cid))
if self.config["report_spam"]:
await self._client(ReportSpamRequest(peer=cid))
if self.config["delete_dialog"]:
await self._client(
DeleteHistoryRequest(peer=cid, just_clear=True, max_id=0)
)
async def _unstatus_func(self, delay=None):
if delay:
await asyncio.sleep(delay)
self.set("status", False)
self.set("status_duration", "")
self.set("gone", "")
self.set("further", "")
self._ratelimit_afk = []
if self.get("old_bio"):
await self._client(UpdateProfileRequest(about=self.get("old_bio")))
self.set("old_bio", None)
for m in self._sent_messages:
try:
await m.delete()
except Exception as exc: # skipcq: PYL-W0703
self.apo_lib.utils.log(
logging.DEBUG,
__name__,
f"Message not deleted due to {exc}",
exc_info=True,
)
self._sent_messages = []
async def cdndcmd(self, message: Message):
"""
This will open the config for the module.
"""
name = self.strings("name")
await self.allmodules.commands["config"](
await utils.answer(message, f"{self.get_prefix()}config {name}")
)
async def pmbanlastcmd(self, message: Message):
"""
<number> - Ban and delete dialogs with n most new users.
"""
n = utils.get_args_raw(message)
if not n or not n.isdigit():
await utils.answer(
message,
self.apo_lib.utils.get_str("args_pmban", self.all_strings, message),
)
return
n = int(n)
await utils.answer(
message,
self.apo_lib.utils.get_str("removing", self.all_strings, message).format(n),
)
dialogs = []
async for dialog in self._client.iter_dialogs(ignore_pinned=True):
try:
if not isinstance(dialog.message.peer_id, PeerUser):
continue
except AttributeError:
continue
m = (
await self._client.get_messages(
dialog.message.peer_id,
limit=1,
reverse=True,
)
)[0]
dialogs += [
(
get_peer_id(dialog.message.peer_id),
int(time.mktime(m.date.timetuple())),
)
]
dialogs.sort(key=lambda x: x[1])
to_ban = [d for d, _ in dialogs[::-1][:n]]
for d in to_ban:
await self._client(BlockRequest(id=d))
await self._client(DeleteHistoryRequest(peer=d, just_clear=True, max_id=0))
await utils.answer(
message,
self.apo_lib.utils.get_str("removed", self.all_strings, message).format(n),
)
async def allowpmcmd(self, message: Message):
"""
<reply or user> - Allow user to pm you.
"""
args = utils.get_args_raw(message)
reply = await message.get_reply_message()
user = None
try:
user = await self._client.get_entity(args)
except Exception:
with contextlib.suppress(Exception):
user = await reply.get_sender() if reply else None
if not user:
chat = await message.get_chat()
if not isinstance(chat, User):
await utils.answer(
message,
self.apo_lib.utils.get_str(
"user_not_specified", self.all_strings, message
),
)
return
user = chat
self._approve(user.id, "manual_approve")
await utils.answer(
message,
self.apo_lib.utils.get_str("approved", self.all_strings, message).format(
user.id, get_display_name(user)
),
)
async def denypmcmd(self, message: Message):
"""
<reply or user> - Deny user to pm you.
"""
args = utils.get_args_raw(message)
reply = await message.get_reply_message()
user = None
try:
user = await self._client.get_entity(int(args))
if not isinstance(user, User):
user = None
except Exception:
with contextlib.suppress(Exception):
user = await reply.get_sender() if reply else None
if not user:
chat = await message.get_chat()
if not isinstance(chat, User):
await utils.answer(
message,
self.apo_lib.utils.get_str(
"user_not_specified", self.all_strings, message
),
)
return
user = chat
self._unapprove(user.id)
await utils.answer(
message,
self.strings("unapproved").format(user.id, get_display_name(user)),
)
async def reportpmcmd(self, message: Message):
"""
<reply> - Report the user to spam. Use only in PM.
"""
if not message.is_private:
await utils.answer(
message,
self.apo_lib.utils.get_str("no_pchat", self.all_strings, message),
)
return
user = await message.get_chat()
await message.client(ReportSpamRequest(peer=user.id))
await utils.answer(
message,
self.apo_lib.utils.get_str("pm_reported", self.all_strings, message),
)
async def blockcmd(self, message: Message):
"""
<reply> - Block this user without being warned.
"""
user = await utils.get_target(message)
user = await self._client.get_entity(user)
if not user:
await utils.answer(
message,
self.apo_lib.utils.get_str("no_reply", self.all_strings, message),
)
return
await message.client(BlockRequest(user.id))
await utils.answer(
message,
self.apo_lib.utils.get_str("blocked", self.all_strings, message).format(
user.id, get_display_name(user)
),
)
async def unblockcmd(self, message: Message):
"""
<reply> - Unblock this user.
"""
user = await utils.get_target(message)
user = await self._client.get_entity(user)
if not user:
await utils.answer(
message,
self.apo_lib.utils.get_str("no_reply", self.all_strings, message),
)
return
await message.client(UnblockRequest(user.id))
await utils.answer(
message,
self.apo_lib.utils.get_str("unblocked", self.all_strings, message).format(
user.id, get_display_name(user)
),
)
async def statuscmd(self, message: Message):
"""
<short_name> [optional duration|1s/m/h/d] [optional further information] - Set status.
"""
status_duration = ""
status = ""
t = ""
args = utils.get_args_raw(self.apo_lib.utils.raw_text(message, True))
args = args.split(" ", 2)
t = sum(
self.apo_lib.utils.convert_time(time_str) or 0
for time_str in re.findall(
r"(\d+[a-zA-Z])", args[1] if len(args) > 1 else ""
)
)
if args[0] not in self.get("texts", {}):
await utils.answer(
message,
self.apo_lib.utils.get_str(
"status_not_found", self.all_strings, message
),
)
await asyncio.sleep(3)
await message.delete()
return
if self.get("status"):
await self._unstatus_func()
if self.config["use_bio"] and not self.get("old_bio"):
full = await self._client(GetFullUserRequest("me"))
self.set("old_bio", getattr(full.full_user, "about", None))
self.set("status", args[0])
self.set("gone", time.time())
if t and len(args) > 2:
args = list(
map(
lambda x: x.replace(
"<emoji document_id=", "</code><emoji document_id="
).replace("</emoji>", "</emoji><code>")
if isinstance(x, str)
else x,
args,
)
)
self.set("further", args[2])
elif not t and len(args) > 1:
args[1:] = [" ".join(args[1:])]
args = list(
map(
lambda x: x.replace(
"<emoji document_id=", "</code><emoji document_id="
).replace("</emoji>", "</emoji><code>")
if isinstance(x, str)
else x,
args,
)
)
self.set("further", args[1])
self._ratelimit_afk = []
if t:
with contextlib.suppress(Exception):
self._unstatus_task.cancel()
self._unstatus_task = asyncio.ensure_future(self._unstatus_func(t))
self.set("status_duration", time.time() + t)
status_duration = (
datetime.datetime.fromtimestamp(self.get("status_duration")).replace(
microsecond=0
)
- datetime.datetime.now().replace(microsecond=0)
).total_seconds()
status += self.apo_lib.utils.get_str(
"status_set", self.all_strings, message
).format(
self.get("texts", {})[args[0]]
.replace("<emoji document_id=", "</code><emoji document_id=")
.replace("</emoji>", "</emoji><code>"),
str(self.get("notif")[args[0]]),
)
if self.get("further"):
status += self.apo_lib.utils.get_str(
"status_set_further", self.all_strings, message
).format(self.get("further"))
if status_duration:
status += self.apo_lib.utils.get_str(
"status_set_duration", self.all_strings, message
).format(self.apo_lib.utils.time_formatter(t, short=True))
if self.config["use_bio"]:
bio = self.get("texts", {})[args[0]]
if self.get("further"):
bio += f" | {self.apo_lib.utils.get_str('afk_message_further', self.all_strings, message).format(self.get('further'))}"
bio = self.apo_lib.utils.remove_html(bio)
bio_len = 140 if (await self._client.get_me()).premium else 70
await self.client(UpdateProfileRequest(about=bio[:bio_len]))
msg = await utils.answer(message, status)
self._sent_messages += [msg]
async def unstatuscmd(self, message: Message):
"""
Remove status.
"""
with contextlib.suppress(Exception):
self._unstatus_task.cancel()
if not self.get("status", False):
await utils.answer(
message,
self.apo_lib.utils.get_str("no_status", self.all_strings, message),
)
await asyncio.sleep(3)
await message.delete()
return
await self._unstatus_func()
msg = await utils.answer(
message,
self.apo_lib.utils.get_str("status_unset", self.all_strings, message),
)
await asyncio.sleep(10)
await msg.delete()
async def newstatuscmd(self, message: Message):
"""
<short_name> <notif|0/1> <text> - New status.
Example: .newstatus test 1 Hello!
"""
args = utils.get_args_raw(self.apo_lib.utils.raw_text(message, True))
args = args.split(" ", 2)
if len(args) < 3 or args[1] not in ["1", "true", "yes", "+"]:
await utils.answer(
message,
self.apo_lib.utils.get_str("args_incorrect", self.all_strings, message),
)
await asyncio.sleep(3)
await message.delete()
return
args[1] = args[1] in ["1", "true", "yes", "+"]
texts = self.get("texts", {})
texts[args[0]] = args[2]
self.set("texts", texts)
notif = self.get("notif", {})
notif[args[0]] = args[1]
args = list(
map(
lambda x: x.replace(
"<emoji document_id=", "</code><emoji document_id="
).replace("</emoji>", "</emoji><code>")
if isinstance(x, str)
else x,
args,
)
)
self.set("notif", notif)
await utils.answer(
message,
self.apo_lib.utils.get_str(
"status_created", self.all_strings, message
).format(
args[0],
args[2],
args[1],
),
)
async def delstatuscmd(self, message: Message):
"""
<short_name> - Delete status.
"""
args = utils.get_args_raw(message)
if args not in self.get("texts", {}):
await utils.answer(
message,
self.apo_lib.utils.get_str(
"status_not_found", self.all_strings, message
),
)
await asyncio.sleep(3)
await message.delete()
return
texts = self.get("texts", {})
del texts[args]
self.set("texts", texts)
notif = self.get("notif", {})
del notif[args]
self.set("notif", notif)
await utils.answer(
message,
self.apo_lib.utils.get_str(
"status_removed", self.all_strings, message
).format(args),
)
async def statusescmd(self, message: Message):
"""
Show available statuses.
"""
res = self.apo_lib.utils.get_str(
"available_statuses", self.all_strings, message
)
for short_name, status in self.get("texts", {}).items():
res += (
f"<b><u>{short_name}</u></b> | Notify:"
f" <b>{self.get('notif', {})[short_name]}</b>\n{status}\n➖➖➖➖➖➖➖➖➖\n"
)
await utils.answer(message, res)
@loader.watcher("only_messages", "in")
async def watcher(self, message: Message):
is_pmbl = False
chat_id = utils.get_chat_id(message)
if chat_id in {
1271266957, # @replies
777000, # Telegram Notifications
self.tg_id, # Self
}:
return
try:
if (
self.config["PMBL_Active"]
and message.is_private
and not isinstance(message, Channel)
and isinstance(message.peer_id, PeerUser)
):
peer = (
getattr(getattr(message, "sender", None), "username", None)
or message.peer_id
)
is_pmbl = await self.p__pmbl(peer, message)
if not is_pmbl and (
message.is_private
or (
self.config["afk_tag_whitelist"]
and chat_id in self.config["afk_group_list"]
)
or (
not self.config["afk_tag_whitelist"]
and chat_id not in self.config["afk_group_list"]
)
):
user_id = await self.apo_lib.utils.get_user_id(message)
await self.p__afk(chat_id, user_id, message)
return
except ValueError as exc: # skipcq: PYL-W0703
self.apo_lib.utils.log(logging.DEBUG, __name__, exc)
async def p__pmbl(
self,
peer,
message: Union[None, Message] = None,
) -> bool:
cid = utils.get_chat_id(message)
if cid in self._whitelist:
return
contact, started_by_you, active_peer = None, None, None
with contextlib.suppress(ValueError):
entity = await message.get_sender()
if entity.bot:
return self._approve(cid, "bot")
if self.config["ignore_contacts"]:
if entity.contact:
return self._approve(cid, "ignore_contacts")
contact = False
first_message = (
await self._client.get_messages(
peer,
limit=1,
reverse=True,
)
)[0]
if (
getattr(message, "raw_text", False)
and first_message.sender_id == self.tg_id
):
return self._approve(cid, "started_by_you")
started_by_you = False
active_peer = await self._active_peer(cid, peer)
if active_peer:
return
self._ratelimit_pmbl = list(
filter(
lambda x: x + self._ratelimit_pmbl_timeout < time.time(),
self._ratelimit_pmbl,
)
)
await self._send_pmbl_message(
message, peer, contact, started_by_you, active_peer, self.tg_id
)
await self._punish_handler(cid)