From 01e8b8a210bc21d243fc7a549aaff3fb90fd8761 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Mon, 16 Aug 2021 00:27:43 +0800 Subject: [PATCH] =?UTF-8?q?getstickerset=20=E8=8E=B7=E5=8F=96=E6=89=80?= =?UTF-8?q?=E5=9B=9E=E5=A4=8D=E8=B4=B4=E7=BA=B8=E7=9A=84=E8=B4=B4=E7=BA=B8?= =?UTF-8?q?=E5=8C=85=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- getstickerset.py | 103 +++++++++++++++++++++++++++++++++++++++++++++++ list.json | 10 +++++ 2 files changed, 113 insertions(+) create mode 100644 getstickerset.py diff --git a/getstickerset.py b/getstickerset.py new file mode 100644 index 0000000..f6f62ca --- /dev/null +++ b/getstickerset.py @@ -0,0 +1,103 @@ +from telethon.errors import StickersetInvalidError +from telethon.tl.custom.message import Message +from telethon.tl.functions.messages import GetStickerSetRequest +from telethon.tl.functions.upload import GetFileRequest +from telethon.tl.types import DocumentAttributeFilename, InputStickerSetEmpty, InputStickerSetID, StickerSet, \ + InputStickerSetThumb, MessageMediaPhoto +from PIL import Image +from pytz import timezone +from pagermaid import bot +from pagermaid.listener import listener +from pagermaid.utils import alias_command + + +@listener(is_plugin=True, outgoing=True, command=alias_command("getstickerset"), + description="获取所回复贴纸的贴纸包信息。") +async def get_sticker_set(context: Message): + """ get sticker set """ + reply = await context.get_reply_message() + if not reply: + await context.edit('出错了呜呜呜 ~ 没有回复贴纸消息。') + return + if not reply.media: + await context.edit('出错了呜呜呜 ~ 没有回复贴纸消息。') + return + if isinstance(reply.media, MessageMediaPhoto): + await context.edit('出错了呜呜呜 ~ 没有回复贴纸消息。') + return + elif "image" in reply.media.document.mime_type.split('/'): + if (DocumentAttributeFilename(file_name='sticker.webp') not in + reply.media.document.attributes): + await context.edit('出错了呜呜呜 ~ 没有回复贴纸消息。') + return + elif (DocumentAttributeFilename(file_name='AnimatedSticker.tgs') in + reply.media.document.attributes): + pass + else: + await context.edit('出错了呜呜呜 ~ 没有回复贴纸消息。') + return + sticker_set = reply.media.document.attributes[1].stickerset + if isinstance(sticker_set, InputStickerSetEmpty): + await context.edit('出错了呜呜呜 ~ 您回复的贴纸不包含任何贴纸包信息。') + return + await context.edit('获取中。。。') + try: + stickers = await context.client(GetStickerSetRequest( + stickerset=InputStickerSetID(id=sticker_set.id, access_hash=sticker_set.access_hash))) + except StickersetInvalidError: + await context.edit('出错了呜呜呜 ~ 您回复的贴纸不包含任何贴纸包信息。') + return + stickers_set = stickers.set + # 再次判断变量类型 + if not isinstance(stickers_set, StickerSet): + await context.edit('出错了呜呜呜 ~ 您回复的贴纸不包含任何贴纸包信息。') + return + # 初始化变量 + sid = sticker_set.id + access_hash = sticker_set.access_hash + thumb_version = stickers_set.thumb_version + official = '✅' if stickers_set.official else '' + animated = '(动态)' if stickers_set.animated else '' + archived = '💤' if stickers_set.archived else '' + time_zone = timezone('Etc/GMT-8') + installed_date = stickers_set.installed_date.astimezone(time_zone).strftime('%Y-%m-%d %H:%M:%S') if \ + stickers_set.installed_date else '未添加' + # 下载预览图 + file = None + if thumb_version: + thumb = await bot(GetFileRequest(location=InputStickerSetThumb( + stickerset=InputStickerSetID(id=sid, access_hash=access_hash), + thumb_version=thumb_version), offset=-1, limit=1048576, precise=False, cdn_supported=True)) + with open('data/sticker_thumb.jpg', 'wb') as f: + f.write(thumb.bytes) + file = 'data/sticker_thumb.jpg' + else: + if not stickers_set.animated: + await bot.download_media(stickers.documents[0], file='data/sticker_thumb.webp') + convert_png('data/sticker_thumb.webp') + file = 'data/sticker_thumb.png' + text = f'贴纸包:{official}[{stickers_set.title}](https://t.me/addstickers/{stickers_set.short_name}) {animated}' \ + f'{archived}\n' \ + f'贴纸数:`{stickers_set.count}`\n' \ + f'添加时间:`{installed_date}`\n' \ + f'id:`{sid}`\n' \ + f'access_hash: `{access_hash}`' + if file: + await context.client.send_file( + context.chat_id, + file, + caption=text, + force_document=False, + allow_cache=False + ) + await context.delete() + else: + await context.edit(text) + + +def convert_png(path): + im = Image.open(path) + im = im.convert('RGBA') + new_path = path.replace(".webp", ".png") + im.save(new_path, 'PNG') + return new_path diff --git a/list.json b/list.json index 240d0dc..dca33c6 100644 --- a/list.json +++ b/list.json @@ -729,6 +729,16 @@ "supported": true, "des-short": "自动转发/备份频道消息", "des": "自动转发频道新消息或者从零开始备份频道。\n指令:-shift\n查看帮助:-help shift" + }, + { + "name": "getstickerset", + "version": "1.0", + "section": "chat", + "maintainer": "xtaodada", + "size": "4.4 kb", + "supported": true, + "des-short": "获取所回复贴纸的贴纸包信息", + "des": "获取所回复贴纸的贴纸包信息。\n指令:-getstickerset" } ] }