iShotaBot/modules/sticker_download.py

153 lines
5.4 KiB
Python
Raw Permalink Normal View History

2024-01-22 15:13:55 +00:00
import contextlib
import tempfile
from typing import TYPE_CHECKING
from cashews import cache
from pyrogram import filters, ContinuePropagation
from pyrogram.enums import ChatAction, MessageEntityType
from pyrogram.errors import StickersetInvalid
2024-07-17 12:16:19 +00:00
from defs.sticker_download import (
get_from_sticker_set,
get_from_sticker,
custom_emoji_filter,
get_from_custom_emoji,
export_end,
export_add,
)
2024-01-22 15:13:55 +00:00
from init import bot
if TYPE_CHECKING:
from pyrogram import Client
from pyrogram.types import Message
@bot.on_message(
2024-07-17 12:16:19 +00:00
filters.private
& filters.text
& filters.incoming
& filters.regex(r"^https://t.me/addstickers/.*")
2024-01-22 15:13:55 +00:00
)
async def process_sticker_set(client: "Client", message: "Message"):
cid = message.from_user.id
short_name = message.text.replace("https://t.me/addstickers/", "")
file = None
try:
reply = await message.reply("正在下载贴纸包...请耐心等待", quote=True)
file = await get_from_sticker_set(short_name, cid, client, reply)
with contextlib.suppress(Exception):
await message.reply_chat_action(ChatAction.UPLOAD_DOCUMENT)
await message.reply_document(file.as_posix(), quote=True)
with contextlib.suppress(Exception):
await reply.delete()
except StickersetInvalid:
await message.reply("无效的贴纸包", quote=True)
finally:
if file:
file.unlink(missing_ok=True)
raise ContinuePropagation
@bot.on_message(filters.private & filters.sticker & filters.incoming)
async def process_single_sticker(client: "Client", message: "Message"):
await message.reply_chat_action(ChatAction.TYPING)
if temp := await cache.get(f"sticker:export:{message.from_user.id}"):
2024-03-07 10:52:58 +00:00
try:
await export_add(temp, message.sticker, client)
2024-11-27 09:09:58 +00:00
await message.reply_text(
"成功加入导出列表,结束选择请输入 /sticker_export_end", quote=True
)
2024-03-07 10:52:58 +00:00
except ValueError as exc:
await message.reply(str(exc), quote=True)
2024-01-22 15:13:55 +00:00
else:
reply = await message.reply("正在转换贴纸...请耐心等待", quote=True)
target_file = None
try:
target_file = await get_from_sticker(client, message)
await message.reply_chat_action(ChatAction.UPLOAD_DOCUMENT)
await message.reply_document(target_file.as_posix(), quote=True)
2024-03-07 10:52:58 +00:00
with contextlib.suppress(Exception):
await reply.delete()
except ValueError as exc:
await reply.edit(str(exc))
2024-01-22 15:13:55 +00:00
finally:
if target_file:
target_file.unlink(missing_ok=True)
raise ContinuePropagation
@bot.on_message(filters.private & custom_emoji_filter & filters.incoming)
async def process_custom_emoji(client: "Client", message: "Message"):
try:
stickers = await client.get_custom_emoji_stickers(
2024-07-17 12:16:19 +00:00
[
i.custom_emoji_id
for i in message.entities
if i and i.type == MessageEntityType.CUSTOM_EMOJI
]
2024-01-22 15:13:55 +00:00
)
except AttributeError:
await message.reply("无法获取贴纸", quote=True)
raise ContinuePropagation
2024-11-27 09:09:58 +00:00
reply = await message.reply(
f"正在下载 {len(stickers)} 个 emoji ...请耐心等待", quote=True
)
2024-03-07 10:52:58 +00:00
exc = None
2024-01-22 15:13:55 +00:00
for sticker in stickers:
target_file = None
try:
target_file = await get_from_custom_emoji(client, sticker)
await message.reply_chat_action(ChatAction.UPLOAD_DOCUMENT)
await message.reply_document(target_file.as_posix(), quote=True)
2024-03-07 10:52:58 +00:00
except ValueError as exc_:
exc = exc_
2024-01-22 15:13:55 +00:00
finally:
if target_file:
target_file.unlink(missing_ok=True)
2024-03-07 10:52:58 +00:00
if exc:
await reply.edit(str(exc))
else:
with contextlib.suppress(Exception):
await reply.delete()
2024-01-22 15:13:55 +00:00
raise ContinuePropagation
@bot.on_message(
2024-07-17 12:16:19 +00:00
filters.private
& filters.incoming
& filters.command(["sticker_export_start", "sticker_export_end"])
2024-01-22 15:13:55 +00:00
)
async def batch_start(_: "Client", message: "Message"):
uid = message.from_user.id
if "start" in message.command[0].lower():
if await cache.get(f"sticker:export:{uid}"):
2024-07-17 12:16:19 +00:00
await message.reply(
2024-11-27 09:09:58 +00:00
"已经开始批量导出贴纸,请直接发送贴纸,完成选择请输入 /sticker_export_end",
quote=True,
2024-07-17 12:16:19 +00:00
)
2024-01-22 15:13:55 +00:00
return
await cache.set(f"sticker:export:{uid}", tempfile.mkdtemp())
2024-11-27 09:09:58 +00:00
await message.reply(
"开始批量导出贴纸,请直接发送贴纸,完成选择请输入 /sticker_export_end",
quote=True,
)
2024-01-22 15:13:55 +00:00
else:
target_dir = await cache.get(f"sticker:export:{uid}")
if not target_dir:
2024-11-27 09:09:58 +00:00
await message.reply(
"未开始批量导出贴纸,请先使用命令 /sticker_export_start", quote=True
)
2024-01-22 15:13:55 +00:00
return
file = None
try:
reply = await message.reply("正在打包贴纸包...请耐心等待", quote=True)
file = await export_end(uid, target_dir, reply)
await message.reply_chat_action(ChatAction.UPLOAD_DOCUMENT)
await message.reply_document(file.as_posix())
except FileNotFoundError:
await message.reply("没有选择贴纸,导出失败", quote=True)
finally:
await cache.delete(f"sticker:export:{uid}")
if file:
file.unlink(missing_ok=True)