taslim19
commited on
Commit
·
db044f6
1
Parent(s):
93ddaf5
Fix quotly.py: remove await on bytes object for /q command
Browse files
DragMusic/plugins/management/kang.py
CHANGED
@@ -13,16 +13,15 @@ from pyrogram.errors import (
|
|
13 |
UserIsBlocked,
|
14 |
)
|
15 |
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, Message
|
|
|
16 |
from DragMusic import app
|
17 |
from config import BOT_USERNAME
|
18 |
from DragMusic.utils.errors import capture_err
|
19 |
-
|
20 |
from DragMusic.utils.files import (
|
21 |
get_document_from_file_id,
|
22 |
resize_file_to_sticker_size,
|
23 |
upload_document,
|
24 |
)
|
25 |
-
|
26 |
from DragMusic.utils.stickerset import (
|
27 |
add_sticker_to_set,
|
28 |
create_sticker,
|
@@ -30,11 +29,9 @@ from DragMusic.utils.stickerset import (
|
|
30 |
get_sticker_set_by_name,
|
31 |
)
|
32 |
|
33 |
-
# -----------
|
34 |
-
|
35 |
MAX_STICKERS = 120
|
36 |
SUPPORTED_TYPES = ["jpeg", "png", "webp"]
|
37 |
-
|
38 |
@app.on_message(filters.command("get_sticker"))
|
39 |
@capture_err
|
40 |
async def sticker_image(_, message: Message):
|
@@ -58,31 +55,26 @@ async def sticker_image(_, message: Message):
|
|
58 |
|
59 |
await m.delete()
|
60 |
os.remove(f)
|
61 |
-
|
62 |
@app.on_message(filters.command("kang"))
|
63 |
@capture_err
|
64 |
async def kang(client, message: Message):
|
65 |
if not message.reply_to_message:
|
66 |
return await message.reply_text("Reply to a sticker/image to kang it.")
|
67 |
if not message.from_user:
|
68 |
-
return await message.reply_text(
|
69 |
-
|
70 |
-
)
|
71 |
msg = await message.reply_text("Kanging Sticker..")
|
72 |
|
73 |
-
#
|
74 |
args = message.text.split()
|
75 |
if len(args) > 1:
|
76 |
sticker_emoji = str(args[1])
|
77 |
-
elif
|
78 |
-
message.reply_to_message.sticker
|
79 |
-
and message.reply_to_message.sticker.emoji
|
80 |
-
):
|
81 |
sticker_emoji = message.reply_to_message.sticker.emoji
|
82 |
else:
|
83 |
sticker_emoji = "🤔"
|
84 |
|
85 |
-
# Get the corresponding fileid, resize the file if necessary
|
86 |
doc = message.reply_to_message.photo or message.reply_to_message.document
|
87 |
try:
|
88 |
if message.reply_to_message.sticker:
|
@@ -93,24 +85,20 @@ async def kang(client, message: Message):
|
|
93 |
sticker_emoji,
|
94 |
)
|
95 |
elif doc:
|
96 |
-
if doc.file_size >
|
97 |
return await msg.edit("File size too large.")
|
98 |
|
99 |
temp_file_path = await app.download_media(doc)
|
100 |
image_type = imghdr.what(temp_file_path)
|
101 |
if image_type not in SUPPORTED_TYPES:
|
102 |
-
return await msg.edit(
|
103 |
-
|
104 |
-
)
|
105 |
try:
|
106 |
-
temp_file_path = await resize_file_to_sticker_size(
|
107 |
-
temp_file_path
|
108 |
-
)
|
109 |
except OSError as e:
|
110 |
-
await msg.edit_text("Something wrong happened.")
|
111 |
-
raise Exception(
|
112 |
-
|
113 |
-
)
|
114 |
sticker = await create_sticker(
|
115 |
await upload_document(client, temp_file_path, message.chat.id),
|
116 |
sticker_emoji,
|
@@ -120,25 +108,35 @@ async def kang(client, message: Message):
|
|
120 |
else:
|
121 |
return await msg.edit("Nope, can't kang that.")
|
122 |
except ShortnameOccupyFailed:
|
123 |
-
await message.reply_text("Change
|
124 |
return
|
125 |
-
|
126 |
except Exception as e:
|
127 |
await message.reply_text(str(e))
|
128 |
-
|
129 |
-
return
|
130 |
-
|
|
|
131 |
bot_username = BOT_USERNAME.lstrip("@").strip("_")
|
132 |
packnum = 0
|
133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
limit = 0
|
|
|
135 |
try:
|
136 |
while True:
|
137 |
if limit >= 50:
|
138 |
return await msg.delete()
|
139 |
-
|
140 |
print(f"[DEBUG] packname: {packname}")
|
141 |
print(f"[DEBUG] bot_username: {bot_username}")
|
|
|
142 |
stickerset = await get_sticker_set_by_name(client, packname)
|
143 |
if not stickerset:
|
144 |
stickerset = await create_sticker_set(
|
@@ -150,7 +148,9 @@ async def kang(client, message: Message):
|
|
150 |
)
|
151 |
elif stickerset.set.count >= MAX_STICKERS:
|
152 |
packnum += 1
|
153 |
-
packname =
|
|
|
|
|
154 |
limit += 1
|
155 |
continue
|
156 |
else:
|
@@ -158,22 +158,23 @@ async def kang(client, message: Message):
|
|
158 |
await add_sticker_to_set(client, stickerset, sticker)
|
159 |
except StickerEmojiInvalid:
|
160 |
return await msg.edit("[ERROR]: INVALID_EMOJI_IN_ARGUMENT")
|
|
|
161 |
limit += 1
|
162 |
break
|
|
|
163 |
await msg.edit(
|
164 |
-
f"Sticker Kanged To [Pack](t.me/addstickers/{packname})\nEmoji: {sticker_emoji}"
|
165 |
)
|
|
|
166 |
except (PeerIdInvalid, UserIsBlocked):
|
167 |
keyboard = InlineKeyboardMarkup(
|
168 |
-
[[InlineKeyboardButton(text="Start", url=f"t.me/{bot_username}")]]
|
169 |
)
|
170 |
await msg.edit(
|
171 |
"You Need To Start A Private Chat With Me.",
|
172 |
reply_markup=keyboard,
|
173 |
)
|
174 |
except StickerPngNopng:
|
175 |
-
await message.reply_text(
|
176 |
-
"Stickers must be png files but the provided image was not a png"
|
177 |
-
)
|
178 |
except StickerPngDimensions:
|
179 |
-
await message.reply_text("The sticker
|
|
|
13 |
UserIsBlocked,
|
14 |
)
|
15 |
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, Message
|
16 |
+
|
17 |
from DragMusic import app
|
18 |
from config import BOT_USERNAME
|
19 |
from DragMusic.utils.errors import capture_err
|
|
|
20 |
from DragMusic.utils.files import (
|
21 |
get_document_from_file_id,
|
22 |
resize_file_to_sticker_size,
|
23 |
upload_document,
|
24 |
)
|
|
|
25 |
from DragMusic.utils.stickerset import (
|
26 |
add_sticker_to_set,
|
27 |
create_sticker,
|
|
|
29 |
get_sticker_set_by_name,
|
30 |
)
|
31 |
|
|
|
|
|
32 |
MAX_STICKERS = 120
|
33 |
SUPPORTED_TYPES = ["jpeg", "png", "webp"]
|
34 |
+
|
35 |
@app.on_message(filters.command("get_sticker"))
|
36 |
@capture_err
|
37 |
async def sticker_image(_, message: Message):
|
|
|
55 |
|
56 |
await m.delete()
|
57 |
os.remove(f)
|
58 |
+
|
59 |
@app.on_message(filters.command("kang"))
|
60 |
@capture_err
|
61 |
async def kang(client, message: Message):
|
62 |
if not message.reply_to_message:
|
63 |
return await message.reply_text("Reply to a sticker/image to kang it.")
|
64 |
if not message.from_user:
|
65 |
+
return await message.reply_text("You are anon admin, kang stickers in my PM.")
|
66 |
+
|
|
|
67 |
msg = await message.reply_text("Kanging Sticker..")
|
68 |
|
69 |
+
# Determine sticker emoji
|
70 |
args = message.text.split()
|
71 |
if len(args) > 1:
|
72 |
sticker_emoji = str(args[1])
|
73 |
+
elif message.reply_to_message.sticker and message.reply_to_message.sticker.emoji:
|
|
|
|
|
|
|
74 |
sticker_emoji = message.reply_to_message.sticker.emoji
|
75 |
else:
|
76 |
sticker_emoji = "🤔"
|
77 |
|
|
|
78 |
doc = message.reply_to_message.photo or message.reply_to_message.document
|
79 |
try:
|
80 |
if message.reply_to_message.sticker:
|
|
|
85 |
sticker_emoji,
|
86 |
)
|
87 |
elif doc:
|
88 |
+
if doc.file_size > 10_000_000:
|
89 |
return await msg.edit("File size too large.")
|
90 |
|
91 |
temp_file_path = await app.download_media(doc)
|
92 |
image_type = imghdr.what(temp_file_path)
|
93 |
if image_type not in SUPPORTED_TYPES:
|
94 |
+
return await msg.edit(f"Format not supported! ({image_type})")
|
95 |
+
|
|
|
96 |
try:
|
97 |
+
temp_file_path = await resize_file_to_sticker_size(temp_file_path)
|
|
|
|
|
98 |
except OSError as e:
|
99 |
+
await msg.edit_text("Something wrong happened while resizing.")
|
100 |
+
raise Exception(f"Resize error at {temp_file_path}: {e}")
|
101 |
+
|
|
|
102 |
sticker = await create_sticker(
|
103 |
await upload_document(client, temp_file_path, message.chat.id),
|
104 |
sticker_emoji,
|
|
|
108 |
else:
|
109 |
return await msg.edit("Nope, can't kang that.")
|
110 |
except ShortnameOccupyFailed:
|
111 |
+
await message.reply_text("Change your Telegram name or username.")
|
112 |
return
|
|
|
113 |
except Exception as e:
|
114 |
await message.reply_text(str(e))
|
115 |
+
print(format_exc())
|
116 |
+
return
|
117 |
+
|
118 |
+
# Packname sanitization
|
119 |
bot_username = BOT_USERNAME.lstrip("@").strip("_")
|
120 |
packnum = 0
|
121 |
+
|
122 |
+
def sanitize_packname(base: str) -> str:
|
123 |
+
base = base.replace("__", "_").strip("_")
|
124 |
+
if not base[0].isalpha():
|
125 |
+
base = f"a{base}"
|
126 |
+
return base
|
127 |
+
|
128 |
+
base_packname = sanitize_packname(f"f{message.from_user.id}_by_{bot_username}")
|
129 |
+
packname = base_packname
|
130 |
limit = 0
|
131 |
+
|
132 |
try:
|
133 |
while True:
|
134 |
if limit >= 50:
|
135 |
return await msg.delete()
|
136 |
+
|
137 |
print(f"[DEBUG] packname: {packname}")
|
138 |
print(f"[DEBUG] bot_username: {bot_username}")
|
139 |
+
|
140 |
stickerset = await get_sticker_set_by_name(client, packname)
|
141 |
if not stickerset:
|
142 |
stickerset = await create_sticker_set(
|
|
|
148 |
)
|
149 |
elif stickerset.set.count >= MAX_STICKERS:
|
150 |
packnum += 1
|
151 |
+
packname = sanitize_packname(
|
152 |
+
f"f{packnum}_{message.from_user.id}_by_{bot_username}"
|
153 |
+
)
|
154 |
limit += 1
|
155 |
continue
|
156 |
else:
|
|
|
158 |
await add_sticker_to_set(client, stickerset, sticker)
|
159 |
except StickerEmojiInvalid:
|
160 |
return await msg.edit("[ERROR]: INVALID_EMOJI_IN_ARGUMENT")
|
161 |
+
|
162 |
limit += 1
|
163 |
break
|
164 |
+
|
165 |
await msg.edit(
|
166 |
+
f"Sticker Kanged To [Pack](https://t.me/addstickers/{packname})\nEmoji: {sticker_emoji}"
|
167 |
)
|
168 |
+
|
169 |
except (PeerIdInvalid, UserIsBlocked):
|
170 |
keyboard = InlineKeyboardMarkup(
|
171 |
+
[[InlineKeyboardButton(text="Start", url=f"https://t.me/{bot_username}")]]
|
172 |
)
|
173 |
await msg.edit(
|
174 |
"You Need To Start A Private Chat With Me.",
|
175 |
reply_markup=keyboard,
|
176 |
)
|
177 |
except StickerPngNopng:
|
178 |
+
await message.reply_text("Stickers must be PNG files but the provided image was not a PNG.")
|
|
|
|
|
179 |
except StickerPngDimensions:
|
180 |
+
await message.reply_text("The sticker PNG dimensions are invalid.")
|