ysc支持获取原图

This commit is contained in:
CMHopeSunshine 2022-10-22 12:10:31 +08:00
parent 1285cb7a74
commit 3504937891
4 changed files with 44 additions and 8 deletions

View File

@ -51,5 +51,10 @@ GREET_CONFIG = Path() / 'config' / 'paimon_greet.yml'
GREET_CONFIG_DEFAULT = Path() / 'config' / 'paimon_greet_default.yml'
# 群聊学习配置文件
LEARNING_CHAT_CONFIG = Path() / 'config' / 'learning_chat.yml'
# 米游社帖子订阅文件,未用上
MIHOYO_BBS_POST_PATH = Path() / 'data' / 'LittlePaimon' / '米游社帖子订阅.json'
# ysc原图临时目录
YSC_TEMP_IMG_PATH = Path() / 'data' / 'LittlePaimon' / 'temp_img'
YSC_TEMP_IMG_PATH.mkdir(parents=True, exist_ok=True)

View File

@ -1,4 +1,5 @@
import datetime
from LittlePaimon.config import YSC_TEMP_IMG_PATH
from LittlePaimon.utils import scheduler, logger
from LittlePaimon.database.models import GuessVoiceRank, PluginStatistics, DailyNoteSub, CookieCache, PublicCookie
@ -20,3 +21,7 @@ async def _():
if now.weekday() == 0:
logger.info('原神猜语音', '清空每周排行榜')
await GuessVoiceRank.all().delete()
if YSC_TEMP_IMG_PATH.exists():
YSC_TEMP_IMG_PATH.unlink()
YSC_TEMP_IMG_PATH.mkdir(parents=True, exist_ok=True)

View File

