🐛 增加 Sticker 命令的适用场景 (#51)

让 Sticker 删了也能偷,除非在bot收到指令前就被删了。
This commit is contained in:
玖柒Max 2021-02-08 13:25:53 +08:00 committed by GitHub
parent 9a2d9ac039
commit 186c709801
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -26,14 +26,20 @@ async def sticker(context):
custom_emoji = False custom_emoji = False
animated = False animated = False
emoji = "" emoji = ""
try:
await context.edit("收集图像/贴纸中 . . .") await context.edit("收集图像/贴纸中 . . .")
except:
pass
if message and message.media: if message and message.media:
if isinstance(message.media, MessageMediaPhoto): if isinstance(message.media, MessageMediaPhoto):
photo = BytesIO() photo = BytesIO()
photo = await bot.download_media(message.photo, photo) photo = await bot.download_media(message.photo, photo)
elif "image" in message.media.document.mime_type.split('/'): elif "image" in message.media.document.mime_type.split('/'):
photo = BytesIO() photo = BytesIO()
try:
await context.edit("下载图片中 . . .") await context.edit("下载图片中 . . .")
except:
pass
await bot.download_file(message.media.document, photo) await bot.download_file(message.media.document, photo)
if (DocumentAttributeFilename(file_name='sticker.webp') in if (DocumentAttributeFilename(file_name='sticker.webp') in
message.media.document.attributes): message.media.document.attributes):
@ -53,10 +59,16 @@ async def sticker(context):
animated = True animated = True
photo = 1 photo = 1
else: else:
try:
await context.edit("`出错了呜呜呜 ~ 不支持此文件类型。`") await context.edit("`出错了呜呜呜 ~ 不支持此文件类型。`")
except:
pass
return return
else: else:
try:
await context.edit("`出错了呜呜呜 ~ 请回复带有图片/贴纸的消息。`") await context.edit("`出错了呜呜呜 ~ 请回复带有图片/贴纸的消息。`")
except:
pass
return return
if photo: if photo:
@ -80,7 +92,10 @@ async def sticker(context):
file = BytesIO() file = BytesIO()
if not animated: if not animated:
try:
await context.edit("调整图像大小中 . . .") await context.edit("调整图像大小中 . . .")
except:
pass
image = await resize_image(photo) image = await resize_image(photo)
file.name = "sticker.png" file.name = "sticker.png"
image.save(file, "PNG") image.save(file, "PNG")
@ -92,7 +107,10 @@ async def sticker(context):
response = request.urlopen( response = request.urlopen(
request.Request(f'http://t.me/addstickers/{pack_name}'), context=ssl.create_default_context(cafile=certifi.where())) request.Request(f'http://t.me/addstickers/{pack_name}'), context=ssl.create_default_context(cafile=certifi.where()))
if not response.status == 200: if not response.status == 200:
try:
await context.edit("连接到 Telegram 服务器失败 . . .") await context.edit("连接到 Telegram 服务器失败 . . .")
except:
pass
return return
http_response = response.read().decode("utf8").split('\n') http_response = response.read().decode("utf8").split('\n')
@ -111,16 +129,21 @@ A pack can't have more than 120 stickers at the moment.":
pack += 1 pack += 1
pack_name = f"{user.username}_{pack}" pack_name = f"{user.username}_{pack}"
pack_title = f"@{user.username} 的私藏 ({pack})" pack_title = f"@{user.username} 的私藏 ({pack})"
await context.edit("切换到私藏 " + str(pack) + try:
" 上一个贴纸包已满 . . .") await context.edit("切换到私藏 " + str(pack) + " 上一个贴纸包已满 . . .")
except:
pass
await conversation.send_message(pack_name) await conversation.send_message(pack_name)
chat_response = await conversation.get_response() chat_response = await conversation.get_response()
if chat_response.text == "Invalid pack selected.": if chat_response.text == "Invalid pack selected.":
await add_sticker(conversation, command, pack_title, pack_name, animated, message, await add_sticker(conversation, command, pack_title, pack_name, animated, message,
context, file, emoji) context, file, emoji)
try:
await context.edit( await context.edit(
f"这张图片/贴纸已经被添加到 [这个](t.me/addstickers/{pack_name}) 贴纸包。", f"这张图片/贴纸已经被添加到 [这个](t.me/addstickers/{pack_name}) 贴纸包。",
parse_mode='md') parse_mode='md')
except:
pass
return return
await upload_sticker(animated, message, context, file, conversation) await upload_sticker(animated, message, context, file, conversation)
await conversation.get_response() await conversation.get_response()
@ -133,7 +156,10 @@ A pack can't have more than 120 stickers at the moment.":
break break
except AlreadyInConversationError: except AlreadyInConversationError:
if not sticker_already: if not sticker_already:
try:
await context.edit("另一个命令正在添加贴纸, 重新尝试中") await context.edit("另一个命令正在添加贴纸, 重新尝试中")
except:
pass
sticker_already = True sticker_already = True
else: else:
pass pass
@ -141,17 +167,23 @@ A pack can't have more than 120 stickers at the moment.":
except Exception: except Exception:
raise raise
else: else:
try:
await context.edit("贴纸包不存在,正在创建 . . .") await context.edit("贴纸包不存在,正在创建 . . .")
except:
pass
async with bot.conversation('Stickers') as conversation: async with bot.conversation('Stickers') as conversation:
await add_sticker(conversation, command, pack_title, pack_name, animated, message, await add_sticker(conversation, command, pack_title, pack_name, animated, message,
context, file, emoji) context, file, emoji)
notification = await context.edit( try:
await context.edit(
f"这张图片/贴纸已经被添加到 [这个](t.me/addstickers/{pack_name}) 贴纸包。", f"这张图片/贴纸已经被添加到 [这个](t.me/addstickers/{pack_name}) 贴纸包。",
parse_mode='md') parse_mode='md')
except:
pass
await sleep(5) await sleep(5)
try: try:
await notification.delete() await context.delete()
except: except:
pass pass
@ -185,12 +217,18 @@ async def add_sticker(conversation, command, pack_title, pack_name, animated, me
async def upload_sticker(animated, message, context, file, conversation): async def upload_sticker(animated, message, context, file, conversation):
if animated: if animated:
try:
await context.edit("上传动图中 . . .") await context.edit("上传动图中 . . .")
except:
pass
await conversation.send_file("AnimatedSticker.tgs", force_document=True) await conversation.send_file("AnimatedSticker.tgs", force_document=True)
remove("AnimatedSticker.tgs") remove("AnimatedSticker.tgs")
else: else:
file.seek(0) file.seek(0)
try:
await context.edit("上传图片中 . . .") await context.edit("上传图片中 . . .")
except:
pass
await conversation.send_file(file, force_document=True) await conversation.send_file(file, force_document=True)