stickers.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. """
  2. MIT License
  3. Copyright (c) 2024 TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. import imghdr
  21. import os
  22. from asyncio import gather
  23. from traceback import format_exc
  24. from pyrogram import filters
  25. from pyrogram.errors import (
  26. PeerIdInvalid,
  27. ShortnameOccupyFailed,
  28. StickerEmojiInvalid,
  29. StickerPngDimensions,
  30. StickerPngNopng,
  31. UserIsBlocked,
  32. )
  33. from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, Message
  34. from wbb import BOT_USERNAME, SUDOERS, USERBOT_PREFIX, app, app2, eor
  35. from wbb.core.decorators.errors import capture_err
  36. from wbb.utils.files import (
  37. get_document_from_file_id,
  38. resize_file_to_sticker_size,
  39. upload_document,
  40. )
  41. from wbb.utils.stickerset import (
  42. add_sticker_to_set,
  43. create_sticker,
  44. create_sticker_set,
  45. get_sticker_set_by_name,
  46. )
  47. __MODULE__ = "Stickers"
  48. __HELP__ = """
  49. /sticker_id
  50. To get FileID of a Sticker.
  51. /get_sticker
  52. To get sticker as a photo and document.
  53. /kang
  54. To kang a Sticker or an Image."""
  55. MAX_STICKERS = (
  56. 120 # would be better if we could fetch this limit directly from telegram
  57. )
  58. SUPPORTED_TYPES = ["jpeg", "png", "webp"]
  59. @app.on_message(filters.command("sticker_id"))
  60. @capture_err
  61. async def sticker_id(_, message: Message):
  62. reply = message.reply_to_message
  63. if not reply:
  64. return await message.reply("Reply to a sticker.")
  65. if not reply.sticker:
  66. return await message.reply("Reply to a sticker.")
  67. await message.reply_text(f"`{reply.sticker.file_id}`")
  68. @app.on_message(filters.command("get_sticker"))
  69. @capture_err
  70. async def sticker_image(_, message: Message):
  71. r = message.reply_to_message
  72. if not r:
  73. return await message.reply("Reply to a sticker.")
  74. if not r.sticker:
  75. return await message.reply("Reply to a sticker.")
  76. m = await message.reply("Sending..")
  77. f = await r.download(f"{r.sticker.file_unique_id}.png")
  78. await gather(
  79. *[
  80. message.reply_photo(f),
  81. message.reply_document(f),
  82. ]
  83. )
  84. await m.delete()
  85. os.remove(f)
  86. @app2.on_message(
  87. filters.command("kang", prefixes=USERBOT_PREFIX)
  88. & ~filters.forwarded
  89. & ~filters.via_bot
  90. & SUDOERS,
  91. )
  92. async def userbot_kang(_, message: Message):
  93. reply = message.reply_to_message
  94. if not reply:
  95. return await message.reply_text("Reply to a sticker/image to kang it.")
  96. sticker_m = await reply.forward(BOT_USERNAME)
  97. # Send /kang message to bot and listen to his reply concurrently
  98. bot_reply, kang_m_bot = await gather(
  99. app2.listen(BOT_USERNAME, filters=~filters.me),
  100. sticker_m.reply(message.text.replace(USERBOT_PREFIX, "/")),
  101. )
  102. # Edit init message of ubot with the reply of
  103. # bot we got in the previous block
  104. bot_reply, ub_m = await gather(
  105. app2.listen(BOT_USERNAME, filters=~filters.me),
  106. eor(message, text=bot_reply.text.markdown),
  107. )
  108. # Edit the main userbot message with bot's final edit
  109. await ub_m.edit(bot_reply.text.markdown)
  110. # Delete all extra messages.
  111. for m in [bot_reply, kang_m_bot, sticker_m]:
  112. await m.delete()
  113. @app.on_message(filters.command("kang"))
  114. @capture_err
  115. async def kang(client, message: Message):
  116. if not message.reply_to_message:
  117. return await message.reply_text("Reply to a sticker/image to kang it.")
  118. if not message.from_user:
  119. return await message.reply_text(
  120. "You are anon admin, kang stickers in my pm."
  121. )
  122. msg = await message.reply_text("Kanging Sticker..")
  123. # Find the proper emoji
  124. args = message.text.split()
  125. if len(args) > 1:
  126. sticker_emoji = str(args[1])
  127. elif (
  128. message.reply_to_message.sticker
  129. and message.reply_to_message.sticker.emoji
  130. ):
  131. sticker_emoji = message.reply_to_message.sticker.emoji
  132. else:
  133. sticker_emoji = "🤔"
  134. # Get the corresponding fileid, resize the file if necessary
  135. doc = message.reply_to_message.photo or message.reply_to_message.document
  136. try:
  137. if message.reply_to_message.sticker:
  138. sticker = await create_sticker(
  139. await get_document_from_file_id(
  140. message.reply_to_message.sticker.file_id
  141. ),
  142. sticker_emoji,
  143. )
  144. elif doc:
  145. if doc.file_size > 10000000:
  146. return await msg.edit("File size too large.")
  147. temp_file_path = await app.download_media(doc)
  148. image_type = imghdr.what(temp_file_path)
  149. if image_type not in SUPPORTED_TYPES:
  150. return await msg.edit(
  151. "Format not supported! ({})".format(image_type)
  152. )
  153. try:
  154. temp_file_path = await resize_file_to_sticker_size(
  155. temp_file_path
  156. )
  157. except OSError as e:
  158. await msg.edit_text("Something wrong happened.")
  159. raise Exception(
  160. f"Something went wrong while resizing the sticker (at {temp_file_path}); {e}"
  161. )
  162. sticker = await create_sticker(
  163. await upload_document(client, temp_file_path, message.chat.id),
  164. sticker_emoji,
  165. )
  166. if os.path.isfile(temp_file_path):
  167. os.remove(temp_file_path)
  168. else:
  169. return await msg.edit("Nope, can't kang that.")
  170. except ShortnameOccupyFailed:
  171. await message.reply_text("Change Your Name Or Username")
  172. return
  173. except Exception as e:
  174. await message.reply_text(str(e))
  175. e = format_exc()
  176. return print(e)
  177. # Find an available pack & add the sticker to the pack; create a new pack if needed
  178. # Would be a good idea to cache the number instead of searching it every single time...
  179. packnum = 0
  180. packname = "f" + str(message.from_user.id) + "_by_" + BOT_USERNAME
  181. limit = 0
  182. try:
  183. while True:
  184. # Prevent infinite rules
  185. if limit >= 50:
  186. return await msg.delete()
  187. stickerset = await get_sticker_set_by_name(client, packname)
  188. if not stickerset:
  189. stickerset = await create_sticker_set(
  190. client,
  191. message.from_user.id,
  192. f"{message.from_user.first_name[:32]}'s kang pack",
  193. packname,
  194. [sticker],
  195. )
  196. elif stickerset.set.count >= MAX_STICKERS:
  197. packnum += 1
  198. packname = (
  199. "f"
  200. + str(packnum)
  201. + "_"
  202. + str(message.from_user.id)
  203. + "_by_"
  204. + BOT_USERNAME
  205. )
  206. limit += 1
  207. continue
  208. else:
  209. try:
  210. await add_sticker_to_set(client, stickerset, sticker)
  211. except StickerEmojiInvalid:
  212. return await msg.edit("[ERROR]: INVALID_EMOJI_IN_ARGUMENT")
  213. limit += 1
  214. break
  215. await msg.edit(
  216. "Sticker Kanged To [Pack](t.me/addstickers/{})\nEmoji: {}".format(
  217. packname, sticker_emoji
  218. )
  219. )
  220. except (PeerIdInvalid, UserIsBlocked):
  221. keyboard = InlineKeyboardMarkup(
  222. [[InlineKeyboardButton(text="Start", url=f"t.me/{BOT_USERNAME}")]]
  223. )
  224. await msg.edit(
  225. "You Need To Start A Private Chat With Me.",
  226. reply_markup=keyboard,
  227. )
  228. except StickerPngNopng:
  229. await message.reply_text(
  230. "Stickers must be png files but the provided image was not a png"
  231. )
  232. except StickerPngDimensions:
  233. await message.reply_text("The sticker png dimensions are invalid.")