stickers.py 8.1 KB

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