2023-02-18 18:46:54 +08:00
import json
from typing import Tuple
2021-12-16 11:16:28 +08:00
from nonebot import on_command
2023-02-18 18:46:54 +08:00
from nonebot . adapters . onebot . v11 import GroupMessageEvent , Message , MessageEvent
from nonebot . params import Command , CommandArg
2021-12-16 11:16:28 +08:00
from services . log import logger
2023-02-18 18:46:54 +08:00
from utils . depends import OneCommand
2022-08-21 13:37:03 +08:00
from utils . http_utils import AsyncHttpx
2023-02-18 18:46:54 +08:00
from utils . utils import is_number
2021-12-16 11:16:28 +08:00
2023-02-18 18:46:54 +08:00
from . . _models import Genshin
2021-12-16 11:16:28 +08:00
__zx_plugin_name__ = " 原神绑定 "
__plugin_usage__ = """
usage :
绑定原神uid等数据 , cookie极为重要 , 请谨慎绑定
* * 如果对拥有者不熟悉 , 并不建议添加cookie * *
该项目只会对cookie用于 ” 米游社签到 “ , “ 原神玩家查询 ” , “ 原神便笺查询 ”
指令 :
原神绑定uid [ uid ]
原神绑定米游社id [ mys_id ]
原神绑定cookie [ cookie ] # 该绑定请私聊
2021-12-26 22:14:01 +08:00
原神解绑
2021-12-16 11:16:28 +08:00
示例 : 原神绑定uid 92342233
如果不明白怎么获取cookie请输入 “ 原神绑定cookie ” 。
""" .strip()
__plugin_des__ = " 绑定自己的原神uid等 "
__plugin_cmd__ = [ " 原神绑定uid [uid] " , " 原神绑定米游社id [mys_id] " , " 原神绑定cookie [cookie] " , " 原神解绑 " ]
__plugin_type__ = ( " 原神相关 " , )
__plugin_version__ = 0.1
__plugin_author__ = " HibiKier "
__plugin_settings__ = {
" level " : 5 ,
" default_status " : True ,
" limit_superuser " : False ,
" cmd " : [ " 原神绑定 " ] ,
}
bind = on_command (
" 原神绑定uid " , aliases = { " 原神绑定米游社id " , " 原神绑定cookie " } , priority = 5 , block = True
)
unbind = on_command ( " 原神解绑 " , priority = 5 , block = True )
2022-08-21 13:37:03 +08:00
web_Api = " https://api-takumi.mihoyo.com "
bbs_Cookie_url = " https://webapi.account.mihoyo.com/Api/cookie_accountinfo_by_loginticket?login_ticket= {} "
2023-02-18 18:46:54 +08:00
bbs_Cookie_url2 = (
web_Api
+ " /auth/api/getMultiTokenByLoginTicket?login_ticket= {} &token_types=3&uid= {} "
)
2022-08-21 13:37:03 +08:00
2021-12-16 11:16:28 +08:00
@bind.handle ( )
2023-02-18 18:46:54 +08:00
async def _ ( event : MessageEvent , cmd : str = OneCommand ( ) , arg : Message = CommandArg ( ) ) :
2022-02-19 18:20:19 +08:00
msg = arg . extract_plain_text ( ) . strip ( )
2023-05-22 20:56:42 +08:00
user = await Genshin . get_or_none ( user_id = str ( event . user_id ) )
2022-02-19 18:20:19 +08:00
if cmd in [ " 原神绑定uid " , " 原神绑定米游社id " ] :
2021-12-16 11:16:28 +08:00
if not is_number ( msg ) :
await bind . finish ( " uid/id必须为纯数字! " , at_senders = True )
msg = int ( msg )
2022-02-19 18:20:19 +08:00
if cmd == " 原神绑定uid " :
2023-02-18 18:46:54 +08:00
if user :
await bind . finish ( f " 您已绑定过uid: { user . uid } , 如果希望更换uid, 请先发送原神解绑" )
2023-05-22 20:56:42 +08:00
if await Genshin . get_or_none ( user_id = str ( event . user_id ) , uid = msg ) :
2021-12-16 11:16:28 +08:00
await bind . finish ( " 添加失败, 该uid可能已存在... " )
2023-05-22 20:56:42 +08:00
user = await Genshin . create ( user_id = str ( event . user_id ) , uid = msg )
2021-12-16 11:16:28 +08:00
_x = f " 已成功添加原神uid: { msg } "
2022-02-19 18:20:19 +08:00
elif cmd == " 原神绑定米游社id " :
2023-02-18 18:46:54 +08:00
if not user :
2021-12-16 11:16:28 +08:00
await bind . finish ( " 请先绑定原神uid.. " )
2023-02-18 18:46:54 +08:00
user . mys_id = int ( msg )
_x = f " 已成功为uid: { user . uid } 设置米游社id: { msg } "
2021-12-16 11:16:28 +08:00
else :
if not msg :
2023-02-18 18:46:54 +08:00
await bind . finish (
""" 私聊发送!!
2022-08-05 22:32:15 +08:00
1. 以无痕模式打开浏览器 ( Edge请新建InPrivate窗口 )
2023-02-18 18:46:54 +08:00
2. 打开http : / / bbs . mihoyo . com / ys / 并登陆
2022-08-05 22:32:15 +08:00
3. 登陆后打开http : / / user . mihoyo . com / 进行登陆
4. 按下F12 , 打开控制台 , 输入以下命令 :
var cookie = document . cookie ; var ask = confirm ( ' Cookie: ' + cookie + ' \\ n \\ nDo you want to copy the cookie to the clipboard? ' ) ; if ( ask == true ) { copy ( cookie ) ; msg = cookie } else { msg = ' Cancel ' }
2023-02-18 18:46:54 +08:00
5. 私聊发送 : 原神绑定cookie 刚刚复制的cookie """
)
2021-12-16 11:16:28 +08:00
if isinstance ( event , GroupMessageEvent ) :
await bind . finish ( " 请立即撤回你的消息并私聊发送! " )
2023-02-18 18:46:54 +08:00
if not user :
2021-12-16 11:16:28 +08:00
await bind . finish ( " 请先绑定原神uid.. " )
2022-02-09 20:05:49 +08:00
if msg . startswith ( ' " ' ) or msg . startswith ( " ' " ) :
2021-12-16 11:16:28 +08:00
msg = msg [ 1 : ]
2022-02-09 20:05:49 +08:00
if msg . endswith ( ' " ' ) or msg . endswith ( " ' " ) :
2021-12-16 11:16:28 +08:00
msg = msg [ : - 1 ]
2022-08-21 13:37:03 +08:00
cookie = msg
# 用: 代替=, ,代替;
2023-02-18 18:46:54 +08:00
cookie = ' { " ' + cookie . replace ( " = " , ' " : " ' ) . replace ( " ; " , ' " , " ' ) + ' " } '
# print(cookie)
2022-08-21 13:37:03 +08:00
cookie_json = json . loads ( cookie )
2023-02-18 18:46:54 +08:00
# print(cookie_json)
if " login_ticket " not in cookie_json :
2022-08-22 20:39:03 +08:00
await bind . finish ( " 请发送正确完整的cookie! " )
2023-02-18 18:46:54 +08:00
user . cookie = str ( msg )
login_ticket = cookie_json [ " login_ticket " ]
2022-08-21 13:37:03 +08:00
# try:
res = await AsyncHttpx . get ( url = bbs_Cookie_url . format ( login_ticket ) )
res . encoding = " utf-8 "
data = json . loads ( res . text )
2023-02-18 18:46:54 +08:00
# print(data)
2022-08-21 13:37:03 +08:00
if " 成功 " in data [ " data " ] [ " msg " ] :
stuid = str ( data [ " data " ] [ " cookie_info " ] [ " account_id " ] )
2023-02-18 18:46:54 +08:00
res = await AsyncHttpx . get ( url = bbs_Cookie_url2 . format ( login_ticket , stuid ) )
2022-08-21 13:37:03 +08:00
res . encoding = " utf-8 "
data = json . loads ( res . text )
stoken = data [ " data " ] [ " list " ] [ 0 ] [ " token " ]
# await Genshin.set_cookie(uid, cookie)
2023-02-18 18:46:54 +08:00
user . stoken = stoken
user . stuid = stuid
user . login_ticket = login_ticket
2022-08-21 13:37:03 +08:00
# except Exception as e:
# await bind.finish("获取登陆信息失败, 请检查cookie是否正确或更新cookie")
elif data [ " data " ] [ " msg " ] == " 登录信息已失效,请重新登录 " :
await bind . finish ( " 登录信息失效, 请重新获取最新cookie进行绑定 " )
2023-02-18 18:46:54 +08:00
_x = f " 已成功为uid: { user . uid } 设置cookie "
2022-02-19 18:20:19 +08:00
if isinstance ( event , GroupMessageEvent ) :
2023-02-18 18:46:54 +08:00
user . bind_group = event . group_id
if user :
await user . save (
update_fields = [
" mys_id " ,
" cookie " ,
" stoken " ,
" stuid " ,
" login_ticket " ,
" bind_group " ,
]
)
2021-12-16 11:16:28 +08:00
await bind . send ( _x )
logger . info (
f " (USER { event . user_id } , "
f " GROUP { event . group_id if isinstance ( event , GroupMessageEvent ) else ' private ' } ) "
2022-02-19 18:20:19 +08:00
f " { cmd } : { msg } "
2021-12-16 11:16:28 +08:00
)
@unbind.handle ( )
2022-02-19 18:20:19 +08:00
async def _ ( event : MessageEvent ) :
2023-05-22 20:56:42 +08:00
await Genshin . filter ( user_id = str ( event . user_id ) ) . delete ( )
2023-02-18 18:46:54 +08:00
await unbind . send ( " 用户数据删除成功... " )
logger . info (
f " (USER { event . user_id } , GROUP "
f " { event . group_id if isinstance ( event , GroupMessageEvent ) else ' private ' } ) "
f " 原神解绑 "
)