stickers.py 5.9 KB

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