Miao-Yunzai/plugins/genshin/apps/setPubCk.js

166 lines
4.7 KiB
JavaScript
Raw Normal View History

2024-01-31 14:52:19 +08:00
import GsCfg from "../model/gsCfg.js"
import fs from "node:fs"
import lodash from "lodash"
import fetch from "node-fetch"
import YAML from "yaml"
import MysInfo from "../model/mys/mysInfo.js"
import common from "../../../lib/common/common.js"
2023-03-04 14:30:13 +08:00
export class setPubCk extends plugin {
constructor (e) {
super({
2024-01-31 14:52:19 +08:00
name: "配置",
dsc: "#配置ck",
event: "message",
2023-03-04 14:30:13 +08:00
priority: 700,
rule: [
{
2024-02-25 20:39:09 +08:00
reg: /^#配置c(oo)?k(ie)?$|^#*配置公共查询c(oo)?k(ie)?$/i,
2024-01-31 14:52:19 +08:00
fnc: "setPubCk",
permission: "master"
2023-03-04 14:30:13 +08:00
},
{
2024-01-31 14:52:19 +08:00
reg: "^#使用(全部|用户)ck$",
fnc: "setUserCk",
permission: "master"
2023-03-04 14:30:13 +08:00
}
]
})
2024-01-31 14:52:19 +08:00
this.file = "./plugins/genshin/config/mys.pubCk.yaml"
2023-03-04 14:30:13 +08:00
}
/** 配置公共ck */
async setPubCk () {
/** 设置上下文后续接收到内容会执行doRep方法 */
2024-01-31 14:52:19 +08:00
this.setContext("pubCk")
2023-03-04 14:30:13 +08:00
/** 回复 */
2024-01-31 14:52:19 +08:00
await this.reply("请发送米游社cookie......\n配置后该ck将会加入公共查询池")
2023-03-04 14:30:13 +08:00
}
async pubCk () {
let msg = this.e.msg
if (!(/(ltoken|ltoken_v2)/.test(this.e.msg) && /(ltuid|ltmid_v2|account_mid_v2)/.test(this.e.msg))) {
2024-01-31 14:52:19 +08:00
this.e.reply("cookie错误请发送正确的cookie")
2023-03-04 14:30:13 +08:00
return true
}
2024-01-31 14:52:19 +08:00
this.finish("pubCk")
2023-03-04 14:30:13 +08:00
2024-01-31 14:52:19 +08:00
let ck = msg.replace(/#|"|"/g, "")
2023-03-04 14:30:13 +08:00
let param = {}
2024-01-31 14:52:19 +08:00
ck.split(";").forEach((v) => {
// cookie_token_v2,ltoken_v2值也可能有=
2024-01-31 14:52:19 +08:00
// let tmp = lodash.trim(v).split("=")
let tmp = lodash.trim(v);
let index = tmp.indexOf("=");
param[tmp.slice(0,index)] = tmp.slice(index+1);
2023-03-04 14:30:13 +08:00
})
2024-01-31 14:52:19 +08:00
this.ck = ""
2023-03-04 14:30:13 +08:00
lodash.forEach(param, (v, k) => {
2024-01-31 14:52:19 +08:00
if (["ltoken", "ltuid", "cookie_token", "account_id", "cookie_token_v2", "account_mid_v2", "ltmid_v2", "ltoken_v2"].includes(k)) {
2023-03-04 14:30:13 +08:00
this.ck += `${k}=${v};`
}
})
/** 检查ck是否失效 */
if (!await this.checkCk()) {
2024-01-31 14:52:19 +08:00
logger.mark(`配置公共cookie错误${this.checkMsg || "cookie错误"}`)
await this.e.reply(`配置公共cookie错误${this.checkMsg || "cookie错误"}`)
2023-03-04 14:30:13 +08:00
return
}
this.ltuid = param.ltuid
// 判断是否是v2版ck
if (param.cookie_token_v2 && (param.account_mid_v2 || param.ltoken_v2) && !(/(\d{4,9})/g).test(this.ltuid)) {
// 获取米游社通行证id
let userFullInfo = await this.getUserInfo()
if (userFullInfo?.data?.user_info) {
let userInfo = userFullInfo?.data?.user_info
this.ltuid = userInfo.uid
this.ck = `${this.ck}ltuid=${this.ltuid};`
} else {
2024-01-31 14:52:19 +08:00
logger.mark(`配置公共cookie错误${userFullInfo.message || "cookie错误"}`)
await this.e.reply(`配置公共cookie错误${userFullInfo.message || "cookie错误"}`)
2023-03-04 14:30:13 +08:00
return
}
}
2024-01-31 14:52:19 +08:00
let ckArr = GsCfg.getConfig("mys", "pubCk") || []
2023-03-04 14:30:13 +08:00
/** 判断是否重复 */
for (let ck of ckArr) {
if (ck.includes(this.ltuid)) {
2024-01-31 14:52:19 +08:00
await this.e.reply("配置公共cookie错误该ck已配置")
2023-03-04 14:30:13 +08:00
return
}
}
ckArr.push(this.ck)
this.save(ckArr)
GsCfg.change_myspubCk()
await this.e.reply(`配置公共ck成功${ckArr.length}`)
}
/** 检查ck是否可用 */
async checkCk () {
2024-01-31 14:52:19 +08:00
let url = "https://api-takumi.mihoyo.com/binding/api/getUserGameRolesByCookie?game_biz=hk4e_cn"
let res = await fetch(url, { method: "get", headers: { Cookie: this.ck } })
2023-03-04 14:30:13 +08:00
if (!res.ok) return false
res = await res.json()
if (res.retcode != 0) {
this.checkMsg = res.message
return false
}
return true
}
// 获取米游社通行证id
2024-01-31 14:52:19 +08:00
async getUserInfo (server = "mys") {
2023-03-04 14:30:13 +08:00
try {
const that = this
let url = {
2024-01-31 14:52:19 +08:00
mys: "https://bbs-api.mihoyo.com/user/wapi/getUserFullInfo?gids=2",
hoyolab: ""
2023-03-04 14:30:13 +08:00
}
let res = await fetch(url[server], {
2024-01-31 14:52:19 +08:00
method: "get",
2023-03-04 14:30:13 +08:00
headers: {
Cookie: that.ck,
2024-01-31 14:52:19 +08:00
Accept: "application/json, text/plain, */*",
Connection: "keep-alive",
Host: "bbs-api.mihoyo.com",
Origin: "https://m.bbs.mihoyo.com",
Referer: " https://m.bbs.mihoyo.com/"
2023-03-04 14:30:13 +08:00
}
})
if (!res.ok) return res
res = await res.json()
return res
} catch (e) {
return null
}
}
save (data) {
data = YAML.stringify(data)
fs.writeFileSync(this.file, data)
}
async setUserCk () {
2024-01-31 14:52:19 +08:00
let set = "./plugins/genshin/config/mys.set.yaml"
2023-03-04 14:30:13 +08:00
2024-01-31 14:52:19 +08:00
let config = fs.readFileSync(set, "utf8")
config = config.replace(/allowUseCookie: [0-1]/g, "allowUseCookie: 1")
fs.writeFileSync(set, config, "utf8")
2023-03-04 14:30:13 +08:00
await common.sleep(500)
await MysInfo.initCache(true)
2024-01-31 14:52:19 +08:00
await this.reply("开启成功用户ck已加入公共查询ck池")
2023-03-04 14:30:13 +08:00
}
2024-01-31 14:52:19 +08:00
}