| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- """
- MIT License
- Copyright (c) 2021 TheHamkerCat
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- """
- import imghdr
- import os
- from traceback import format_exc
- from pyrogram import filters
- from pyrogram.errors.exceptions.bad_request_400 import (
- PeerIdInvalid, ShortnameOccupyFailed, StickerEmojiInvalid,
- StickerPngDimensions, StickerPngNopng, UserIsBlocked)
- from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
- from wbb import BOT_USERNAME, app
- from wbb.core.decorators.errors import capture_err
- from wbb.utils.files import (get_document_from_file_id,
- resize_file_to_sticker_size,
- upload_document)
- from wbb.utils.stickerset import (add_sticker_to_set, create_sticker,
- create_sticker_set,
- get_sticker_set_by_name)
- __MODULE__ = "Stickers"
- __HELP__ = """/sticker_id - To Get File ID of A Sticker.
- /kang - To Kang A Sticker or Image."""
- MAX_STICKERS = 120 # would be better if we could fetch this limit directly from telegram
- SUPPORTED_TYPES = ["jpeg", "png", "webp"]
- @app.on_message(filters.command("sticker_id") & ~filters.edited)
- @capture_err
- async def sticker_id(_, message):
- if not message.reply_to_message:
- return await message.reply_text("Reply to a sticker.")
- if not message.reply_to_message.sticker:
- return await message.reply_text("Reply to a sticker.")
- file_id = message.reply_to_message.sticker.file_id
- await message.reply_text(f"`{file_id}`")
- @app.on_message(filters.command("kang") & ~filters.edited)
- @capture_err
- async def kang(client, message):
- if not message.reply_to_message:
- return await message.reply_text(
- "Reply to a sticker/image to kang it."
- )
- if not message.from_user:
- return await message.reply_text(
- "You are anon admin, kang stickers in my pm."
- )
- msg = await message.reply_text("Kanging Sticker..")
- # Find the proper emoji
- args = message.text.split()
- if len(args) > 1:
- sticker_emoji = str(args[1])
- elif (
- message.reply_to_message.sticker
- and message.reply_to_message.sticker.emoji
- ):
- sticker_emoji = message.reply_to_message.sticker.emoji
- else:
- sticker_emoji = "🤔"
- # Get the corresponding fileid, resize the file if necessary
- doc = (
- message.reply_to_message.photo
- or message.reply_to_message.document
- )
- try:
- if message.reply_to_message.sticker:
- sticker = await create_sticker(
- await get_document_from_file_id(
- message.reply_to_message.sticker.file_id
- ),
- sticker_emoji,
- )
- elif doc:
- temp_file_path = await app.download_media(doc)
- image_type = imghdr.what(temp_file_path)
- if image_type not in SUPPORTED_TYPES:
- return await msg.edit(
- "Format not supported! ({})".format(image_type)
- )
- try:
- temp_file_path = await resize_file_to_sticker_size(
- temp_file_path
- )
- except OSError as e:
- await msg.edit_text("Something wrong happened.")
- raise Exception(
- f"Something went wrong while resizing the sticker (at {temp_file_path}); {e}"
- )
- return False
- sticker = await create_sticker(
- await upload_document(
- client, temp_file_path, message.chat.id
- ),
- sticker_emoji,
- )
- if os.path.isfile(temp_file_path):
- os.remove(temp_file_path)
- else:
- return await msg.edit("Nope, can't kang that.")
- except ShortnameOccupyFailed:
- await message.reply_text("Change Your Name Or Username")
- return
- except Exception as e:
- await message.reply_text(str(e))
- e = format_exc()
- return print(e)
- # Find an available pack & add the sticker to the pack; create a new pack if needed
- # Would be a good idea to cache the number instead of searching it every single time...
- packnum = 0
- packname = "f" + str(message.from_user.id) + "_by_" + BOT_USERNAME
- limit = 0
- try:
- while True:
- # Prevent infinite rules
- if limit >= 50:
- return await msg.delete()
- stickerset = await get_sticker_set_by_name(
- client, packname
- )
- if not stickerset:
- stickerset = await create_sticker_set(
- client,
- message.from_user.id,
- f"{message.from_user.first_name[:32]}'s kang pack",
- packname,
- [sticker],
- )
- elif stickerset.set.count >= MAX_STICKERS:
- packnum += 1
- packname = (
- "f"
- + str(packnum)
- + "_"
- + str(message.from_user.id)
- + "_by_"
- + BOT_USERNAME
- )
- limit += 1
- continue
- else:
- try:
- await add_sticker_to_set(
- client, stickerset, sticker
- )
- except StickerEmojiInvalid:
- return await msg.edit(
- "[ERROR]: INVALID_EMOJI_IN_ARGUMENT"
- )
- limit += 1
- break
- await msg.edit(
- "Sticker Kanged To [Pack](t.me/addstickers/{})\nEmoji: {}".format(
- packname, sticker_emoji
- )
- )
- except (PeerIdInvalid, UserIsBlocked):
- keyboard = InlineKeyboardMarkup(
- [
- [
- InlineKeyboardButton(
- text="Start", url=f"t.me/{BOT_USERNAME}"
- )
- ]
- ]
- )
- await msg.edit(
- "You Need To Start A Private Chat With Me.",
- reply_markup=keyboard,
- )
- except StickerPngNopng:
- await message.reply_text(
- "Stickers must be png files but the provided image was not a png"
- )
- except StickerPngDimensions:
- await message.reply_text(
- "The sticker png dimensions are invalid."
- )
|