forked from a-fruity-melon/PagerMaid_Plugins
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgetstickerset.py
105 lines (101 loc) · 4.44 KB
/
getstickerset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from telethon.errors import StickersetInvalidError, FileMigrateError
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, version
from pagermaid.listener import listener
@listener(is_plugin=True, outgoing=True, 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:
try:
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'
except FileMigrateError:
pass
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