Repository navigation
Expand file tree
/
Copy pathEmojiClone.py
More file actions
1057 lines (952 loc) · 41.7 KB
/
Copy pathEmojiClone.py
File metadata and controls
1057 lines (952 loc) · 41.7 KB
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
# CopyLeft 2026 github.com/i-execute
# Author: I_execute.t.me
# Licensed under AGPLv3.
__version__ = (1, 1, 0)
# meta developer: Execute_forge.t.me
import asyncio
import io
import logging
import os
import random
import re
import sys
from .. import loader, utils
from ..inline.types import InlineCall
DEPS = ["Pillow"]
ADDEMOJI_RE = re.compile(r'^https://t\.me/addemoji/([A-Za-z0-9_]+)$')
PACKS_PER_PAGE = 5
def _install_deps():
import subprocess
pip = os.path.join(os.path.dirname(sys.executable), "pip")
if not os.path.exists(pip):
pip = "pip"
for pkg in DEPS:
try:
subprocess.run(
[pip, "install", "-U", pkg, "--break-system-packages", "-q"],
capture_output=True,
text=True,
timeout=120,
)
except Exception as e:
logger.error(f"[EmojiClone] dep install {pkg}: {e}")
logger = logging.getLogger(__name__)
@loader.tds
class EmojiClone(loader.Module):
"""Emoji pack cloner"""
strings = {
"name": "EmojiClone",
"state_menu": (
"<b>EmojiClone</b>\n"
"<blockquote>Source pack: {source_status}\n"
"New pack link: {short_status}\n"
"Pack name: {name_status}</blockquote>"
),
"btn_set_source": "Source Pack Link",
"btn_set_short": "New Pack Link",
"btn_set_name": "Pack Name",
"btn_start": "Start Copy",
"btn_back": "Back",
"btn_close": "Close",
"btn_retry": "Try Again",
"input_source": "Send source emoji pack link:",
"input_short": "Send new pack link - must be free:",
"input_name": "Send the name for the new emoji pack:",
"source_set": (
"<b>Source Pack Set</b>\n"
"<blockquote>{link}\n"
"Emoji: {count}</blockquote>"
),
"source_invalid_format": (
"<b>Invalid Format</b>\n"
"<blockquote>Link must start with https://t.me/addemoji/\n"
"Example: https://t.me/addemoji/MyPack</blockquote>"
),
"source_invalid_resolve": (
"<b>Pack Not Found</b>\n"
"<blockquote>Could not find emoji pack at this link.\n"
"Make sure the link is correct and the pack exists.</blockquote>"
),
"short_set": (
"<b>New Pack Link Set</b>\n"
"<blockquote>{link}</blockquote>"
),
"short_invalid_format": (
"<b>Invalid Format</b>\n"
"<blockquote>Link must start with https://t.me/addemoji/\n"
"Example: https://t.me/addemoji/MyNewPack</blockquote>"
),
"short_occupied": (
"<b>Link Already Taken</b>\n"
"<blockquote>This short name is already in use.\n"
"Choose a different link.</blockquote>"
),
"name_set": (
"<b>Pack Name Set</b>\n"
"<blockquote>{name}</blockquote>"
),
"no_source": (
"<b>No Source Pack</b>\n"
"<blockquote>Set the source pack link first</blockquote>"
),
"no_short": (
"<b>No New Pack Link</b>\n"
"<blockquote>Set the new pack link first</blockquote>"
),
"no_name": (
"<b>No Pack Name</b>\n"
"<blockquote>Set the pack name first</blockquote>"
),
"copying": (
"<b>Copying</b>\n"
"<blockquote>Progress: {current}/{total}\n"
"Pack: {name}{flood}</blockquote>"
),
"done": (
"<b>Done</b>\n"
"<blockquote>Pack: <b>{name}</b>\n"
"Emoji: {count}</blockquote>\n"
"<blockquote><a href='https://t.me/addemoji/{short}'>Open pack</a></blockquote>"
),
"done_partial": (
"<b>Done with errors</b>\n"
"<blockquote>Pack: <b>{name}</b>\n"
"Copied: {copied}/{total}\n"
"Failed: {failed}</blockquote>\n"
"<blockquote><a href='https://t.me/addemoji/{short}'>Open pack</a></blockquote>"
),
"copy_failed": (
"<b>Copy Failed</b>\n"
"<blockquote>Could not copy any emoji.\n"
"Errors: {failed}</blockquote>"
),
"error": (
"<b>Error</b>\n"
"<blockquote>{error}</blockquote>"
),
"no_reply_emoji": (
"<b>No Custom Emoji</b>\n"
"<blockquote>Reply to a message containing a custom emoji</blockquote>"
),
"status_not_set": "Not set",
"checking": "Checking...",
"btn_left": "<",
"btn_right": ">",
"eadd_no_packs": (
"<b>No Packs Found</b>\n"
"<blockquote>You have not created any emoji packs yet.</blockquote>"
),
"eadd_list": (
"<b>Eadd - Choose Target Pack</b>\n"
"<blockquote>Page {page}/{total_pages}\n"
"Total packs: {total}</blockquote>"
),
"eadd_pack_detail": (
"<b>Eadd - Pack Selected</b>\n"
"<blockquote>{title}\n"
"@{short}\n"
"Emoji: {count}</blockquote>"
),
"eadd_btn_select": "Add Here",
"eadd_input_source": "Send source emoji pack link (https://t.me/addemoji/PackName):",
"eadd_ask_source": (
"<b>Target Pack: {title}</b>\n"
"<blockquote>Send the source emoji pack link to resolve emoji from.</blockquote>"
),
"eadd_source_set": (
"<b>Source Pack Resolved</b>\n"
"<blockquote>{link}\n"
"Emoji: {count}</blockquote>\n"
"Now send the emoji ID to add."
),
"eadd_btn_id": "Emoji ID",
"eadd_input_id": "Send the numeric emoji document ID from the resolved source pack:",
"eadd_id_invalid": (
"<b>Invalid ID</b>\n"
"<blockquote>Emoji ID must be a number.</blockquote>"
),
"eadd_id_not_found": (
"<b>Emoji Not Found</b>\n"
"<blockquote>No emoji with ID {id} in the resolved source pack.</blockquote>"
),
"eadd_adding": (
"<b>Adding Emoji</b>\n"
"<blockquote>Please wait...</blockquote>"
),
"eadd_done": (
"<b>Emoji Added</b>\n"
"<blockquote>Pack: <b>{title}</b>\n"
"Emoji ID: {id}</blockquote>\n"
"<blockquote><a href='https://t.me/addemoji/{short}'>Open pack</a></blockquote>"
),
"eadd_done_reply": (
"<b>Emoji Added</b>\n"
"<blockquote>Pack: <b>{title}</b></blockquote>\n"
"<blockquote><a href='https://t.me/addemoji/{short}'>Open pack</a></blockquote>"
),
"eadd_fail": (
"<b>Add Failed</b>\n"
"<blockquote>{error}</blockquote>"
),
}
strings_ru = {
"state_menu": (
"<b>EmojiClone</b>\n"
"<blockquote>Исходный пак: {source_status}\n"
"Ссылка нового пака: {short_status}\n"
"Название пака: {name_status}</blockquote>"
),
"btn_set_source": "Ссылка исходного пака",
"btn_set_short": "Ссылка нового пака",
"btn_set_name": "Название пака",
"btn_start": "Начать копирование",
"btn_back": "Назад",
"btn_close": "Закрыть",
"btn_retry": "Попробовать снова",
"input_source": "Отправьте ссылку на исходный пак:",
"input_short": "Отправьте ссылку для нового пака:",
"input_name": "Отправьте название для нового эмодзи-пака:",
"source_set": (
"<b>Исходный пак задан</b>\n"
"<blockquote>{link}\n"
"Эмодзи: {count}</blockquote>"
),
"source_invalid_format": (
"<b>Неверный формат</b>\n"
"<blockquote>Ссылка должна начинаться с https://t.me/addemoji/\n"
"Пример: https://t.me/addemoji/MyPack</blockquote>"
),
"source_invalid_resolve": (
"<b>Пак не найден</b>\n"
"<blockquote>Не удалось найти эмодзи-пак по этой ссылке.\n"
"Убедитесь что ссылка правильная и пак существует.</blockquote>"
),
"short_set": (
"<b>Ссылка нового пака задана</b>\n"
"<blockquote>{link}</blockquote>"
),
"short_invalid_format": (
"<b>Неверный формат</b>\n"
"<blockquote>Ссылка должна начинаться с https://t.me/addemoji/\n"
"Пример: https://t.me/addemoji/MyNewPack</blockquote>"
),
"short_occupied": (
"<b>Ссылка занята</b>\n"
"<blockquote>Это короткое имя уже используется.\n"
"Выберите другую ссылку.</blockquote>"
),
"name_set": (
"<b>Название задано</b>\n"
"<blockquote>{name}</blockquote>"
),
"no_source": (
"<b>Нет исходного пака</b>\n"
"<blockquote>Сначала укажите ссылку на исходный пак</blockquote>"
),
"no_short": (
"<b>Нет ссылки нового пака</b>\n"
"<blockquote>Сначала укажите ссылку для нового пака</blockquote>"
),
"no_name": (
"<b>Нет названия</b>\n"
"<blockquote>Сначала укажите название пака</blockquote>"
),
"copying": (
"<b>Копирование</b>\n"
"<blockquote>Прогресс: {current}/{total}\n"
"Пак: {name}{flood}</blockquote>"
),
"done": (
"<b>Готово</b>\n"
"<blockquote>Пак: <b>{name}</b>\n"
"Эмодзи: {count}</blockquote>\n"
"<blockquote><a href='https://t.me/addemoji/{short}'>Открыть пак</a></blockquote>"
),
"done_partial": (
"<b>Готово с ошибками</b>\n"
"<blockquote>Пак: <b>{name}</b>\n"
"Скопировано: {copied}/{total}\n"
"Ошибок: {failed}</blockquote>\n"
"<blockquote><a href='https://t.me/addemoji/{short}'>Открыть пак</a></blockquote>"
),
"copy_failed": (
"<b>Ошибка копирования</b>\n"
"<blockquote>Не удалось скопировать ни одного эмодзи.\n"
"Ошибок: {failed}</blockquote>"
),
"error": (
"<b>Ошибка</b>\n"
"<blockquote>{error}</blockquote>"
),
"no_reply_emoji": (
"<b>Нет кастомного эмодзи</b>\n"
"<blockquote>Ответьте на сообщение с кастомным эмодзи</blockquote>"
),
"status_not_set": "Не задано",
"checking": "Проверяем...",
"btn_left": "<",
"btn_right": ">",
"eadd_no_packs": (
"<b>Паки не найдены</b>\n"
"<blockquote>У вас пока нет созданных эмодзи-паков.</blockquote>"
),
"eadd_list": (
"<b>Eadd - Выбор пака</b>\n"
"<blockquote>Страница {page}/{total_pages}\n"
"Всего паков: {total}</blockquote>"
),
"eadd_pack_detail": (
"<b>Eadd - Пак выбран</b>\n"
"<blockquote>{title}\n"
"@{short}\n"
"Эмодзи: {count}</blockquote>"
),
"eadd_btn_select": "Добавить сюда",
"eadd_input_source": "Отправьте ссылку на исходный пак (https://t.me/addemoji/PackName):",
"eadd_ask_source": (
"<b>Целевой пак: {title}</b>\n"
"<blockquote>Отправьте ссылку на исходный эмодзи-пак для получения эмодзи.</blockquote>"
),
"eadd_source_set": (
"<b>Исходный пак получен</b>\n"
"<blockquote>{link}\n"
"Эмодзи: {count}</blockquote>\n"
"Теперь отправьте ID эмодзи для добавления."
),
"eadd_btn_id": "ID эмодзи",
"eadd_input_id": "Отправьте числовой ID эмодзи из полученного исходного пака:",
"eadd_id_invalid": (
"<b>Неверный ID</b>\n"
"<blockquote>ID эмодзи должен быть числом.</blockquote>"
),
"eadd_id_not_found": (
"<b>Эмодзи не найден</b>\n"
"<blockquote>Эмодзи с ID {id} не найден в полученном исходном паке.</blockquote>"
),
"eadd_adding": (
"<b>Добавление эмодзи</b>\n"
"<blockquote>Пожалуйста, подождите...</blockquote>"
),
"eadd_done": (
"<b>Эмодзи добавлен</b>\n"
"<blockquote>Пак: <b>{title}</b>\n"
"ID эмодзи: {id}</blockquote>\n"
"<blockquote><a href='https://t.me/addemoji/{short}'>Открыть пак</a></blockquote>"
),
"eadd_done_reply": (
"<b>Эмодзи добавлен</b>\n"
"<blockquote>Пак: <b>{title}</b></blockquote>\n"
"<blockquote><a href='https://t.me/addemoji/{short}'>Открыть пак</a></blockquote>"
),
"eadd_fail": (
"<b>Ошибка добавления</b>\n"
"<blockquote>{error}</blockquote>"
),
}
def __init__(self):
self._state = {
"source_link": None,
"source_short": None,
"source_documents": None,
"new_short": None,
"name": None,
}
self._eadd_state = {
"packs": [],
"page": 0,
"target_short": None,
"target_title": None,
"target_count": None,
"source_link": None,
"source_documents": None,
"reply_doc": None,
}
async def client_ready(self, client, db):
self._client = client
_install_deps()
def _get_emoji_alt(self, doc) -> str:
try:
from telethon.tl.types import DocumentAttributeCustomEmoji, DocumentAttributeSticker
for attr in doc.attributes:
if isinstance(attr, (DocumentAttributeCustomEmoji, DocumentAttributeSticker)):
return attr.alt or "⭐"
except Exception:
pass
return "⭐"
def _extract_reply_emoji_doc_id(self, message) -> int | None:
from telethon.tl.types import MessageEntityCustomEmoji
if not message:
return None
entities = getattr(message, "entities", None) or []
for ent in entities:
if isinstance(ent, MessageEntityCustomEmoji):
return ent.document_id
return None
async def _resolve_doc_by_id(self, document_id: int):
from telethon.tl.functions.messages import GetCustomEmojiDocumentsRequest
try:
docs = await self._client(GetCustomEmojiDocumentsRequest(document_id=[document_id]))
if docs:
return docs[0]
except Exception as e:
logger.error(f"[EmojiClone] _resolve_doc_by_id {document_id}: {e}")
return None
async def _with_floodwait(self, coro, call, current, total, name):
from telethon.errors import FloodWaitError
while True:
try:
return await coro
except FloodWaitError as e:
wait = e.seconds
extra = random.randint(1, 10)
flood_line = f"\nGot floodwait, waiting {wait} + {extra} seconds"
try:
await call.edit(
self.strings["copying"].format(
current=current,
total=total,
name=name,
flood=flood_line,
)
)
except Exception:
pass
await asyncio.sleep(wait + extra)
async def _upload_emoji_doc(self, doc, call, current, total, name):
from telethon.tl.functions.messages import UploadMediaRequest
from telethon.tl.types import (
InputPeerSelf,
InputMediaUploadedDocument,
DocumentAttributeFilename,
DocumentAttributeCustomEmoji,
InputStickerSetEmpty,
InputDocument,
)
try:
buf = io.BytesIO()
await self._client.download_file(doc, buf)
raw = buf.getvalue()
mime = doc.mime_type or "application/x-tgsticker"
alt = self._get_emoji_alt(doc)
if mime == "application/x-tgsticker":
fname = "emoji.tgs"
elif mime == "video/webm":
fname = "emoji.webm"
else:
fname = "emoji.webp"
file_buf = io.BytesIO(raw)
file_buf.name = fname
uploaded = await self._client.upload_file(file_buf)
media = InputMediaUploadedDocument(
file=uploaded,
mime_type=mime,
attributes=[
DocumentAttributeFilename(file_name=fname),
DocumentAttributeCustomEmoji(
alt=alt,
stickerset=InputStickerSetEmpty(),
free=False,
text_color=False,
),
],
)
result = await self._with_floodwait(
self._client(UploadMediaRequest(peer=InputPeerSelf(), media=media)),
call, current, total, name,
)
d = result.document
return InputDocument(d.id, d.access_hash, d.file_reference), alt
except Exception as e:
logger.error(f"[EmojiClone] _upload_emoji_doc: {e}")
return None, None
async def _try_resolve_pack(self, short_name: str):
from telethon.tl.functions.messages import GetStickerSetRequest
from telethon.tl.types import InputStickerSetShortName
try:
result = await self._client(GetStickerSetRequest(
stickerset=InputStickerSetShortName(short_name=short_name),
hash=random.randint(-2147483647, 2147483647),
))
return result
except Exception as e:
logger.info(f"[EmojiClone] _try_resolve_pack '{short_name}': {e}")
return None
def _extract_short_name(self, link: str):
m = ADDEMOJI_RE.match(link.strip())
if m:
return m.group(1)
return None
def _format_state_menu(self):
s = self._state
source_status = s["source_link"] if s["source_link"] else self.strings["status_not_set"]
short_status = f"https://t.me/addemoji/{s['new_short']}" if s["new_short"] else self.strings["status_not_set"]
name_status = s["name"] if s["name"] else self.strings["status_not_set"]
return self.strings["state_menu"].format(
source_status=source_status,
short_status=short_status,
name_status=name_status,
)
def _get_state_markup(self):
return [
[
{"text": self.strings["btn_set_source"], "input": self.strings["input_source"], "handler": self._cb_set_source, "style": "primary"},
],
[
{"text": self.strings["btn_set_short"], "input": self.strings["input_short"], "handler": self._cb_set_short, "style": "primary"},
{"text": self.strings["btn_set_name"], "input": self.strings["input_name"], "handler": self._cb_set_name, "style": "primary"},
],
[{"text": self.strings["btn_start"], "callback": self._cb_start, "style": "success"}],
[{"text": self.strings["btn_close"], "callback": self._cb_close, "style": "danger"}],
]
def _format_eadd_list(self):
s = self._eadd_state
packs = s["packs"]
page = s["page"]
total = len(packs)
total_pages = max(1, (total + PACKS_PER_PAGE - 1) // PACKS_PER_PAGE)
return self.strings["eadd_list"].format(
page=page + 1,
total_pages=total_pages,
total=total,
)
def _get_eadd_list_markup(self):
s = self._eadd_state
packs = s["packs"]
page = s["page"]
total = len(packs)
total_pages = max(1, (total + PACKS_PER_PAGE - 1) // PACKS_PER_PAGE)
start = page * PACKS_PER_PAGE
end = min(start + PACKS_PER_PAGE, total)
page_packs = packs[start:end]
pack_buttons = []
for i, pack in enumerate(page_packs):
pack_buttons.append({
"text": pack.title[:32],
"callback": self._cb_eadd_pack_btn,
"args": (start + i,),
"style": "primary",
})
rows = [[b] for b in pack_buttons]
nav_row = []
if page > 0:
nav_row.append({"text": self.strings["btn_left"], "callback": self._cb_eadd_page_left, "style": "primary"})
if page < total_pages - 1:
nav_row.append({"text": self.strings["btn_right"], "callback": self._cb_eadd_page_right, "style": "primary"})
if nav_row:
rows.append(nav_row)
rows.append([{"text": self.strings["btn_close"], "callback": self._cb_close, "style": "danger"}])
return rows
async def _cb_state_menu(self, call: InlineCall):
await call.edit(self._format_state_menu(), reply_markup=self._get_state_markup())
async def _cb_close(self, call: InlineCall):
await call.delete()
async def _cb_set_source(self, call: InlineCall, query: str):
link = query.strip()
short_name = self._extract_short_name(link)
if not short_name:
await call.edit(
self.strings["source_invalid_format"],
reply_markup=[
[{"text": self.strings["btn_retry"], "input": self.strings["input_source"], "handler": self._cb_set_source, "style": "primary"}],
[{"text": self.strings["btn_back"], "callback": self._cb_state_menu, "style": "danger"}],
],
)
return
await call.edit(self.strings["checking"])
result = await self._try_resolve_pack(short_name)
if not result or not result.documents:
await call.edit(
self.strings["source_invalid_resolve"],
reply_markup=[
[{"text": self.strings["btn_retry"], "input": self.strings["input_source"], "handler": self._cb_set_source, "style": "primary"}],
[{"text": self.strings["btn_back"], "callback": self._cb_state_menu, "style": "danger"}],
],
)
return
self._state["source_link"] = link
self._state["source_short"] = short_name
self._state["source_documents"] = result.documents
await call.edit(
self.strings["source_set"].format(link=link, count=len(result.documents)),
reply_markup=[[{"text": self.strings["btn_back"], "callback": self._cb_state_menu, "style": "danger"}]],
)
async def _cb_set_short(self, call: InlineCall, query: str):
link = query.strip()
short_name = self._extract_short_name(link)
if not short_name:
await call.edit(
self.strings["short_invalid_format"],
reply_markup=[
[{"text": self.strings["btn_retry"], "input": self.strings["input_short"], "handler": self._cb_set_short, "style": "primary"}],
[{"text": self.strings["btn_back"], "callback": self._cb_state_menu, "style": "danger"}],
],
)
return
await call.edit(self.strings["checking"])
result = await self._try_resolve_pack(short_name)
if result is not None:
await call.edit(
self.strings["short_occupied"],
reply_markup=[
[{"text": self.strings["btn_retry"], "input": self.strings["input_short"], "handler": self._cb_set_short, "style": "primary"}],
[{"text": self.strings["btn_back"], "callback": self._cb_state_menu, "style": "danger"}],
],
)
return
self._state["new_short"] = short_name
await call.edit(
self.strings["short_set"].format(link=link),
reply_markup=[[{"text": self.strings["btn_back"], "callback": self._cb_state_menu, "style": "danger"}]],
)
async def _cb_set_name(self, call: InlineCall, query: str):
name = query.strip()
self._state["name"] = name if name else None
await call.edit(
self.strings["name_set"].format(name=name),
reply_markup=[[{"text": self.strings["btn_back"], "callback": self._cb_state_menu, "style": "danger"}]],
)
async def _cb_start(self, call: InlineCall):
if not self._state["source_documents"]:
await call.edit(
self.strings["no_source"],
reply_markup=[[{"text": self.strings["btn_back"], "callback": self._cb_state_menu, "style": "danger"}]],
)
return
if not self._state["new_short"]:
await call.edit(
self.strings["no_short"],
reply_markup=[[{"text": self.strings["btn_back"], "callback": self._cb_state_menu, "style": "danger"}]],
)
return
if not self._state["name"]:
await call.edit(
self.strings["no_name"],
reply_markup=[[{"text": self.strings["btn_back"], "callback": self._cb_state_menu, "style": "danger"}]],
)
return
from telethon.tl.functions.stickers import CreateStickerSetRequest, AddStickerToSetRequest
from telethon.tl.functions.messages import UninstallStickerSetRequest
from telethon.tl.types import InputStickerSetShortName, InputStickerSetItem, InputUserSelf
from telethon.errors.rpcerrorlist import PackShortNameOccupiedError
documents = self._state["source_documents"]
pack_title = self._state["name"]
short_name = self._state["new_short"]
total = len(documents)
pack_created = False
copied = 0
failed = 0
for i, doc in enumerate(documents, 1):
try:
await call.edit(
self.strings["copying"].format(
current=i,
total=total,
name=pack_title,
flood="",
)
)
except Exception:
pass
input_doc, alt = await self._upload_emoji_doc(doc, call, i, total, pack_title)
if input_doc is None:
failed += 1
continue
try:
if not pack_created:
await self._with_floodwait(
self._client(CreateStickerSetRequest(
user_id=InputUserSelf(),
title=pack_title,
short_name=short_name,
stickers=[InputStickerSetItem(document=input_doc, emoji=alt)],
emojis=True,
)),
call, i, total, pack_title,
)
pack_created = True
copied += 1
else:
await self._with_floodwait(
self._client(AddStickerToSetRequest(
stickerset=InputStickerSetShortName(short_name=short_name),
sticker=InputStickerSetItem(document=input_doc, emoji=alt),
)),
call, i, total, pack_title,
)
copied += 1
except PackShortNameOccupiedError:
await call.edit(
self.strings["short_occupied"],
reply_markup=[[{"text": self.strings["btn_back"], "callback": self._cb_state_menu, "style": "danger"}]],
)
return
except Exception as e:
logger.error(f"[EmojiClone] add emoji {i}/{total}: {e}")
failed += 1
if not pack_created:
await call.edit(
self.strings["copy_failed"].format(failed=failed),
reply_markup=[[{"text": self.strings["btn_back"], "callback": self._cb_state_menu, "style": "danger"}]],
)
return
try:
await self._client(UninstallStickerSetRequest(
stickerset=InputStickerSetShortName(short_name=short_name)
))
except Exception:
pass
self._state = {
"source_link": None,
"source_short": None,
"source_documents": None,
"new_short": None,
"name": None,
}
if failed == 0:
await call.edit(
self.strings["done"].format(name=pack_title, count=copied, short=short_name),
reply_markup=[[{"text": self.strings["btn_close"], "callback": self._cb_close, "style": "danger"}]],
)
else:
await call.edit(
self.strings["done_partial"].format(
name=pack_title, copied=copied, total=total, failed=failed, short=short_name
),
reply_markup=[[{"text": self.strings["btn_close"], "callback": self._cb_close, "style": "danger"}]],
)
async def _cb_eadd_page_left(self, call: InlineCall):
s = self._eadd_state
if s["page"] <= 0:
await call.answer()
return
s["page"] -= 1
await call.edit(self._format_eadd_list(), reply_markup=self._get_eadd_list_markup())
async def _cb_eadd_page_right(self, call: InlineCall):
s = self._eadd_state
total = len(s["packs"])
total_pages = max(1, (total + PACKS_PER_PAGE - 1) // PACKS_PER_PAGE)
if s["page"] >= total_pages - 1:
await call.answer()
return
s["page"] += 1
await call.edit(self._format_eadd_list(), reply_markup=self._get_eadd_list_markup())
async def _cb_eadd_pack_btn(self, call: InlineCall, pack_index: int):
s = self._eadd_state
pack = s["packs"][pack_index]
await call.edit(
self.strings["eadd_pack_detail"].format(
title=pack.title,
short=pack.short_name,
count=pack.count,
),
reply_markup=[
[{"text": self.strings["eadd_btn_select"], "callback": self._cb_eadd_confirm_select, "args": (pack_index,), "style": "success"}],
[{"text": self.strings["btn_back"], "callback": self._cb_eadd_back_list, "style": "danger"}],
],
)
async def _cb_eadd_back_list(self, call: InlineCall):
await call.edit(self._format_eadd_list(), reply_markup=self._get_eadd_list_markup())
async def _cb_eadd_confirm_select(self, call: InlineCall, pack_index: int):
s = self._eadd_state
pack = s["packs"][pack_index]
s["target_short"] = pack.short_name
s["target_title"] = pack.title
s["target_count"] = pack.count
logger.info(f"[EmojiClone] eadd target selected: {pack.short_name}")
reply_doc = s.get("reply_doc")
if reply_doc is not None:
await self._eadd_do_add_reply(call, reply_doc)
return
await call.edit(
self.strings["eadd_ask_source"].format(title=pack.title),
reply_markup=[
[{"text": self.strings["btn_set_source"], "input": self.strings["eadd_input_source"], "handler": self._cb_eadd_set_source, "style": "primary"}],
[{"text": self.strings["btn_close"], "callback": self._cb_close, "style": "danger"}],
],
)
async def _eadd_do_add_reply(self, call: InlineCall, doc):
from telethon.tl.functions.stickers import AddStickerToSetRequest
from telethon.tl.types import InputStickerSetShortName, InputStickerSetItem, InputDocument
from telethon.errors import FloodWaitError
s = self._eadd_state
target_short = s["target_short"]
target_title = s["target_title"]
alt = self._get_emoji_alt(doc)
await call.edit(self.strings["eadd_adding"])
try:
input_doc, alt = await self._upload_emoji_doc(doc, call, 1, 1, target_title)
if input_doc is None:
raise Exception("upload failed")
await self._client(AddStickerToSetRequest(
stickerset=InputStickerSetShortName(short_name=target_short),
sticker=InputStickerSetItem(
document=input_doc,
emoji=alt,
),
))
except FloodWaitError as e:
logger.info(f"[EmojiClone] eadd reply FloodWait {e.seconds}s")
await call.edit(
self.strings["eadd_fail"].format(error=f"FloodWait {e.seconds}s"),
reply_markup=[[{"text": self.strings["btn_close"], "callback": self._cb_close, "style": "danger"}]],
)
return
except Exception as e:
logger.error(f"[EmojiClone] eadd reply add error: {e}")
await call.edit(
self.strings["eadd_fail"].format(error=str(e)),
reply_markup=[[{"text": self.strings["btn_close"], "callback": self._cb_close, "style": "danger"}]],
)
return
logger.info(f"[EmojiClone] eadd reply: added emoji {doc.id} to {target_short}")
s["reply_doc"] = None
await call.edit(
self.strings["eadd_done_reply"].format(title=target_title, short=target_short),
reply_markup=[[{"text": self.strings["btn_close"], "callback": self._cb_close, "style": "danger"}]],
)
async def _cb_eadd_set_source(self, call: InlineCall, query: str):
link = query.strip()
short_name = self._extract_short_name(link)
if not short_name:
await call.edit(
self.strings["source_invalid_format"],
reply_markup=[
[{"text": self.strings["btn_retry"], "input": self.strings["eadd_input_source"], "handler": self._cb_eadd_set_source, "style": "primary"}],
[{"text": self.strings["btn_close"], "callback": self._cb_close, "style": "danger"}],
],
)
return
await call.edit(self.strings["checking"])
result = await self._try_resolve_pack(short_name)
if not result or not result.documents:
await call.edit(
self.strings["source_invalid_resolve"],
reply_markup=[
[{"text": self.strings["btn_retry"], "input": self.strings["eadd_input_source"], "handler": self._cb_eadd_set_source, "style": "primary"}],
[{"text": self.strings["btn_close"], "callback": self._cb_close, "style": "danger"}],
],
)
return
s = self._eadd_state
s["source_link"] = link
s["source_documents"] = {doc.id: doc for doc in result.documents}
logger.info(f"[EmojiClone] eadd source resolved: {short_name}, {len(result.documents)} emoji")
await call.edit(
self.strings["eadd_source_set"].format(link=link, count=len(result.documents)),
reply_markup=[
[{"text": self.strings["eadd_btn_id"], "input": self.strings["eadd_input_id"], "handler": self._cb_eadd_set_id, "style": "primary"}],
[{"text": self.strings["btn_close"], "callback": self._cb_close, "style": "danger"}],
],
)
async def _cb_eadd_set_id(self, call: InlineCall, query: str):
s = self._eadd_state
raw_id = query.strip()
try:
emoji_id = int(raw_id)
except ValueError:
await call.edit(
self.strings["eadd_id_invalid"],
reply_markup=[
[{"text": self.strings["btn_retry"], "input": self.strings["eadd_input_id"], "handler": self._cb_eadd_set_id, "style": "primary"}],
[{"text": self.strings["btn_close"], "callback": self._cb_close, "style": "danger"}],
],
)
return
doc = (s["source_documents"] or {}).get(emoji_id)
if not doc:
await call.edit(
self.strings["eadd_id_not_found"].format(id=emoji_id),
reply_markup=[
[{"text": self.strings["btn_retry"], "input": self.strings["eadd_input_id"], "handler": self._cb_eadd_set_id, "style": "primary"}],
[{"text": self.strings["btn_close"], "callback": self._cb_close, "style": "danger"}],
],
)
return
await call.edit(self.strings["eadd_adding"])
from telethon.tl.functions.stickers import AddStickerToSetRequest
from telethon.tl.types import InputStickerSetShortName, InputStickerSetItem
from telethon.errors import FloodWaitError
target_short = s["target_short"]
try:
input_doc, alt = await self._upload_emoji_doc(doc, call, 1, 1, s["target_title"])
if input_doc is None:
raise Exception("upload failed")
await self._client(AddStickerToSetRequest(
stickerset=InputStickerSetShortName(short_name=target_short),
sticker=InputStickerSetItem(document=input_doc, emoji=alt),
))
except FloodWaitError as e:
logger.info(f"[EmojiClone] eadd FloodWait {e.seconds}s")
await call.edit(
self.strings["eadd_fail"].format(error=f"FloodWait {e.seconds}s"),
reply_markup=[[{"text": self.strings["btn_close"], "callback": self._cb_close, "style": "danger"}]],
)
return
except Exception as e:
logger.error(f"[EmojiClone] eadd add error: {e}")
await call.edit(
self.strings["eadd_fail"].format(error=str(e)),
reply_markup=[[{"text": self.strings["btn_close"], "callback": self._cb_close, "style": "danger"}]],
)
return
logger.info(f"[EmojiClone] eadd: added emoji {emoji_id} to {target_short}")
await call.edit(
self.strings["eadd_done"].format(title=s["target_title"], id=emoji_id, short=target_short),
reply_markup=[[{"text": self.strings["btn_close"], "callback": self._cb_close, "style": "danger"}]],
)
@loader.command(
ru_doc="Открыть меню клонирования эмодзи-пака",
en_doc="Open emoji pack cloner menu",
)
async def eclone(self, message):
"""Open emoji pack cloner menu"""