@ -1,12 +1,14 @@
from nonebot import on_command
from nonebot.adapters.onebot.v11 import Message, MessageEvent, MessageSegment
from nonebot.adapters.onebot.v11.helpers import HandleCancellation
from nonebot.rule import Rule
from nonebot.params import ArgPlainText, CommandArg
from nonebot.plugin import PluginMetadata
from nonebot.typing import T_State
from LittlePaimon import NICKNAME
from LittlePaimon.database.models import PlayerAlias
from LittlePaimon.config import YSC_TEMP_IMG_PATH
from LittlePaimon.utils import logger
from LittlePaimon.utils.message import CommandPlayer, CommandCharacter, CommandUID
from LittlePaimon.utils.genshin import GenshinInfoManager
@ -29,6 +31,11 @@ __plugin_meta__ = PluginMetadata(
}
)
def has_raw_rule(event: MessageEvent) -> bool:
return bool(event.reply and (YSC_TEMP_IMG_PATH / f'{event.reply.message_id}.jpg').exists())
ys = on_command('ys', aliases={'原神卡片', '个人卡片'}, priority=10, block=True, state={
'pm_name': 'ys',
'pm_description': '查看原神个人信息卡片',
@ -77,6 +84,12 @@ show_alias = on_command('查看别名', priority=10, block=True, state={
'pm_usage': '查看别名',
'pm_priority': 9
})
raw_img_cmd = on_command('原图', priority=10, block=True, rule=Rule(has_raw_rule), state={
'pm_name': 'ysc原图',
'pm_description': '获取ysc指令卡片中的原神同人图',
'pm_usage': '(回复ysc消息) 原图',
'pm_priority': 10
})
@ys.handle()
@ -139,6 +152,7 @@ async def _(event: MessageEvent, players=CommandPlayer(2)):
async def _(event: MessageEvent, players=CommandPlayer(only_cn=False), characters=CommandCharacter()):
logger.info('原神角色卡片', '开始执行')
msg = Message()
temp_img = None
if len(players) == 1:
# 当查询对象只有一个时,查询所有角色
gim = GenshinInfoManager(players[0].user_id, players[0].uid)
@ -150,7 +164,7 @@ async def _(event: MessageEvent, players=CommandPlayer(only_cn=False), character
logger.info('原神角色卡片', '➤➤', {'角色': character}, '没有该角色信息,发送随机图', True)
msg += MessageSegment.image(f'http://img.genshin.cherishmoon.fun/{character}')
else:
img = await draw_chara_card(character_info)
img, temp_img = await draw_chara_card(character_info)
logger.info('原神角色卡片', '➤➤', {'角色': character}, '制图完成', True)
msg += img
else:
@ -164,10 +178,18 @@ async def _(event: MessageEvent, players=CommandPlayer(only_cn=False), character
logger.info('原神角色卡片', '➤➤', {'角色': characters[0]}, '没有该角色信息,发送随机图', True)
msg += MessageSegment.image(f'http://img.genshin.cherishmoon.fun/{characters[0]}')
else:
img = await draw_chara_card(character_info)
img, temp_img = await draw_chara_card(character_info)
logger.info('原神角色卡片', '➤➤', {'角色': characters[0]}, '制图完成', True)
msg += img
await ysd.finish(msg, at_sender=True)
send_result = await ysd.send(msg, at_sender=True)
if temp_img:
temp_img.convert('RGB').save(YSC_TEMP_IMG_PATH / f'{send_result["message_id"]}.jpg', )
@raw_img_cmd.handle()
async def _(event: MessageEvent):
with open(YSC_TEMP_IMG_PATH / f'{event.reply.message_id}.jpg', 'rb') as f:
await raw_img_cmd.finish(MessageSegment.image(f.read()))
@ysd.handle()
@ -240,7 +262,8 @@ async def _(event: MessageEvent, state: T_State, msg: Message = CommandArg()):
state['alias'] = Message(msg[1])
@add_alias.got('alias', prompt=Message.template('你想把{chara}设置为你的谁呢?'), parameterless=[HandleCancellation(f'好吧,有事再找{NICKNAME}')])
@add_alias.got('alias', prompt=Message.template('你想把{chara}设置为你的谁呢?'),
parameterless=[HandleCancellation(f'好吧,有事再找{NICKNAME}')])
async def _(event: MessageEvent, chara: str = ArgPlainText('chara'), alias: str = ArgPlainText('alias')):
await PlayerAlias.update_or_create(user_id=str(event.user_id), alias=alias, defaults={'character': chara})
await add_alias.finish(f'设置成功,{NICKNAME}知道{chara}是你的{alias}啦..')
@ -251,12 +274,14 @@ async def _(event: MessageEvent, state: T_State, msg: Message = CommandArg()):
if msg:
state['alias'] = msg
elif aliases := await PlayerAlias.filter(user_id=str(event.user_id)).all():
state['msg'] = '你已设置以下别名:\n' + '\n'.join([f'{i.alias} -> {i.character}' for i in aliases]) + '\n请输入你想删除的别名或发送"全部"删除全部别名'
state['msg'] = '你已设置以下别名:\n' + '\n'.join(
[f'{i.alias} -> {i.character}' for i in aliases]) + '\n请输入你想删除的别名或发送"全部"删除全部别名'
else:
await delete_alias.finish('你还没有设置任何别名哦')
@delete_alias.got('alias', prompt=Message.template('{msg}'), parameterless=[HandleCancellation(f'好吧,有事再找{NICKNAME}')])
@delete_alias.got('alias', prompt=Message.template('{msg}'),
parameterless=[HandleCancellation(f'好吧,有事再找{NICKNAME}')])
async def _(event: MessageEvent, msg: str = ArgPlainText('alias')):
if msg == '全部':
await PlayerAlias.filter(user_id=str(event.user_id)).delete()

View File

@ -29,7 +29,7 @@ talent_color = [('#d5f2b6', '#6d993d'), ('#d5f2b6', '#6d993d'), ('#d5f2b6', '#6d
async def draw_chara_card(info: Character):
img = await aiorequests.get_img(f'http://img.genshin.cherishmoon.fun/{info.name}', mode='RGBA')
if img == 'No Such File':
return '暂时没有这个角色的同人图哦~'
return '暂时没有这个角色的同人图哦~', None
# 新建卡片
card = PMImage(size=(385, 500), color=(0, 0, 0, 0), mode='RGBA')
await card.draw_rounded_rectangle2((0, 0), (card.width, card.height), 80, (0, 0, 0, 153), ['ur'])
@ -133,6 +133,7 @@ async def draw_chara_card(info: Character):
per = 1 / ((card.width / img.width) / 0.4)
await card.resize(per)
raw_img = img.copy()
img.alpha_composite(card.image, (0, img.height - card.height))
return MessageBuild.Image(img, mode='RGB')
return MessageBuild.Image(img, mode='RGB'), raw_img