stickers.py 7.2 KB

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