eat 优化判断

This commit is contained in:
xtaodada 2021-11-10 20:40:33 +08:00
parent d12d4d8be9
commit 9ebdc9a229
No known key found for this signature in database
GPG Key ID: EE4DC37B55E24736
2 changed files with 41 additions and 34 deletions

39
eat.py
View File

@ -6,7 +6,7 @@ from os import remove
from requests import get
from random import randint
from telethon.tl.functions.users import GetFullUserRequest
from telethon.tl.types import MessageEntityMentionName
from telethon.tl.types import MessageEntityMentionName, MessageEntityPhone
from telethon.errors.rpcerrorlist import ChatSendStickersForbiddenError
from struct import error as StructError
from pagermaid.listener import listener
@ -15,7 +15,6 @@ from pagermaid import redis, config
from collections import defaultdict
import json
try:
git_source = config['git_source']
except:
@ -36,7 +35,7 @@ configFilePath = 'plugins/eat/config.json'
configFileRemoteUrlKey = "eat.configFileRemoteUrl"
async def eat_it(context, base, mask, photo, number, layer = 0):
async def eat_it(context, from_user, base, mask, photo, number, layer=0):
mask_size = mask.size
photo_size = photo.size
if mask_size[0] < photo_size[0] and mask_size[1] < photo_size[1]:
@ -62,7 +61,7 @@ async def eat_it(context, base, mask, photo, number, layer = 0):
except:
await context.edit(f"图片模版加载出错请检查并更新配置mask{str(numberPosition[2])}.png")
return base
base = await eat_it(context, base, maskImg, markImg, numberPosition[2], layer+1)
base = await eat_it(context, from_user, base, maskImg, markImg, numberPosition[2], layer + 1)
temp = base.size[0] if base.size[0] > base.size[1] else base.size[1]
if temp != 512:
@ -72,14 +71,14 @@ async def eat_it(context, base, mask, photo, number, layer = 0):
return base
async def updateConfig(configFilePath, context):
async def updateConfig(context):
configFileRemoteUrl = redis.get(configFileRemoteUrlKey)
if configFileRemoteUrl:
if downloadFileFromUrl(configFileRemoteUrl, configFilePath) != 0:
redis.set(configFileRemoteUrlKey, configFileRemoteUrl)
return -1
else:
return await loadConfigFile(configFilePath, context, True)
return await loadConfigFile(context, True)
return 0
@ -93,7 +92,7 @@ def downloadFileFromUrl(url, filepath):
return 0
async def loadConfigFile(configFilePath, context, forceDownload = False):
async def loadConfigFile(context, forceDownload=False):
global positions, notifyStrArr
try:
with open(configFilePath, 'r', encoding='utf8') as cf:
@ -154,7 +153,6 @@ async def eat(context):
return
diu_round = False
user_object = await context.client.get_me()
global from_user
from_user = await context.client(GetFullUserRequest(user_object.id))
if context.reply_to_msg_id:
reply_message = await context.get_reply_message()
@ -165,15 +163,23 @@ async def eat(context):
return
target_user = await context.client(GetFullUserRequest(user_id))
else:
user_raw = ""
if len(context.parameter) == 1 or len(context.parameter) == 2:
user = context.parameter[0]
user_raw = user = context.parameter[0]
if user.isnumeric():
user = int(user)
else:
user = user_object.id
if context.message.entities is not None:
if isinstance(context.message.entities[0], MessageEntityMentionName):
return await context.client(GetFullUserRequest(context.message.entities[0].user_id))
target_user = await context.client(GetFullUserRequest(context.message.entities[0].user_id))
elif isinstance(context.message.entities[0], MessageEntityPhone):
target_user = int(context.parameter[0])
else:
return await context.edit("出错了呜呜呜 ~ 参数错误。")
elif user_raw[:1] in [".", "/", "-", "!"]:
target_user = from_user
else:
try:
user_object = await context.client.get_entity(user)
target_user = await context.client(GetFullUserRequest(user_object.id))
@ -184,7 +190,8 @@ async def eat(context):
if str(exception).startswith("No user has"):
await context.edit("出错了呜呜呜 ~ 指定的道纹不存在。")
return
if str(exception).startswith("Could not find the input entity for") or isinstance(exception, StructError):
if str(exception).startswith("Could not find the input entity for") or isinstance(exception,
StructError):
await context.edit("出错了呜呜呜 ~ 无法通过此 UserID 找到对应的用户。")
return
if isinstance(exception, OverflowError):
@ -253,14 +260,14 @@ async def eat(context):
else:
# 下载成功,加载配置文件
redis.set(configFileRemoteUrlKey, p2)
if await loadConfigFile(configFilePath, context, True) != 0:
if await loadConfigFile(context, True) != 0:
await context.edit(f"加载配置文件异常,请确认从远程下载的配置文件格式是否正确")
return
else:
await context.edit(f"下载并加载配置文件成功")
else:
# 没传url直接更新
if await updateConfig(configFilePath, context) != 0:
if await updateConfig(context) != 0:
await context.edit(f"更新配置文件异常,请确认从远程下载的配置文件格式是否正确")
return
else:
@ -269,7 +276,7 @@ async def eat(context):
elif p1[0] == "" or p1[0] == "!":
# 加载配置
if exists(configFilePath):
if await loadConfigFile(configFilePath, context) != 0:
if await loadConfigFile(context) != 0:
await context.edit(f"加载配置文件异常,请确认从远程下载的配置文件格式是否正确")
return
txt = ""
@ -311,7 +318,7 @@ async def eat(context):
# 加载配置
if exists(configFilePath):
if await loadConfigFile(configFilePath, context) != 0:
if await loadConfigFile(context) != 0:
await context.edit(f"加载配置文件异常,请确认从远程下载的配置文件格式是否正确")
return
@ -334,7 +341,7 @@ async def eat(context):
number = str(number)
except:
pass
result = await eat_it(context, eatImg, maskImg, markImg, number)
result = await eat_it(context, from_user, eatImg, maskImg, markImg, number)
result.save('plugins/eat/eat.webp')
target_file = await context.client.upload_file("plugins/eat/eat.webp")
try:

View File

@ -202,7 +202,7 @@
},
{
"name": "eat",
"version": "1.035",
"version": "1.036",
"section": "profile",
"maintainer": "SF_PICK",
"size": "6.4 kb",