mirror of
https://github.com/xuthus83/LittlePaimon.git
synced 2025-04-12 23:29:37 +08:00
新增对话式查询武器和原魔,添加鹿野院平藏材料,修复深渊登场率
This commit is contained in:
parent
ec5fa675c1
commit
c04406ceb0
@ -3,6 +3,7 @@ import random
|
||||
from nonebot import on_regex, on_command, logger
|
||||
from nonebot.matcher import matchers
|
||||
from nonebot.rule import Rule
|
||||
from nonebot import get_driver
|
||||
from nonebot.adapters.onebot.v11 import Bot, MessageEvent, GroupMessageEvent, MessageSegment
|
||||
from nonebot.exception import FinishedException
|
||||
|
||||
@ -11,6 +12,8 @@ from utils.auth_util import FreqLimiter2
|
||||
from utils.message_util import MessageBuild
|
||||
from utils.file_handler import load_json_from_url
|
||||
|
||||
driver = get_driver()
|
||||
|
||||
voice_url = 'https://static.cherishmoon.fun/LittlePaimon/voice/'
|
||||
chat_lmt = FreqLimiter2(60)
|
||||
|
||||
@ -27,8 +30,8 @@ def check_group(event: GroupMessageEvent) -> bool:
|
||||
async def update_paimon_voice(event: MessageEvent):
|
||||
try:
|
||||
old_len = len([m for m in matchers[10] if m.plugin_name == 'Paimon_Chat'])
|
||||
voice_list = await load_json_from_url('https://static.cherishmoon.fun/LittlePaimon/voice/voice_list.json')
|
||||
matchers[10] = [m for m in matchers[10] if m.plugin_name != 'Paimon_Chat']
|
||||
voice_list = load_json_from_url('https://static.cherishmoon.fun/LittlePaimon/voice/voice_list.json')
|
||||
for key, value in voice_list.items():
|
||||
create_matcher(key, value['pattern'], value['cooldown'], value['pro'], value['files'])
|
||||
new_len = len(voice_list) - old_len
|
||||
@ -65,6 +68,8 @@ def create_matcher(chat_word: str, pattern: str, cooldown: int, pro: float, resp
|
||||
logger.error('派蒙发送语音失败', e)
|
||||
|
||||
|
||||
voice_list = load_json_from_url('https://static.cherishmoon.fun/LittlePaimon/voice/voice_list.json')
|
||||
for k, v in voice_list.items():
|
||||
create_matcher(k, v['pattern'], v['cooldown'], v['pro'], v['files'])
|
||||
@driver.on_startup
|
||||
async def load_voice():
|
||||
voice_list = await load_json_from_url('https://static.cherishmoon.fun/LittlePaimon/voice/voice_list.json')
|
||||
for k, v in voice_list.items():
|
||||
create_matcher(k, v['pattern'], v['cooldown'], v['pro'], v['files'])
|
||||
|
@ -3,12 +3,13 @@ import re
|
||||
import time
|
||||
|
||||
from nonebot import on_endswith, on_command, on_regex
|
||||
from nonebot.adapters.onebot.v11 import MessageSegment, MessageEvent
|
||||
from nonebot.adapters.onebot.v11 import MessageEvent, Message, MessageSegment
|
||||
from nonebot.params import RegexDict
|
||||
from nonebot.typing import T_State
|
||||
|
||||
from utils.character_alias import get_id_by_alias
|
||||
from utils.decorator import exception_handler
|
||||
from utils.message_util import MessageBuild
|
||||
from utils.message_util import MessageBuild, match_alias
|
||||
from .abyss_rate_draw import draw_rate_rank, draw_teams_rate
|
||||
from .blue import get_blue_pic
|
||||
|
||||
@ -54,7 +55,7 @@ async def genshin_guide(event: MessageEvent):
|
||||
async def genshin_material(event: MessageEvent):
|
||||
name: str = event.message.extract_plain_text().replace('角色材料', '').strip()
|
||||
realname = get_id_by_alias(name)
|
||||
if name in ['夜兰', '久岐忍'] or realname:
|
||||
if name in ['夜兰', '久岐忍', '鹿野院平藏'] or realname:
|
||||
name = realname[1][0] if realname else name
|
||||
await material.finish(await MessageBuild.StaticImage(f'LittlePaimon/RoleMaterials/{name}材料.jpg'))
|
||||
else:
|
||||
@ -132,15 +133,55 @@ async def abyss_team_handler(event: MessageEvent, reGroup=RegexDict()):
|
||||
await abyss_team.finish(abyss_img)
|
||||
|
||||
|
||||
@weapon_guide.handle()
|
||||
@exception_handler()
|
||||
async def weapon_guide_handler(event: MessageEvent):
|
||||
name: str = event.message.extract_plain_text().replace('武器攻略', '').strip()
|
||||
await weapon_guide.finish(await MessageBuild.StaticImage(url=f'LittlePaimon/WeaponGuild/{name}.png'))
|
||||
def create_choice_command(endswith: str, type_: str, url: str, file_type: str = 'jpg'):
|
||||
command = on_endswith(endswith, priority=6, block=False)
|
||||
|
||||
@command.handle()
|
||||
async def _(event: MessageEvent, state: T_State):
|
||||
name = event.message.extract_plain_text().replace(endswith, '').strip()
|
||||
if name:
|
||||
state['name'] = name
|
||||
|
||||
@command.got('name', prompt=f'请把要查询的{endswith[0:2]}告诉我哦~')
|
||||
async def _(event: MessageEvent, state: T_State):
|
||||
name = state['name']
|
||||
if isinstance(name, Message):
|
||||
name = name.extract_plain_text().strip()
|
||||
if name == 'q':
|
||||
await command.finish()
|
||||
finally_name = await match_alias(name, type_)
|
||||
if isinstance(finally_name, str):
|
||||
await command.finish(await MessageBuild.StaticImage(url=url + finally_name + '.' + file_type))
|
||||
elif isinstance(finally_name, list):
|
||||
if not finally_name:
|
||||
await command.finish(f'没有该{endswith[0:2]}的信息哦,问一个别的吧~')
|
||||
else:
|
||||
if 'choice' not in state:
|
||||
msg = f'你要找的{endswith[0:2]}是哪个呀:\n'
|
||||
msg += '\n'.join([f'{int(i) + 1}. {name}' for i, name in enumerate(finally_name)])
|
||||
await command.send(msg + '\n回答\"q\"可以取消查询')
|
||||
state['match_alias'] = finally_name
|
||||
|
||||
@command.got('choice')
|
||||
async def _(event: MessageEvent, state: T_State):
|
||||
match_alias = state['match_alias']
|
||||
choice = state['choice']
|
||||
choice = choice.extract_plain_text().strip()
|
||||
if choice == 'q':
|
||||
await command.finish()
|
||||
if choice.isdigit() and (1 <= int(choice) <= len(match_alias)):
|
||||
await command.finish(
|
||||
await MessageBuild.StaticImage(url=url + match_alias[int(choice) - 1] + '.' + file_type))
|
||||
if choice not in match_alias:
|
||||
state['times'] = state['times'] + 1 if 'times' in state else 1
|
||||
if state['times'] >= 3:
|
||||
await command.finish(f'看来旅行者您有点神志不清哦(,下次再问派蒙吧' + MessageSegment.face(146))
|
||||
elif state['times'] == 1:
|
||||
await command.reject(f'请旅行者从上面的{endswith[0:2]}中选一个问派蒙\n回答\"q\"可以取消查询')
|
||||
elif state['times'] == 2:
|
||||
await command.reject(f'别调戏派蒙啦,快选一个吧,不想问了麻烦回答\"q\"!')
|
||||
await command.finish(await MessageBuild.StaticImage(url=url + choice + '.' + file_type))
|
||||
|
||||
|
||||
@monster_map.handle()
|
||||
@exception_handler()
|
||||
async def monster_map_handler(event: MessageEvent):
|
||||
name = event.message.extract_plain_text().replace('原魔图鉴', '').strip()
|
||||
await monster_map.finish(await MessageBuild.StaticImage(url=f'LittlePaimon/MonsterMaps/{name}.jpg'))
|
||||
create_choice_command('原魔图鉴', 'monsters', 'LittlePaimon/MonsterMaps/', 'jpg')
|
||||
create_choice_command('武器攻略', 'weapons', 'LittlePaimon/WeaponGuild/', 'png')
|
||||
|
@ -37,9 +37,8 @@ async def draw_rate_rank(type: str = 'role', mode: str = 'used'):
|
||||
role_draw.text((28 if len(role['rate']) == 6 else 38, 158), role['rate'], font=get_font(30), fill='black')
|
||||
bg_img.alpha_composite(role_img, (50 + 204 * (n % 5), 180 + 240 * int(n / 5)))
|
||||
n += 1
|
||||
bg_img = pil2b64(bg_img, 75)
|
||||
bg_img = MessageSegment.image(bg_img)
|
||||
return bg_img
|
||||
|
||||
return MessageBuild.Image(bg_img, quality=75)
|
||||
|
||||
|
||||
async def draw_teams_rate(floor='上半半'):
|
||||
|
@ -1,9 +1,9 @@
|
||||
import json
|
||||
import requests
|
||||
from pathlib import Path
|
||||
from typing import Union, Optional, Tuple
|
||||
from ssl import SSLCertVerificationError
|
||||
from PIL import Image
|
||||
from utils import aiorequests
|
||||
|
||||
|
||||
def load_image(
|
||||
@ -37,12 +37,18 @@ def load_json(file: str = None, path: Union[Path, str] = None, encoding: str = '
|
||||
return json.load(path.open('r', encoding=encoding))
|
||||
|
||||
|
||||
def load_json_from_url(url: str):
|
||||
try:
|
||||
resp = requests.get(url)
|
||||
except SSLCertVerificationError:
|
||||
resp = requests.get(url.replace('https', 'http'))
|
||||
return resp.json()
|
||||
async def load_json_from_url(url: str, encoding: str = 'utf-8', refresh: bool = False):
|
||||
if 'static.cherishmoon.fun' in url:
|
||||
url_path = Path() / 'data' / url.split('static.cherishmoon.fun/')[1]
|
||||
if refresh or not url_path.exists():
|
||||
try:
|
||||
resp = await aiorequests.get(url)
|
||||
except SSLCertVerificationError:
|
||||
resp = await aiorequests.get(url.replace('https', 'http'))
|
||||
save_json(resp.json(), path=url_path, encoding=encoding)
|
||||
return resp.json()
|
||||
else:
|
||||
return load_json(path=url_path, encoding=encoding)
|
||||
|
||||
|
||||
def save_json(data, file: str = None, path: Union[Path, str] = None, encoding: str = 'utf-8'):
|
||||
|
@ -1,5 +1,6 @@
|
||||
import re
|
||||
import base64
|
||||
import difflib
|
||||
|
||||
from PIL import Image
|
||||
from pathlib import Path
|
||||
@ -10,7 +11,7 @@ from nonebot import get_bot, logger
|
||||
from nonebot.adapters.onebot.v11 import MessageEvent, Message, MessageSegment
|
||||
|
||||
from .db_util import get_last_query, update_last_query
|
||||
from .file_handler import load_image
|
||||
from .file_handler import load_image, load_json_from_url
|
||||
from . import aiorequests
|
||||
|
||||
|
||||
@ -132,3 +133,21 @@ def get_message_id(event):
|
||||
return event.group_id
|
||||
elif event.message_type == 'guild':
|
||||
return event.channel_id
|
||||
|
||||
|
||||
async def match_alias(msg: str, type: str = 'weapons') -> Union[str, list]:
|
||||
alias_file = await load_json_from_url(url='https://static.cherishmoon.fun/LittlePaimon/alias.json')
|
||||
alias_list = alias_file[type]
|
||||
if type == 'weapons':
|
||||
possible = []
|
||||
for name, alias in alias_list.items():
|
||||
match_list = difflib.get_close_matches(msg, alias, cutoff=0.4, n=5)
|
||||
if msg in match_list:
|
||||
return name
|
||||
elif match_list:
|
||||
possible.append(name)
|
||||
return possible
|
||||
elif type == 'monsters':
|
||||
match_list = difflib.get_close_matches(msg, alias_list, cutoff=0.4, n=5)
|
||||
return match_list[0] if len(match_list) == 1 else match_list
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user