stickers.py 6.1 KB

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