🎨 抽卡记录链接功能放到抽卡记录插件里

This commit is contained in:
CMHopeSunshine 2023-03-23 23:51:04 +08:00
parent 63f5080edc
commit 5e867d4603
2 changed files with 78 additions and 83 deletions

View File

@ -1,16 +1,20 @@
import time
from typing import Union
from asyncio import get_running_loop
from urllib.parse import quote
from nonebot import on_command
from nonebot.adapters.onebot.v11 import (
Bot,
MessageEvent,
GroupMessageEvent,
PrivateMessageEvent,
PrivateMessageEvent, ActionFailed,
)
from nonebot.plugin import PluginMetadata
from LittlePaimon.utils import logger
from LittlePaimon.utils.message import CommandPlayer
from LittlePaimon.utils import logger, NICKNAME
from LittlePaimon.utils.message import CommandPlayer, CommandUID
from LittlePaimon.utils.api import get_authkey_by_stoken
from .data_source import (
get_gacha_log_img,
get_gacha_log_data,
@ -78,6 +82,18 @@ export_log = on_command(
'pm_priority': 4,
},
)
gacha_url = on_command(
'查看抽卡记录链接',
aliases={'导出抽卡记录链接'},
priority=11,
block=True,
state={
'pm_name': '查看抽卡记录链接',
'pm_description': '*获取你的抽卡记录链接',
'pm_usage': '查看抽卡记录链接(uid)',
'pm_priority': 5,
},
)
running_update = []
running_show = []
@ -153,3 +169,62 @@ async def _(
)
except Exception as e:
await export_log.finish(f'上传文件失败,错误信息:{e}', at_sender=True)
@gacha_url.handle()
async def _(
bot: Bot,
event: MessageEvent,
uid: str = CommandUID()
):
await gacha_url.send("正在获取抽卡记录链接")
logger.info('获取抽卡记录链接', '开始执行')
authkey, state, _ = await get_authkey_by_stoken(str(event.user_id), uid)
if not state:
return authkey
if authkey == {}:
await gacha_url.finish(authkey, at_sender=True)
region = 'cn_qd01' if uid[0] == '5' else 'cn_gf01'
url = (
f"https://hk4e-api.mihoyo.com/event/gacha_info/api/getGachaLog?"
f"authkey_ver=1&sign_type=2&auth_appid=webview_gacha&init_type=301&"
f"gacha_id=fecafa7b6560db5f3182222395d88aaa6aaac1bc"
f"&timestamp={int(time.time())}"
f"&lang=zh-cn&device_type=mobile&plat_type=ios&region={region}"
f"&authkey={quote(authkey,'utf-8')}"
f"&game_biz=hk4e_cn&gacha_type=301&page=1&size=5&end_id=0"
)
msgs = [
{
"type": "node",
"data": {
"name": NICKNAME,
"uin": event.self_id,
"content": f"UID{uid}的抽卡记录链接为:",
},
},
{
"type": "node",
"data": {"name": NICKNAME, "uin": event.self_id, "content": url},
},
]
try:
if isinstance(event, PrivateMessageEvent):
msg = await bot.call_api(
"send_private_forward_msg", user_id=event.user_id, messages=msgs
)
else:
msg = await bot.call_api(
"send_group_forward_msg", group_id=event.group_id, messages=msgs
)
loop = get_running_loop()
loop.call_later(85,
lambda: loop.create_task(bot.delete_msg(message_id=msg['message_id'])))
except ActionFailed:
logger.info('抽卡记录', '➤抽卡记录链接合并转发<r>发送失败</r>,尝试以普通信息发送')
try:
await gacha_url.finish(f"UID{uid}的抽卡记录链接为:\n{url}")
except ActionFailed as e:
logger.info('抽卡记录', f'➤抽卡记录链接<r>发送失败</r>{e}')
except Exception as e:
logger.info('抽卡记录', f'➤抽卡记录链接<r>发送失败</r>,报错信息:{e}')

View File

@ -1,80 +0,0 @@
from time import time
from urllib.parse import quote
from datetime import datetime
from typing import Union
from nonebot import on_command
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, PrivateMessageEvent
from nonebot.adapters.onebot.v11.exception import ActionFailed
from LittlePaimon.utils import logger, scheduler
from LittlePaimon.utils.message import CommandUID
from LittlePaimon.utils.api import get_authkey_by_stoken
gacha_url = on_command(
'查看抽卡记录链接',
aliases={'导出抽卡记录链接'},
priority=11,
block=True,
state={
'pm_name': '查看抽卡记录链接',
'pm_description': '*获取你的抽卡记录链接',
'pm_usage': '查看抽卡记录链接(uid)',
'pm_priority': 1,
},
)
async def withdraw(bot: Bot, message_id: int) -> None:
"""撤回消息任务"""
await bot.delete_msg(message_id=message_id)
@gacha_url.handle()
async def _(
bot: Bot,
event: Union[GroupMessageEvent, PrivateMessageEvent],
uid=CommandUID()
):
await gacha_url.send("正在获取抽卡记录链接")
logger.info('获取抽卡记录链接', '开始执行')
authkey, state, _ = await get_authkey_by_stoken(event.user_id, uid)
if not state:
return authkey
if authkey == {}:
await gacha_url.finish(authkey, at_sender=True)
now = time()
region = 'cn_qd01' if uid[0] == '5' else 'cn_gf01'
msgs = []
url = (
f"https://hk4e-api.mihoyo.com/event/gacha_info/api/getGachaLog?"
f"authkey_ver=1&sign_type=2&auth_appid=webview_gacha&init_type=301&"
f"gacha_id=fecafa7b6560db5f3182222395d88aaa6aaac1bc"
f"&timestamp={str(int(now))}"
f"&lang=zh-cn&device_type=mobile&plat_type=ios&region={region}"
f"&authkey={quote(authkey,'utf-8')}"
f"&game_biz=hk4e_cn&gacha_type=301&page=1&size=5&end_id=0"
)
msgs.append({"type": "node", "data": {"name": '抽卡记录',
"uin": event.user_id, "content": f"uid:{uid}的抽卡记录链接为:"}})
msgs.append({"type": "node", "data": {"name": '抽卡记录',
"uin": event.user_id, "content": url}})
try:
if isinstance(event, PrivateMessageEvent):
msg = await bot.call_api(
"send_private_forward_msg", user_id=event.user_id, messages=msgs
)
else:
msg = await bot.call_api(
"send_group_forward_msg", group_id=event.group_id, messages=msgs
)
scheduler.add_job(
withdraw,
"date",
args=[bot, dict(msg)["message_id"]],
run_date=datetime.fromtimestamp(time() + 90),
)
except ActionFailed:
logger.info('获取抽卡记录链接', '', '', '发送失败', False)
except Exception as e:
logger.info('获取抽卡记录链接', '', e, '撤回任务异常!', False)