stickers.py 6.7 KB

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