Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/arcaea/__init__.py

247 lines
10 KiB
Python
Raw Normal View History

2021-12-06 15:32:27 +00:00
import os
2021-12-22 12:48:02 +00:00
import traceback
2021-12-06 15:32:27 +00:00
2021-12-21 11:13:38 +00:00
from config import Config
2023-02-05 14:33:33 +00:00
from core.builtins import Bot
from core.builtins import Plain, Image
2023-03-04 08:51:56 +00:00
from core.component import module
2023-02-05 14:33:33 +00:00
from core.utils.http import get_url
2022-01-20 12:13:03 +00:00
from .dbutils import ArcBindInfoManager
2021-12-06 15:32:27 +00:00
from .getb30 import getb30
from .getb30_official import getb30_official
2021-12-06 15:32:27 +00:00
from .info import get_info
2022-01-20 03:39:07 +00:00
from .info_official import get_info_official
2021-12-06 15:32:27 +00:00
from .initialize import arcb30init
from .song import get_song_info
from .utils import get_userinfo
2021-12-06 15:32:27 +00:00
2023-03-04 08:51:56 +00:00
arc = module('arcaea', developers=['OasisAkari'], desc='查询Arcaea相关内容。',
alias={'b30': 'arcaea b30', 'a': 'arcaea', 'arc': 'arcaea'})
webrender = Config('web_render')
assets_path = os.path.abspath('./assets/arcaea')
2021-12-06 15:32:27 +00:00
2023-03-04 08:51:56 +00:00
@arc.command('b30 [<friendcode>] {查询一个Arcaea用户的b30列表自动选择使用API}',
'b30 official [<friendcode>] {使用官方API}',
'b30 unofficial [<friendcode>] {使用非官方API}')
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
if not os.path.exists(assets_path):
2023-01-28 05:53:11 +00:00
await msg.finish(
'未找到资源文件请放置一枚arcaea的apk到机器人的assets目录并重命名为arc.apk后使用~arcaea initialize初始化资源。')
query_code = None
prefer_uses = msg.options.get('arc_api', None)
official = msg.parsed_msg.get('official', False)
2022-08-08 11:29:02 +00:00
unofficial = msg.parsed_msg.get('unofficial', False)
if not unofficial and not official and prefer_uses is False:
unofficial = True
if not official and prefer_uses is True:
official = True
2022-08-08 14:55:02 +00:00
friendcode: str = msg.parsed_msg.get('<friendcode>', False)
2022-08-08 11:29:02 +00:00
if friendcode:
2022-01-20 03:39:07 +00:00
if friendcode.isdigit():
if len(friendcode) == 9:
query_code = friendcode
else:
2022-05-21 16:04:29 +00:00
await msg.finish('好友码必须是9位数字')
2022-01-20 03:39:07 +00:00
else:
2022-05-21 16:04:29 +00:00
await msg.finish('请输入正确的好友码!')
else:
get_friendcode_from_db = ArcBindInfoManager(msg).get_bind_friendcode()
if get_friendcode_from_db is not None:
query_code = get_friendcode_from_db
if query_code is not None:
if not unofficial:
try:
resp = await getb30_official(query_code)
msgchain = [Plain(resp['text'])]
if 'file' in resp and msg.Feature.image:
msgchain.append(Image(path=resp['file']))
2022-08-27 14:02:26 +00:00
await msg.sendMessage(msgchain, allow_split_image=False)
except Exception:
traceback.print_exc()
if not official and prefer_uses is None:
await msg.sendMessage('使用官方API获取失败尝试使用非官方接口。')
unofficial = True
else:
await msg.finish('使用官方API获取失败。')
if unofficial:
try:
resp = await getb30(query_code)
msgchain = [Plain(resp['text'])]
if 'file' in resp and msg.Feature.image:
msgchain.append(Image(path=resp['file']))
2022-05-21 16:04:29 +00:00
await msg.finish(msgchain)
except Exception:
2022-05-13 11:10:27 +00:00
traceback.print_exc()
await msg.finish('使用非官方API获取失败。')
else:
2022-05-21 16:04:29 +00:00
await msg.finish('未绑定用户,请使用~arcaea bind <friendcode>绑定一个用户。')
2023-03-04 08:51:56 +00:00
@arc.command('info [<friendcode>] {查询一个Arcaea用户的最近游玩记录}',
'info official [<friendcode>] {使用官方API}',
2023-01-28 05:53:11 +00:00
'info unofficial [<friendcode>] {使用非官方API}', )
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
if not os.path.exists(assets_path):
2023-01-28 05:53:11 +00:00
await msg.sendMessage(
'未找到资源文件请放置一枚arcaea的apk到机器人的assets目录并重命名为arc.apk后使用~arcaea initialize初始化资源。')
return
query_code = None
prefer_uses = msg.options.get('arc_api', None)
2022-08-08 11:29:02 +00:00
unofficial = msg.parsed_msg.get('unofficial', False)
official = msg.parsed_msg.get('official', False)
if not unofficial and not official and prefer_uses is False:
unofficial = True
if not official and prefer_uses is True:
official = True
2022-08-08 14:55:02 +00:00
friendcode = msg.parsed_msg.get('<friendcode>', False)
2022-08-08 11:29:02 +00:00
if friendcode:
2022-01-20 03:39:07 +00:00
if friendcode.isdigit():
if len(friendcode) == 9:
query_code = friendcode
else:
2022-05-21 16:04:29 +00:00
await msg.finish('好友码必须是9位数字')
2022-01-20 03:39:07 +00:00
else:
2022-05-21 16:04:29 +00:00
await msg.finish('请输入正确的好友码!')
else:
get_friendcode_from_db = ArcBindInfoManager(msg).get_bind_friendcode()
if get_friendcode_from_db is not None:
query_code = get_friendcode_from_db
if query_code is not None:
2022-01-20 03:39:07 +00:00
if not unofficial:
try:
resp = await get_info_official(query_code)
if resp['success']:
2022-05-21 16:04:29 +00:00
await msg.finish(resp['msg'])
2022-01-20 03:39:07 +00:00
else:
if not official and prefer_uses is None:
await msg.sendMessage('使用官方API获取失败尝试使用非官方接口。')
unofficial = True
2022-01-20 03:39:07 +00:00
except Exception:
traceback.print_exc()
if not official and prefer_uses is None:
await msg.sendMessage('使用官方API获取失败尝试使用非官方接口。')
unofficial = True
2022-01-20 03:39:07 +00:00
if unofficial:
try:
resp = await get_info(query_code)
2022-05-21 16:04:29 +00:00
await msg.finish(resp)
2022-01-20 03:39:07 +00:00
except Exception:
traceback.print_exc()
await msg.finish('使用非官方API获取失败。')
2021-12-06 15:32:27 +00:00
else:
2022-05-21 16:04:29 +00:00
await msg.finish('未绑定用户,请使用~arcaea bind <friendcode>绑定一个用户。')
2021-12-06 15:32:27 +00:00
2023-03-04 08:51:56 +00:00
@arc.command('song <songname+prs/pst/byd> {查询一首Arcaea谱面的信息}')
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
songname_ = msg.parsed_msg.get('<songname+prs/pst/byd>', False)
songname_split = songname_.split(' ')
diff = -1
for s in songname_split:
s = s.lower()
if s == 'prs':
diff = 0
elif s == 'pst':
diff = 1
elif s == 'ftr':
diff = 2
elif s == 'byd':
diff = 3
if diff != -1:
songname_split.remove(s)
break
if diff == -1:
await msg.finish('请输入正确的谱面难度!')
songname = ' '.join(songname_split)
2022-12-25 07:25:50 +00:00
usercode = None
get_friendcode_from_db = ArcBindInfoManager(msg).get_bind_friendcode()
if get_friendcode_from_db is not None:
usercode = get_friendcode_from_db
await msg.finish(Plain(await get_song_info(songname, diff, usercode)))
2023-03-04 08:51:56 +00:00
@arc.command('bind <friendcode/username> {绑定一个Arcaea用户}')
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
code: str = msg.parsed_msg['<friendcode/username>']
getcode = await get_userinfo(code)
if getcode:
bind = ArcBindInfoManager(msg).set_bind_info(username=getcode[0], friendcode=getcode[1])
if bind:
2022-05-21 16:04:29 +00:00
await msg.finish(f'绑定成功:{getcode[0]}({getcode[1]})')
else:
if code.isdigit():
bind = ArcBindInfoManager(msg).set_bind_info(username='', friendcode=code)
if bind:
2022-05-21 16:04:29 +00:00
await msg.finish('绑定成功,但是无法获取用户信息。请自行检查命令是否可用。')
else:
2022-05-21 16:04:29 +00:00
await msg.finish('绑定失败,请尝试使用好友码绑定。')
2023-03-04 08:51:56 +00:00
@arc.command('unbind {取消绑定用户}')
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
unbind = ArcBindInfoManager(msg).remove_bind_info()
if unbind:
2022-05-21 16:04:29 +00:00
await msg.finish('取消绑定成功。')
2021-12-06 15:32:27 +00:00
2023-03-04 08:51:56 +00:00
@arc.command('initialize', required_superuser=True)
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
2021-12-25 09:32:41 +00:00
assets_apk = os.path.abspath('./assets/arc.apk')
if not os.path.exists(assets_apk):
2022-05-21 16:04:29 +00:00
await msg.finish('未找到arc.apk')
2021-12-25 09:32:41 +00:00
return
result = await arcb30init()
if result:
2022-05-21 16:04:29 +00:00
await msg.finish('成功初始化!')
2021-12-21 11:08:17 +00:00
2023-03-04 08:51:56 +00:00
@arc.command('download {获取最新版本的游戏apk}')
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
2022-06-15 00:29:20 +00:00
if not webrender:
await msg.finish(['未配置webrender无法使用此命令。'])
2022-01-20 12:13:03 +00:00
resp = await get_url(webrender + 'source?url=https://webapi.lowiro.com/webapi/serve/static/bin/arcaea/apk/', 200,
fmt='json')
2021-12-21 11:08:17 +00:00
if resp:
2022-05-21 16:04:29 +00:00
await msg.finish([Plain(f'目前的最新版本为{resp["value"]["version"]}\n下载地址:{resp["value"]["url"]}')])
2023-03-04 08:51:56 +00:00
@arc.command('random {随机一首曲子}')
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
2022-06-15 00:29:20 +00:00
if not webrender:
await msg.finish(['未配置webrender无法使用此命令。'])
resp = await get_url(webrender + 'source?url=https://webapi.lowiro.com/webapi/song/showcase/', 200, fmt='json')
if resp:
value = resp["value"][0]
image = f'{assets_path}/jacket/{value["song_id"]}.jpg'
result = [Plain(value["title"]["en"])]
if os.path.exists(image):
result.append(Image(path=image))
2022-05-21 16:04:29 +00:00
await msg.finish(result)
2023-03-04 08:51:56 +00:00
@arc.command('rank free {查看当前免费包游玩排行}', 'rank paid {查看当前付费包游玩排行}')
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
2022-06-15 00:29:20 +00:00
if not webrender:
await msg.finish(['未配置webrender无法使用此命令。'])
2022-08-08 11:29:02 +00:00
if msg.parsed_msg.get('free', False):
resp = await get_url(webrender + 'source?url=https://webapi.lowiro.com/webapi/song/rank/free/', 200, fmt='json')
else:
resp = await get_url(webrender + 'source?url=https://webapi.lowiro.com/webapi/song/rank/paid/', 200, fmt='json')
if resp:
r = []
rank = 0
for x in resp['value']:
rank += 1
r.append(f'{rank}. {x["title"]["en"]} ({x["status"]})')
2022-05-21 16:04:29 +00:00
await msg.finish('\n'.join(r))
2023-03-04 08:51:56 +00:00
@arc.command('switch {切换查询时默认优先使用的API接口}')
2023-02-05 14:33:33 +00:00
async def _(msg: Bot.MessageSession):
value = msg.options.get('arc_api', True)
set_value = msg.data.edit_option('arc_api', not value)
await msg.finish(f'已切换为{"官方" if not value else "非官方"}API。')