taslim19 commited on
Commit
7710b6e
·
1 Parent(s): 3e70f93

Add kang and get_sticker management plugin adapted for DragMusic

Browse files
DragMusic/plugins/management/kang.py ADDED
@@ -0,0 +1,190 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import imghdr
3
+ import os
4
+ from asyncio import gather
5
+ from traceback import format_exc
6
+
7
+ from pyrogram import filters
8
+ from pyrogram.errors import (
9
+ PeerIdInvalid,
10
+ ShortnameOccupyFailed,
11
+ StickerEmojiInvalid,
12
+ StickerPngDimensions,
13
+ StickerPngNopng,
14
+ UserIsBlocked,
15
+ )
16
+ from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, Message
17
+ from DragMusic import app
18
+ from config import BOT_USERNAME
19
+ from DragMusic.utils.errors import capture_err
20
+
21
+ from DragMusic.utils.files import (
22
+ get_document_from_file_id,
23
+ resize_file_to_sticker_size,
24
+ upload_document,
25
+ )
26
+
27
+ from DragMusic.utils.stickerset import (
28
+ add_sticker_to_set,
29
+ create_sticker,
30
+ create_sticker_set,
31
+ get_sticker_set_by_name,
32
+ )
33
+
34
+ # -----------
35
+
36
+ MAX_STICKERS = (
37
+ 120 # would be better if we could fetch this limit directly from telegram
38
+ )
39
+ SUPPORTED_TYPES = ["jpeg", "png", "webp"]
40
+ # ------------------------------------------
41
+ @app.on_message(filters.command("get_sticker"))
42
+ @capture_err
43
+ async def sticker_image(_, message: Message):
44
+ r = message.reply_to_message
45
+
46
+ if not r:
47
+ return await message.reply("Reply to a sticker.")
48
+
49
+ if not r.sticker:
50
+ return await message.reply("Reply to a sticker.")
51
+
52
+ m = await message.reply("Sending..")
53
+ f = await r.download(f"{r.sticker.file_unique_id}.png")
54
+
55
+ await gather(
56
+ *[
57
+ message.reply_photo(f),
58
+ message.reply_document(f),
59
+ ]
60
+ )
61
+
62
+ await m.delete()
63
+ os.remove(f)
64
+ #----------------
65
+ @app.on_message(filters.command("kang"))
66
+ @capture_err
67
+ async def kang(client, message: Message):
68
+ if not message.reply_to_message:
69
+ return await message.reply_text("Reply to a sticker/image to kang it.")
70
+ if not message.from_user:
71
+ return await message.reply_text(
72
+ "You are anon admin, kang stickers in my pm."
73
+ )
74
+ msg = await message.reply_text("Kanging Sticker..")
75
+
76
+ # Find the proper emoji
77
+ args = message.text.split()
78
+ if len(args) > 1:
79
+ sticker_emoji = str(args[1])
80
+ elif (
81
+ message.reply_to_message.sticker
82
+ and message.reply_to_message.sticker.emoji
83
+ ):
84
+ sticker_emoji = message.reply_to_message.sticker.emoji
85
+ else:
86
+ sticker_emoji = "🤔"
87
+
88
+ # Get the corresponding fileid, resize the file if necessary
89
+ doc = message.reply_to_message.photo or message.reply_to_message.document
90
+ try:
91
+ if message.reply_to_message.sticker:
92
+ sticker = await create_sticker(
93
+ await get_document_from_file_id(
94
+ message.reply_to_message.sticker.file_id
95
+ ),
96
+ sticker_emoji,
97
+ )
98
+ elif doc:
99
+ if doc.file_size > 10000000:
100
+ return await msg.edit("File size too large.")
101
+
102
+ temp_file_path = await app.download_media(doc)
103
+ image_type = imghdr.what(temp_file_path)
104
+ if image_type not in SUPPORTED_TYPES:
105
+ return await msg.edit(
106
+ "Format not supported! ({})".format(image_type)
107
+ )
108
+ try:
109
+ temp_file_path = await resize_file_to_sticker_size(
110
+ temp_file_path
111
+ )
112
+ except OSError as e:
113
+ await msg.edit_text("Something wrong happened.")
114
+ raise Exception(
115
+ f"Something went wrong while resizing the sticker (at {temp_file_path}); {e}"
116
+ )
117
+ sticker = await create_sticker(
118
+ await upload_document(client, temp_file_path, message.chat.id),
119
+ sticker_emoji,
120
+ )
121
+ if os.path.isfile(temp_file_path):
122
+ os.remove(temp_file_path)
123
+ else:
124
+ return await msg.edit("Nope, can't kang that.")
125
+ except ShortnameOccupyFailed:
126
+ await message.reply_text("Change Your Name Or Username")
127
+ return
128
+
129
+ except Exception as e:
130
+ await message.reply_text(str(e))
131
+ e = format_exc()
132
+ return print(e)
133
+ #-------
134
+ packnum = 0
135
+ packname = "f" + str(message.from_user.id) + "_by_" + BOT_USERNAME
136
+ limit = 0
137
+ try:
138
+ while True:
139
+ # Prevent infinite rules
140
+ if limit >= 50:
141
+ return await msg.delete()
142
+
143
+ stickerset = await get_sticker_set_by_name(client, packname)
144
+ if not stickerset:
145
+ stickerset = await create_sticker_set(
146
+ client,
147
+ message.from_user.id,
148
+ f"{message.from_user.first_name[:32]}'s kang pack",
149
+ packname,
150
+ [sticker],
151
+ )
152
+ elif stickerset.set.count >= MAX_STICKERS:
153
+ packnum += 1
154
+ packname = (
155
+ "f"
156
+ + str(packnum)
157
+ + "_"
158
+ + str(message.from_user.id)
159
+ + "_by_"
160
+ + BOT_USERNAME
161
+ )
162
+ limit += 1
163
+ continue
164
+ else:
165
+ try:
166
+ await add_sticker_to_set(client, stickerset, sticker)
167
+ except StickerEmojiInvalid:
168
+ return await msg.edit("[ERROR]: INVALID_EMOJI_IN_ARGUMENT")
169
+ limit += 1
170
+ break
171
+
172
+ await msg.edit(
173
+ "Sticker Kanged To [Pack](t.me/addstickers/{})\nEmoji: {}".format(
174
+ packname, sticker_emoji
175
+ )
176
+ )
177
+ except (PeerIdInvalid, UserIsBlocked):
178
+ keyboard = InlineKeyboardMarkup(
179
+ [[InlineKeyboardButton(text="Start", url=f"t.me/{BOT_USERNAME}")]]
180
+ )
181
+ await msg.edit(
182
+ "You Need To Start A Private Chat With Me.",
183
+ reply_markup=keyboard,
184
+ )
185
+ except StickerPngNopng:
186
+ await message.reply_text(
187
+ "Stickers must be png files but the provided image was not a png"
188
+ )
189
+ except StickerPngDimensions:
190
+ await message.reply_text("The sticker png dimensions are invalid.")
DragMusic/utils/files.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import os
3
+
4
+ from PIL import Image
5
+ from pyrogram import Client, raw
6
+ from pyrogram.file_id import FileId
7
+ # ------------------
8
+ STICKER_DIMENSIONS = (512, 512)
9
+
10
+ # -------------------
11
+ async def resize_file_to_sticker_size(file_path: str) -> str:
12
+ im = Image.open(file_path)
13
+ if (im.width, im.height) < STICKER_DIMENSIONS:
14
+ size1 = im.width
15
+ size2 = im.height
16
+ if im.width > im.height:
17
+ scale = STICKER_DIMENSIONS[0] / size1
18
+ size1new = STICKER_DIMENSIONS[0]
19
+ size2new = size2 * scale
20
+ else:
21
+ scale = STICKER_DIMENSIONS[1] / size2
22
+ size1new = size1 * scale
23
+ size2new = STICKER_DIMENSIONS[1]
24
+ size1new = math.floor(size1new)
25
+ size2new = math.floor(size2new)
26
+ sizenew = (size1new, size2new)
27
+ im = im.resize(sizenew)
28
+ else:
29
+ im.thumbnail(STICKER_DIMENSIONS)
30
+ try:
31
+ os.remove(file_path)
32
+ file_path = f"{file_path}.png"
33
+ return file_path
34
+ finally:
35
+ im.save(file_path)
36
+
37
+
38
+ async def upload_document(
39
+ client: Client, file_path: str, chat_id: int
40
+ ) -> raw.base.InputDocument:
41
+ media = await client.invoke(
42
+ raw.functions.messages.UploadMedia(
43
+ peer=await client.resolve_peer(chat_id),
44
+ media=raw.types.InputMediaUploadedDocument(
45
+ mime_type=client.guess_mime_type(file_path)
46
+ or "application/zip",
47
+ file=await client.save_file(file_path),
48
+ attributes=[
49
+ raw.types.DocumentAttributeFilename(
50
+ file_name=os.path.basename(file_path)
51
+ )
52
+ ],
53
+ ),
54
+ )
55
+ )
56
+ return raw.types.InputDocument(
57
+ id=media.document.id,
58
+ access_hash=media.document.access_hash,
59
+ file_reference=media.document.file_reference,
60
+ )
61
+
62
+
63
+ async def get_document_from_file_id(
64
+ file_id: str,
65
+ ) -> raw.base.InputDocument:
66
+ decoded = FileId.decode(file_id)
67
+ return raw.types.InputDocument(
68
+ id=decoded.media_id,
69
+ access_hash=decoded.access_hash,
70
+ file_reference=decoded.file_reference,
71
+ )
DragMusic/utils/stickerset.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List
2
+
3
+ from pyrogram import Client, errors, raw
4
+
5
+
6
+ async def get_sticker_set_by_name(
7
+ client: Client, name: str
8
+ ) -> raw.base.messages.StickerSet:
9
+ try:
10
+ return await client.invoke(
11
+ raw.functions.messages.GetStickerSet(
12
+ stickerset=raw.types.InputStickerSetShortName(short_name=name),
13
+ hash=0,
14
+ )
15
+ )
16
+ except errors.exceptions.not_acceptable_406.StickersetInvalid:
17
+ return None
18
+
19
+
20
+
21
+
22
+ async def create_sticker_set(
23
+ client: Client,
24
+ owner: int,
25
+ title: str,
26
+ short_name: str,
27
+ stickers: List[raw.base.InputStickerSetItem],
28
+ ) -> raw.base.messages.StickerSet:
29
+ return await client.invoke(
30
+ raw.functions.stickers.CreateStickerSet(
31
+ user_id=await client.resolve_peer(owner),
32
+ title=title,
33
+ short_name=short_name,
34
+ stickers=stickers,
35
+ )
36
+ )
37
+
38
+
39
+ async def add_sticker_to_set(
40
+ client: Client,
41
+ stickerset: raw.base.messages.StickerSet,
42
+ sticker: raw.base.InputStickerSetItem,
43
+ ) -> raw.base.messages.StickerSet:
44
+ return await client.invoke(
45
+ raw.functions.stickers.AddStickerToSet(
46
+ stickerset=raw.types.InputStickerSetShortName(
47
+ short_name=stickerset.set.short_name
48
+ ),
49
+ sticker=sticker,
50
+ )
51
+ )
52
+
53
+
54
+ async def create_sticker(
55
+ sticker: raw.base.InputDocument, emoji: str
56
+ ) -> raw.base.InputStickerSetItem:
57
+ return raw.types.InputStickerSetItem(document=sticker, emoji=emoji)