Miao-Yunzai/apps/setPubCk.ts

193 lines
4.8 KiB
TypeScript
Raw Normal View History

2024-06-17 23:04:18 +08:00
import { GSCfg as GsCfg } from 'yunzai/mys'
import fs from 'node:fs'
import lodash from 'lodash'
import fetch from 'node-fetch'
import YAML from 'yaml'
import { MysInfo } from 'yunzai/mys'
import { sleep } from 'yunzai/utils'
2024-06-14 10:44:18 +08:00
export class setPubCk extends plugin {
2024-06-17 23:04:18 +08:00
constructor() {
2024-06-14 10:44:18 +08:00
/**
*
name: "配置",
dsc: "#配置ck",
*/
super({
priority: 700,
rule: [
{
reg: /^#配置c(oo)?k(ie)?$|^#*配置公共查询c(oo)?k(ie)?$/i,
2024-06-17 23:04:18 +08:00
fnc: 'setPubCk',
permission: 'master'
2024-06-14 10:44:18 +08:00
},
{
2024-06-17 23:04:18 +08:00
reg: '^#使用(全部|用户)ck$',
fnc: 'setUserCk',
permission: 'master'
2024-06-14 10:44:18 +08:00
}
]
})
}
2024-06-17 23:04:18 +08:00
file = './plugins/genshin/config/mys.pubCk.yaml'
2024-06-14 10:44:18 +08:00
/** 配置公共ck */
2024-06-17 23:04:18 +08:00
async setPubCk() {
2024-06-14 10:44:18 +08:00
/** 设置上下文后续接收到内容会执行doRep方法 */
2024-06-17 23:04:18 +08:00
this.setContext('pubCk')
2024-06-14 10:44:18 +08:00
/** 回复 */
2024-06-17 23:04:18 +08:00
await this.reply('请发送米游社cookie......\n配置后该ck将会加入公共查询池')
2024-06-14 10:44:18 +08:00
}
2024-06-17 23:04:18 +08:00
async pubCk() {
2024-06-14 10:44:18 +08:00
let msg = this.e.msg
2024-06-17 23:04:18 +08:00
if (
!(
/(ltoken|ltoken_v2)/.test(this.e.msg) &&
/(ltuid|ltmid_v2|account_mid_v2)/.test(this.e.msg)
)
) {
this.e.reply('cookie错误请发送正确的cookie')
2024-06-14 10:44:18 +08:00
return true
}
2024-06-17 23:04:18 +08:00
this.finish('pubCk')
2024-06-14 10:44:18 +08:00
2024-06-17 23:04:18 +08:00
let ck = msg.replace(/#|"|"/g, '')
2024-06-14 10:44:18 +08:00
let param = {}
2024-06-17 23:04:18 +08:00
ck.split(';').forEach(v => {
2024-06-14 10:44:18 +08:00
// cookie_token_v2,ltoken_v2值也可能有=
// let tmp = lodash.trim(v).split("=")
2024-06-17 23:04:18 +08:00
let tmp = lodash.trim(v)
let index = tmp.indexOf('=')
param[tmp.slice(0, index)] = tmp.slice(index + 1)
2024-06-14 10:44:18 +08:00
})
2024-06-17 23:04:18 +08:00
this.ck = ''
2024-06-14 10:44:18 +08:00
lodash.forEach(param, (v, k) => {
2024-06-17 23:04:18 +08:00
if (
[
'ltoken',
'ltuid',
'cookie_token',
'account_id',
'cookie_token_v2',
'account_mid_v2',
'ltmid_v2',
'ltoken_v2'
].includes(k)
) {
2024-06-14 10:44:18 +08:00
this.ck += `${k}=${v};`
}
})
/** 检查ck是否失效 */
2024-06-17 23:04:18 +08:00
if (!(await this.checkCk())) {
logger.mark(`配置公共cookie错误${this.checkMsg || 'cookie错误'}`)
await this.e.reply(`配置公共cookie错误${this.checkMsg || 'cookie错误'}`)
2024-06-14 10:44:18 +08:00
return
}
this.ltuid = param.ltuid
// 判断是否是v2版ck
2024-06-17 23:04:18 +08:00
if (
param.cookie_token_v2 &&
(param.account_mid_v2 || param.ltoken_v2) &&
!/(\d{4,9})/g.test(this.ltuid)
) {
2024-06-14 10:44:18 +08:00
// 获取米游社通行证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-06-17 23:04:18 +08:00
logger.mark(
`配置公共cookie错误${userFullInfo.message || 'cookie错误'}`
)
await this.e.reply(
`配置公共cookie错误${userFullInfo.message || 'cookie错误'}`
)
2024-06-14 10:44:18 +08:00
return
}
}
2024-06-17 23:04:18 +08:00
let ckArr = GsCfg.getConfig('mys', 'pubCk') || []
2024-06-14 10:44:18 +08:00
/** 判断是否重复 */
for (let ck of ckArr) {
if (ck.includes(this.ltuid)) {
2024-06-17 23:04:18 +08:00
await this.e.reply('配置公共cookie错误该ck已配置')
2024-06-14 10:44:18 +08:00
return
}
}
ckArr.push(this.ck)
this.save(ckArr)
GsCfg.change_myspubCk()
await this.e.reply(`配置公共ck成功${ckArr.length}`)
}
/** 检查ck是否可用 */
2024-06-17 23:04:18 +08:00
async checkCk() {
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 } })
2024-06-14 10:44:18 +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-06-17 23:04:18 +08:00
async getUserInfo(server = 'mys') {
2024-06-14 10:44:18 +08:00
try {
const that = this
let url = {
2024-06-17 23:04:18 +08:00
mys: 'https://bbs-api.mihoyo.com/user/wapi/getUserFullInfo?gids=2',
hoyolab: ''
2024-06-14 10:44:18 +08:00
}
let res = await fetch(url[server], {
2024-06-17 23:04:18 +08:00
method: 'get',
2024-06-14 10:44:18 +08:00
headers: {
Cookie: that.ck,
2024-06-17 23:04:18 +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/'
2024-06-14 10:44:18 +08:00
}
})
if (!res.ok) return res
res = await res.json()
return res
} catch (e) {
return null
}
}
2024-06-17 23:04:18 +08:00
save(data) {
2024-06-14 10:44:18 +08:00
data = YAML.stringify(data)
fs.writeFileSync(this.file, data)
}
2024-06-17 23:04:18 +08:00
async setUserCk() {
let set = './plugins/genshin/config/mys.set.yaml'
2024-06-14 10:44:18 +08:00
2024-06-17 23:04:18 +08:00
let config = fs.readFileSync(set, 'utf8')
config = config.replace(/allowUseCookie: [0-1]/g, 'allowUseCookie: 1')
fs.writeFileSync(set, config, 'utf8')
2024-06-14 10:44:18 +08:00
await sleep(500)
await MysInfo.initCache(true)
2024-06-17 23:04:18 +08:00
await this.reply('开启成功用户ck已加入公共查询ck池')
2024-06-14 10:44:18 +08:00
}
2024-06-17 23:04:18 +08:00
}