eat 支持解析频道发言

This commit is contained in:
xtaodada 2021-11-26 13:52:35 +08:00
parent 8cf9274d9b
commit 21547d85ce
No known key found for this signature in database
GPG Key ID: EE4DC37B55E24736
2 changed files with 44 additions and 27 deletions

63
eat.py
View File

@ -8,13 +8,14 @@ from random import randint
from telethon.events import NewMessage from telethon.events import NewMessage
from telethon.tl.functions.users import GetFullUserRequest from telethon.tl.functions.users import GetFullUserRequest
from telethon.tl.functions.channels import GetFullChannelRequest
from telethon.tl.patched import Message from telethon.tl.patched import Message
from telethon.tl.types import MessageEntityMentionName, MessageEntityPhone, MessageEntityBotCommand from telethon.tl.types import Channel, MessageEntityMentionName, MessageEntityPhone, MessageEntityBotCommand
from telethon.errors.rpcerrorlist import ChatSendStickersForbiddenError from telethon.errors.rpcerrorlist import ChatSendStickersForbiddenError
from struct import error as StructError from struct import error as StructError
from pagermaid.listener import listener from pagermaid.listener import listener
from pagermaid.utils import alias_command from pagermaid.utils import alias_command
from pagermaid import redis, config from pagermaid import redis, config, bot
from collections import defaultdict from collections import defaultdict
import json import json
@ -39,7 +40,13 @@ configFilePath = 'plugins/eat/config.json'
configFileRemoteUrlKey = "eat.configFileRemoteUrl" configFileRemoteUrlKey = "eat.configFileRemoteUrl"
async def eat_it(context, from_user, base, mask, photo, number, layer=0): async def get_full_id(object_n):
if isinstance(object_n, Channel):
return (await bot(GetFullChannelRequest(object_n.id))).full_chat.id # noqa
return (await bot(GetFullUserRequest(object_n.id))).user.id
async def eat_it(context, uid, base, mask, photo, number, layer=0):
mask_size = mask.size mask_size = mask.size
photo_size = photo.size photo_size = photo.size
if mask_size[0] < photo_size[0] and mask_size[1] < photo_size[1]: if mask_size[0] < photo_size[0] and mask_size[1] < photo_size[1]:
@ -63,22 +70,21 @@ async def eat_it(context, from_user, base, mask, photo, number, layer=0):
else: else:
base.paste(mask1, (numberPosition[0], numberPosition[1]), mask1) base.paste(mask1, (numberPosition[0], numberPosition[1]), mask1)
# 增加判断是否有第二个头像孔 # 增加判断是否有第二个头像孔
isContinue = len(numberPosition) > 2 and layer == 0 isContinue = len(numberPosition) > 2 and layer == 0
if isContinue: if isContinue:
await context.client.download_profile_photo( await context.client.download_profile_photo(
from_user.user.id, uid,
"plugins/eat/" + str(from_user.user.id) + ".jpg", "plugins/eat/" + str(uid) + ".jpg",
download_big=True download_big=True
) )
try: try:
markImg = Image.open("plugins/eat/" + str(from_user.user.id) + ".jpg") markImg = Image.open("plugins/eat/" + str(uid) + ".jpg")
maskImg = Image.open("plugins/eat/mask" + str(numberPosition[2]) + ".png") maskImg = Image.open("plugins/eat/mask" + str(numberPosition[2]) + ".png")
except: except:
await context.edit(f"图片模版加载出错请检查并更新配置mask{str(numberPosition[2])}.png") await context.edit(f"图片模版加载出错请检查并更新配置mask{str(numberPosition[2])}.png")
return base return base
base = await eat_it(context, from_user, base, maskImg, markImg, numberPosition[2], layer + 1) base = await eat_it(context, uid, base, maskImg, markImg, numberPosition[2], layer + 1)
temp = base.size[0] if base.size[0] > base.size[1] else base.size[1] temp = base.size[0] if base.size[0] > base.size[1] else base.size[1]
if temp != 512: if temp != 512:
@ -199,7 +205,6 @@ async def downloadFileByIds(ids, context):
await context.edit("更新下载模版图片失败,请确认配置文件是否正确") await context.edit("更新下载模版图片失败,请确认配置文件是否正确")
@listener(is_plugin=True, outgoing=True, command=alias_command("eat"), @listener(is_plugin=True, outgoing=True, command=alias_command("eat"),
description="生成一张 吃头像 图片\n" description="生成一张 吃头像 图片\n"
"可选:当第二个参数是数字时,读取预存的配置;\n\n" "可选:当第二个参数是数字时,读取预存的配置;\n\n"
@ -214,8 +219,8 @@ async def eat(context: NewMessage.Event):
await context.edit("出错了呜呜呜 ~ 无效的参数。") await context.edit("出错了呜呜呜 ~ 无效的参数。")
return return
diu_round = False diu_round = False
user_object = await context.client.get_me() from_user = user_object = context.sender
from_user = await context.client(GetFullUserRequest(user_object.id)) from_user_id = await get_full_id(from_user)
if context.reply_to_msg_id: if context.reply_to_msg_id:
reply_message = await context.get_reply_message() reply_message = await context.get_reply_message()
try: try:
@ -223,7 +228,12 @@ async def eat(context: NewMessage.Event):
except AttributeError: except AttributeError:
await context.edit("出错了呜呜呜 ~ 无效的参数。") await context.edit("出错了呜呜呜 ~ 无效的参数。")
return return
if user_id > 0:
target_user = await context.client(GetFullUserRequest(user_id)) target_user = await context.client(GetFullUserRequest(user_id))
target_user_id = target_user.user.id
else:
target_user = await context.client(GetFullChannelRequest(user_id))
target_user_id = target_user.full_chat.id
else: else:
user_raw = "" user_raw = ""
if len(context.parameter) == 1 or len(context.parameter) == 2: if len(context.parameter) == 1 or len(context.parameter) == 2:
@ -235,18 +245,25 @@ async def eat(context: NewMessage.Event):
if context.message.entities is not None: if context.message.entities is not None:
if isinstance(context.message.entities[0], MessageEntityMentionName): if isinstance(context.message.entities[0], MessageEntityMentionName):
target_user = await context.client(GetFullUserRequest(context.message.entities[0].user_id)) target_user = await context.client(GetFullUserRequest(context.message.entities[0].user_id))
target_user_id = target_user.user.id
elif isinstance(context.message.entities[0], MessageEntityPhone): elif isinstance(context.message.entities[0], MessageEntityPhone):
if user > 0:
target_user = await context.client(GetFullUserRequest(user)) target_user = await context.client(GetFullUserRequest(user))
target_user_id = target_user.user.id
else:
target_user = await context.client(GetFullChannelRequest(user))
target_user_id = target_user.full_chat.id
elif isinstance(context.message.entities[0], MessageEntityBotCommand): elif isinstance(context.message.entities[0], MessageEntityBotCommand):
target_user = from_user target_user = await context.client(GetFullUserRequest(user_object.id))
target_user_id = target_user.user.id
else: else:
return await context.edit("出错了呜呜呜 ~ 参数错误。") return await context.edit("出错了呜呜呜 ~ 参数错误。")
elif user_raw[:1] in [".", "/", "-", "!"]: elif user_raw[:1] in [".", "/", "-", "!"]:
target_user = from_user target_user_id = await get_full_id(from_user)
else: else:
try: try:
user_object = await context.client.get_entity(user) user_object = await context.client.get_entity(user)
target_user = await context.client(GetFullUserRequest(user_object.id)) target_user_id = await get_full_id(user_object)
except (TypeError, ValueError, OverflowError, StructError) as exception: except (TypeError, ValueError, OverflowError, StructError) as exception:
if str(exception).startswith("Cannot find any entity corresponding to"): if str(exception).startswith("Cannot find any entity corresponding to"):
await context.edit("出错了呜呜呜 ~ 指定的用户不存在。") await context.edit("出错了呜呜呜 ~ 指定的用户不存在。")
@ -263,13 +280,13 @@ async def eat(context: NewMessage.Event):
return return
raise exception raise exception
photo = await context.client.download_profile_photo( photo = await context.client.download_profile_photo(
target_user.user.id, target_user_id,
"plugins/eat/" + str(target_user.user.id) + ".jpg", "plugins/eat/" + str(target_user_id) + ".jpg",
download_big=True download_big=True
) )
reply_to = context.message.reply_to_msg_id reply_to = context.message.reply_to_msg_id
if exists("plugins/eat/" + str(target_user.user.id) + ".jpg"): if exists("plugins/eat/" + str(target_user_id) + ".jpg"):
for num in range(1, max_number + 1): for num in range(1, max_number + 1):
print(num) print(num)
if not exists('plugins/eat/eat' + str(num) + '.png'): if not exists('plugins/eat/eat' + str(num) + '.png'):
@ -416,7 +433,7 @@ async def eat(context: NewMessage.Event):
except: except:
notifyStr = "吃头像" notifyStr = "吃头像"
await context.edit(f"正在生成 {notifyStr} 图片中 . . .") await context.edit(f"正在生成 {notifyStr} 图片中 . . .")
markImg = Image.open("plugins/eat/" + str(target_user.user.id) + ".jpg") markImg = Image.open("plugins/eat/" + str(target_user_id) + ".jpg")
try: try:
eatImg = Image.open("plugins/eat/eat" + str(number) + ".png") eatImg = Image.open("plugins/eat/eat" + str(number) + ".png")
maskImg = Image.open("plugins/eat/mask" + str(number) + ".png") maskImg = Image.open("plugins/eat/mask" + str(number) + ".png")
@ -430,14 +447,14 @@ async def eat(context: NewMessage.Event):
number = str(number) number = str(number)
except: except:
pass pass
result = await eat_it(context, from_user, eatImg, maskImg, markImg, number) result = await eat_it(context, from_user_id, eatImg, maskImg, markImg, number)
result.save('plugins/eat/eat.webp') result.save('plugins/eat/eat.webp')
target_file = await context.client.upload_file("plugins/eat/eat.webp") target_file = await context.client.upload_file("plugins/eat/eat.webp")
try: try:
remove("plugins/eat/" + str(target_user.user.id) + ".jpg") remove("plugins/eat/" + str(target_user_id) + ".jpg")
remove("plugins/eat/" + str(target_user.user.id) + ".png") remove("plugins/eat/" + str(target_user_id) + ".png")
remove("plugins/eat/" + str(from_user.user.id) + ".jpg") remove("plugins/eat/" + str(from_user_id) + ".jpg")
remove("plugins/eat/" + str(from_user.user.id) + ".png") remove("plugins/eat/" + str(from_user_id) + ".png")
remove("plugins/eat/eat.webp") remove("plugins/eat/eat.webp")
remove(photo) remove(photo)
except: except:

View File

@ -202,10 +202,10 @@
}, },
{ {
"name": "eat", "name": "eat",
"version": "1.040", "version": "1.041",
"section": "profile", "section": "profile",
"maintainer": "SF_PICK", "maintainer": "SF_PICK",
"size": "6.4 kb", "size": "22.3 kb",
"supported": false, "supported": false,
"des-short": "生成一张 吃头像 图片。", "des-short": "生成一张 吃头像 图片。",
"des": "这个人很懒,什么都没有留下。" "des": "这个人很懒,什么都没有留下。"