2022-07-24 11:46:05 +08:00
|
|
|
import YAML from 'yaml'
|
|
|
|
import chokidar from 'chokidar'
|
|
|
|
import fs from 'node:fs'
|
2022-07-29 01:42:35 +08:00
|
|
|
import {
|
|
|
|
promisify
|
|
|
|
} from 'node:util'
|
2022-07-24 11:46:05 +08:00
|
|
|
import lodash from 'lodash'
|
2022-08-19 17:39:28 +08:00
|
|
|
import {
|
|
|
|
Data
|
|
|
|
} from "../components/index.js";
|
2022-08-29 21:18:09 +08:00
|
|
|
|
|
|
|
import {
|
|
|
|
isV3
|
|
|
|
} from '../components/Changelog.js';
|
2022-09-05 11:49:06 +08:00
|
|
|
import utils from './mys/utils.js';
|
2022-07-29 01:42:35 +08:00
|
|
|
const plugin = "xiaoyao-cvs-plugin"
|
2022-08-29 21:18:09 +08:00
|
|
|
const pathPlugin=`./plugins/${plugin}/data/`
|
2022-09-09 10:24:10 +08:00
|
|
|
/**
|
2022-08-29 21:18:09 +08:00
|
|
|
* 配置文件
|
|
|
|
* 主要用于处理 stoken以及云原神账号数据
|
|
|
|
*/
|
2022-10-13 11:35:24 +08:00
|
|
|
const _path = process.cwd();
|
2022-07-24 11:46:05 +08:00
|
|
|
class GsCfg {
|
2022-07-29 01:42:35 +08:00
|
|
|
constructor() {
|
2022-09-09 10:24:10 +08:00
|
|
|
|
2022-08-21 22:40:37 +08:00
|
|
|
}
|
2022-07-31 12:32:03 +08:00
|
|
|
/** 通用yaml读取*/
|
2022-08-19 17:39:28 +08:00
|
|
|
getfileYaml(path, name) {
|
2022-10-31 13:57:07 +08:00
|
|
|
this.cpCfg('config', 'config')
|
2022-07-29 22:43:28 +08:00
|
|
|
return YAML.parse(
|
2022-08-19 17:39:28 +08:00
|
|
|
fs.readFileSync(path + name + ".yaml", 'utf8')
|
2022-07-29 22:43:28 +08:00
|
|
|
)
|
|
|
|
}
|
2022-10-24 08:16:27 +08:00
|
|
|
cpCfg (app, name) {
|
|
|
|
if (!fs.existsSync(`./plugins/${plugin}/config`)) {
|
|
|
|
fs.mkdirSync(`./plugins/${plugin}/config`)
|
|
|
|
}
|
|
|
|
|
|
|
|
let set = `./plugins/${plugin}/config/${name}.yaml`
|
|
|
|
if (!fs.existsSync(set)) {
|
|
|
|
fs.copyFileSync(`./plugins/${plugin}/defSet/${app}/${name}.yaml`, set)
|
|
|
|
}
|
|
|
|
}
|
2022-10-12 16:51:51 +08:00
|
|
|
async getMasterQQ(){
|
|
|
|
let qq;
|
|
|
|
if(isV3){
|
2022-10-13 11:35:24 +08:00
|
|
|
let config=(await import(`file://${_path}/lib/config/config.js`)).default
|
2022-10-12 16:51:51 +08:00
|
|
|
qq=config.masterQQ[0]
|
|
|
|
}else{
|
|
|
|
qq=BotConfig.masterQQ[0]
|
|
|
|
}
|
|
|
|
return qq
|
|
|
|
}
|
2022-07-29 01:42:35 +08:00
|
|
|
/** 读取用户绑定的ck */
|
|
|
|
async getBingCk() {
|
|
|
|
let ck = {}
|
|
|
|
let ckQQ = {}
|
|
|
|
let dir = './data/MysCookie/'
|
|
|
|
let files = fs.readdirSync(dir).filter(file => file.endsWith('.yaml'))
|
2022-09-09 10:24:10 +08:00
|
|
|
|
2022-07-29 01:42:35 +08:00
|
|
|
const readFile = promisify(fs.readFile)
|
2022-09-09 10:24:10 +08:00
|
|
|
|
2022-07-29 01:42:35 +08:00
|
|
|
let promises = []
|
2022-09-09 10:24:10 +08:00
|
|
|
|
2022-07-29 01:42:35 +08:00
|
|
|
files.forEach((v) => promises.push(readFile(`${dir}${v}`, 'utf8')))
|
2022-09-09 10:24:10 +08:00
|
|
|
|
2022-07-29 01:42:35 +08:00
|
|
|
const res = await Promise.all(promises)
|
2022-09-09 10:24:10 +08:00
|
|
|
|
2022-07-29 01:42:35 +08:00
|
|
|
res.forEach((v) => {
|
|
|
|
let tmp = YAML.parse(v)
|
|
|
|
lodash.forEach(tmp, (v, i) => {
|
|
|
|
ck[String(i)] = v
|
|
|
|
if (v.isMain && !ckQQ[String(v.qq)]) {
|
|
|
|
ckQQ[String(v.qq)] = v
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2022-09-09 10:24:10 +08:00
|
|
|
|
2022-07-29 01:42:35 +08:00
|
|
|
return {
|
|
|
|
ck,
|
|
|
|
ckQQ
|
|
|
|
}
|
|
|
|
}
|
2022-10-31 13:57:07 +08:00
|
|
|
|
|
|
|
/** 读取所有用户绑定的ck */
|
|
|
|
async getBingAllCk () {
|
|
|
|
let ck = {}
|
|
|
|
let ckQQ = {}
|
|
|
|
let qqCk={}
|
|
|
|
let dir = './data/MysCookie/'
|
|
|
|
let files = fs.readdirSync(dir).filter(file => file.endsWith('.yaml'))
|
|
|
|
|
|
|
|
const readFile = promisify(fs.readFile)
|
|
|
|
|
|
|
|
let promises = []
|
|
|
|
|
|
|
|
files.forEach((v) => promises.push(readFile(`${dir}${v}`, 'utf8')))
|
|
|
|
|
|
|
|
const res = await Promise.all(promises)
|
|
|
|
|
|
|
|
res.forEach((v) => {
|
|
|
|
let tmp = YAML.parse(v)
|
|
|
|
let qq
|
|
|
|
lodash.forEach(tmp, (item, uid) => {
|
|
|
|
qq = item.qq
|
|
|
|
ck[String(uid)] = item
|
|
|
|
if(!qqCk[String(item.qq)]) qqCk[String(item.qq)]=[]
|
|
|
|
qqCk[String(item.qq)].push(item)
|
|
|
|
if (item.isMain && !ckQQ[String(item.qq)]) {
|
|
|
|
ckQQ[String(item.qq)] = item
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (qq && !ckQQ[String(qq)]) {
|
|
|
|
ckQQ[String(qq)] = Object.values(tmp)[0]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return { ck, ckQQ,qqCk }
|
|
|
|
}
|
|
|
|
|
2022-09-09 10:24:10 +08:00
|
|
|
async getUserStoken(userId){
|
2022-09-09 10:17:33 +08:00
|
|
|
try {
|
2022-09-09 10:24:10 +08:00
|
|
|
let ck=YAML.parse(
|
2022-09-09 10:17:33 +08:00
|
|
|
fs.readFileSync(`plugins/${plugin}/data/yaml/${userId}.yaml`, 'utf8')
|
|
|
|
)
|
2022-09-09 10:24:10 +08:00
|
|
|
return ck||{}
|
2022-09-09 10:17:33 +08:00
|
|
|
}catch (ex) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
}
|
2022-08-19 17:39:28 +08:00
|
|
|
/** 读取所有用户绑定的stoken */
|
2022-08-01 23:13:34 +08:00
|
|
|
async getBingStoken() {
|
|
|
|
let ck = []
|
|
|
|
let ckQQ = {}
|
|
|
|
let dir = `plugins/${plugin}/data/yaml/`
|
|
|
|
let files = fs.readdirSync(dir).filter(file => file.endsWith('.yaml'))
|
2022-09-09 10:24:10 +08:00
|
|
|
|
2022-08-01 23:13:34 +08:00
|
|
|
const readFile = promisify(fs.readFile)
|
2022-09-09 10:24:10 +08:00
|
|
|
|
2022-08-01 23:13:34 +08:00
|
|
|
let promises = []
|
2022-09-09 10:24:10 +08:00
|
|
|
|
2022-08-01 23:13:34 +08:00
|
|
|
files.forEach((v) => promises.push(readFile(`${dir}${v}`, 'utf8')))
|
|
|
|
const res = await Promise.all(promises)
|
2022-08-19 17:39:28 +08:00
|
|
|
res.forEach((v, index) => {
|
2022-08-01 23:13:34 +08:00
|
|
|
let tmp = YAML.parse(v)
|
|
|
|
ck.push(tmp)
|
|
|
|
})
|
|
|
|
return ck
|
|
|
|
}
|
2022-07-29 01:42:35 +08:00
|
|
|
getBingCkSingle(userId) {
|
|
|
|
let file = `./data/MysCookie/${userId}.yaml`
|
|
|
|
try {
|
|
|
|
let ck = fs.readFileSync(file, 'utf-8')
|
|
|
|
ck = YAML.parse(ck)
|
|
|
|
return ck
|
|
|
|
} catch (error) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
getBingCookie(userId) {
|
|
|
|
let file = `./data/MysCookie/${userId}.yaml`
|
|
|
|
try {
|
|
|
|
let ck = fs.readFileSync(file, 'utf-8')
|
|
|
|
ck = YAML.parse(ck)
|
2022-08-19 17:39:28 +08:00
|
|
|
for (let item in ck) {
|
2022-08-18 23:15:49 +08:00
|
|
|
let login_ticket;
|
2022-08-19 17:39:28 +08:00
|
|
|
if (!ck[item].isMain) {
|
2022-08-18 23:15:49 +08:00
|
|
|
continue;
|
|
|
|
}
|
2022-08-19 17:39:28 +08:00
|
|
|
login_ticket = ck[item]?.login_ticket
|
|
|
|
ck = ck[item].ck
|
|
|
|
return {
|
|
|
|
ck,
|
|
|
|
item,
|
|
|
|
login_ticket
|
|
|
|
};
|
2022-07-29 01:42:35 +08:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
}
|
2022-08-19 17:39:28 +08:00
|
|
|
saveBingStoken(userId, data) {
|
2022-08-18 23:15:49 +08:00
|
|
|
let file = `./plugins/${plugin}/data/yaml/${userId}.yaml`
|
|
|
|
if (lodash.isEmpty(data)) {
|
|
|
|
fs.existsSync(file) && fs.unlinkSync(file)
|
|
|
|
} else {
|
2022-09-09 10:24:10 +08:00
|
|
|
fs.exists(file, (exists) => {
|
2022-08-19 17:39:28 +08:00
|
|
|
if (!exists) {
|
|
|
|
fs.writeFileSync(file, "", 'utf8')
|
|
|
|
}
|
|
|
|
let ck = fs.readFileSync(file, 'utf-8')
|
|
|
|
let yaml = YAML.stringify(data)
|
|
|
|
ck = YAML.parse(ck)
|
2022-09-02 08:08:23 +08:00
|
|
|
if (ck?.uid||!ck) {
|
2022-08-19 17:39:28 +08:00
|
|
|
fs.writeFileSync(file, yaml, 'utf8')
|
|
|
|
} else {
|
2022-09-01 22:52:29 +08:00
|
|
|
if(!ck[Object.keys(data)[0]]){
|
|
|
|
ck = YAML.stringify(ck)
|
|
|
|
fs.writeFileSync(file, yaml + ck, 'utf8')
|
|
|
|
}
|
2022-08-19 17:39:28 +08:00
|
|
|
}
|
|
|
|
})
|
2022-08-18 23:15:49 +08:00
|
|
|
}
|
|
|
|
}
|
2022-07-24 11:46:05 +08:00
|
|
|
}
|
|
|
|
|
2022-08-29 21:18:09 +08:00
|
|
|
|
2022-07-24 11:46:05 +08:00
|
|
|
export default new GsCfg()
|