stickers.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. """
  2. MIT License
  3. Copyright (c) 2021 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 (PeerIdInvalid, ShortnameOccupyFailed,
  26. StickerEmojiInvalid, StickerPngDimensions,
  27. StickerPngNopng, UserIsBlocked)
  28. from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, Message
  29. from wbb import BOT_USERNAME, SUDOERS, USERBOT_PREFIX, app, app2, eor
  30. from wbb.core.decorators.errors import capture_err
  31. from wbb.utils.files import (get_document_from_file_id,
  32. resize_file_to_sticker_size, upload_document)
  33. from wbb.utils.stickerset import (add_sticker_to_set, create_sticker,
  34. create_sticker_set, get_sticker_set_by_name)
  35. __MODULE__ = "Stickers"
  36. __HELP__ = """
  37. /sticker_id
  38. To get FileID of a Sticker.
  39. /get_sticker
  40. To get sticker as a photo and document.
  41. /kang
  42. To kang a Sticker or an Image."""
  43. MAX_STICKERS = (
  44. 120 # would be better if we could fetch this limit directly from telegram
  45. )
  46. SUPPORTED_TYPES = ["jpeg", "png", "webp"]
  47. @app.on_message(filters.command("sticker_id") & ~filters.edited)
  48. @capture_err
  49. async def sticker_id(_, message: Message):
  50. reply = message.reply_to_message
  51. if not reply:
  52. return await message.reply("Reply to a sticker.")
  53. if not reply.sticker:
  54. return await message.reply("Reply to a sticker.")
  55. await message.reply_text(f"`{reply.sticker.file_id}`")
  56. @app.on_message(filters.command("get_sticker") & ~filters.edited)
  57. @capture_err
  58. async def sticker_image(_, message: Message):
  59. r = message.reply_to_message
  60. if not r:
  61. return await message.reply("Reply to a sticker.")
  62. if not r.sticker:
  63. return await message.reply("Reply to a sticker.")
  64. m = await message.reply("Sending..")
  65. f = await r.download(f"{r.sticker.file_unique_id}.png")
  66. await gather(
  67. *[
  68. message.reply_photo(f),
  69. message.reply_document(f),
  70. ]
  71. )
  72. await m.delete()
  73. os.remove(f)
  74. @app2.on_message(
  75. filters.command("kang", prefixes=USERBOT_PREFIX) & filters.user(SUDOERS),
  76. )
  77. async def userbot_kang(_, message: Message):
  78. reply = message.reply_to_message
  79. if not reply:
  80. return await message.reply_text("Reply to a sticker/image to kang it.")
  81. sticker_m = await reply.forward(BOT_USERNAME)
  82. # Send /kang message to bot and listen to his reply concurrently
  83. bot_reply, kang_m_bot = await gather(
  84. app2.listen(BOT_USERNAME, filters=~filters.me),
  85. sticker_m.reply(message.text.replace(USERBOT_PREFIX, "/")),
  86. )
  87. # Edit init message of ubot with the reply of
  88. # bot we got in the previous block
  89. bot_reply, ub_m = await gather(
  90. app2.listen(BOT_USERNAME, filters=~filters.me),
  91. eor(message, text=bot_reply.text.markdown),
  92. )
  93. # Edit the main userbot message with bot's final edit
  94. await ub_m.edit(bot_reply.text.markdown)
  95. # Delete all extra messages.
  96. for m in [bot_reply, kang_m_bot, sticker_m]:
  97. await m.delete()
  98. @app.on_message(filters.command("kang") & ~filters.edited)
  99. @capture_err
  100. async def kang(client, message: Message):
  101. if not message.reply_to_message:
  102. return await message.reply_text("Reply to a sticker/image to kang it.")
  103. if not message.from_user:
  104. return await message.reply_text(
  105. "You are anon admin, kang stickers in my pm."
  106. )
  107. msg = await message.reply_text("Kanging Sticker..")
  108. # Find the proper emoji
  109. args = message.text.split()
  110. if len(args) > 1:
  111. sticker_emoji = str(args[1])
  112. elif (
  113. message.reply_to_message.sticker
  114. and message.reply_to_message.sticker.emoji
  115. ):
  116. sticker_emoji = message.reply_to_message.sticker.emoji
  117. else:
  118. sticker_emoji = "🤔"
  119. # Get the corresponding fileid, resize the file if necessary
  120. doc = message.reply_to_message.photo or message.reply_to_message.document
  121. try:
  122. if message.reply_to_message.sticker:
  123. sticker = await create_sticker(
  124. await get_document_from_file_id(
  125. message.reply_to_message.sticker.file_id
  126. ),
  127. sticker_emoji,
  128. )
  129. elif doc:
  130. if doc.file_size > 10000000:
  131. return await msg.edit("File size too large.")
  132. temp_file_path = await app.download_media(doc)
  133. image_type = imghdr.what(temp_file_path)
  134. if image_type not in SUPPORTED_TYPES:
  135. return await msg.edit(
  136. "Format not supported! ({})".format(image_type)
  137. )
  138. try:
  139. temp_file_path = await resize_file_to_sticker_size(
  140. temp_file_path
  141. )
  142. except OSError as e:
  143. await msg.edit_text("Something wrong happened.")
  144. raise Exception(
  145. f"Something went wrong while resizing the sticker (at {temp_file_path}); {e}"
  146. )
  147. sticker = await create_sticker(
  148. await upload_document(client, temp_file_path, message.chat.id),
  149. sticker_emoji,
  150. )
  151. if os.path.isfile(temp_file_path):
  152. os.remove(temp_file_path)
  153. else:
  154. return await msg.edit("Nope, can't kang that.")
  155. except ShortnameOccupyFailed:
  156. await message.reply_text("Change Your Name Or Username")
  157. return
  158. except Exception as e:
  159. await message.reply_text(str(e))
  160. e = format_exc()
  161. return print(e)
  162. # Find an available pack & add the sticker to the pack; create a new pack if needed
  163. # Would be a good idea to cache the number instead of searching it every single time...
  164. packnum = 0
  165. packname = "f" + str(message.from_user.id) + "_by_" + BOT_USERNAME
  166. limit = 0
  167. try:
  168. while True:
  169. # Prevent infinite rules
  170. if limit >= 50:
  171. return await msg.delete()
  172. stickerset = await get_sticker_set_by_name(client, packname)
  173. if not stickerset:
  174. stickerset = await create_sticker_set(
  175. client,
  176. message.from_user.id,
  177. f"{message.from_user.first_name[:32]}'s kang pack",
  178. packname,
  179. [sticker],
  180. )
  181. elif stickerset.set.count >= MAX_STICKERS:
  182. packnum += 1
  183. packname = (
  184. "f"
  185. + str(packnum)
  186. + "_"
  187. + str(message.from_user.id)
  188. + "_by_"
  189. + BOT_USERNAME
  190. )
  191. limit += 1
  192. continue
  193. else:
  194. try:
  195. await add_sticker_to_set(client, stickerset, sticker)
  196. except StickerEmojiInvalid:
  197. return await msg.edit("[ERROR]: INVALID_EMOJI_IN_ARGUMENT")
  198. limit += 1
  199. break
  200. await msg.edit(
  201. "Sticker Kanged To [Pack](t.me/addstickers/{})\nEmoji: {}".format(
  202. packname, sticker_emoji
  203. )
  204. )
  205. except (PeerIdInvalid, UserIsBlocked):
  206. keyboard = InlineKeyboardMarkup(
  207. [[InlineKeyboardButton(text="Start", url=f"t.me/{BOT_USERNAME}")]]
  208. )
  209. await msg.edit(
  210. "You Need To Start A Private Chat With Me.",
  211. reply_markup=keyboard,
  212. )
  213. except StickerPngNopng:
  214. await message.reply_text(
  215. "Stickers must be png files but the provided image was not a png"
  216. )
  217. except StickerPngDimensions:
  218. await message.reply_text("The sticker png dimensions are invalid.")