2
0
mirror of https://github.com/ctrlcvs/xiaoyao-cvs-plugin.git synced 2024-12-23 03:20:52 +08:00
xiaoyao-cvs-plugin/apps/Note.js

368 lines
9.5 KiB
JavaScript
Raw Normal View History

2022-06-23 22:30:41 +08:00
import fetch from "node-fetch";
import fs from "fs";
2022-08-03 21:25:29 +08:00
import {
isV3
} from '../components/Changelog.js'
2022-07-24 11:46:05 +08:00
import gsCfg from '../model/gsCfg.js'
2022-06-23 23:06:30 +08:00
import {
2022-07-04 23:32:26 +08:00
Cfg,
Data
2022-06-23 23:06:30 +08:00
} from "../components/index.js";
import utils from "../model/mys/utils.js";
2022-10-31 13:57:07 +08:00
import note from '../model/note.js'
import User from "../model/user.js";
2022-06-23 22:30:41 +08:00
const _path = process.cwd();
let tempDataUrl = `${_path}/plugins/xiaoyao-cvs-plugin/data/NoteTemp`
2022-07-04 23:32:26 +08:00
let tempData = {};
init()
2022-08-03 21:25:29 +08:00
2022-07-04 23:32:26 +08:00
function init() {
Data.createDir("", tempDataUrl, false);
tempData = Data.readJSON(tempDataUrl, "tempData")
2022-07-04 23:32:26 +08:00
}
2022-06-23 22:30:41 +08:00
//#体力
export async function Note(e, {
render
2022-08-03 21:25:29 +08:00
}, poke) {
2023-01-14 22:49:52 +08:00
if (!Cfg.get("sys.Note") && !poke && !e?.isTask) {
2022-06-23 23:06:30 +08:00
return false;
}
2022-11-08 22:25:42 +08:00
let notes = new note(e);
2022-08-03 21:25:29 +08:00
let cookie, uid, res;
if (isV3) {
2023-01-14 11:08:36 +08:00
if (e.msg.replace(/全|全部/g, '多').includes('多')) {
let ck = await gsCfg.getBingCkSingle(e.user_id)
if (Object.keys(ck).length == 0) {
2022-11-09 22:14:24 +08:00
e.reply(`请先【#绑定cookie】`)
return true;
}
2023-01-14 11:08:36 +08:00
let sendMsg = []
e._reply = e.reply;
e.reply = ((msg) => {
sendMsg.push(msg)
})
2023-01-14 11:08:36 +08:00
if (Object.keys(ck).length > 1) {
e._reply(`多账号体力查询中请稍等...`)
}
2023-01-14 11:08:36 +08:00
for (let item of Object.keys(ck)) {
let res = await (await e.runtime.createMysApi(ck[item].uid, ck[item].ck)).getData('dailyNote')
await notes.getNote(ck[item].ck, ck[item].uid, res, { render })
}
e._reply(sendMsg)
return true;
}
2022-10-12 16:51:51 +08:00
let MysInfo = await import(`file://${_path}/plugins/genshin/model/mys/mysInfo.js`);
res = await MysInfo.default.get(e, 'dailyNote')
2022-07-24 12:12:39 +08:00
if (!res || res.retcode !== 0) return true
2022-08-03 21:25:29 +08:00
uid = e.uid;
} else {
2022-07-24 11:46:05 +08:00
if (NoteCookie[e.user_id]) {
cookie = NoteCookie[e.user_id].cookie;
uid = NoteCookie[e.user_id].uid;
} else if (BotConfig.dailyNote && BotConfig.dailyNote[e.user_id]) {
cookie = BotConfig.dailyNote[e.user_id].cookie;
uid = BotConfig.dailyNote[e.user_id].uid;
2022-06-23 22:30:41 +08:00
} else {
2022-07-24 11:46:05 +08:00
e.reply(`尚未配置,无法查询体力\n配置教程:${BotConfig.cookieDoc}`);
return true;
2022-06-23 22:30:41 +08:00
}
2022-07-24 11:46:05 +08:00
const response = await getDailyNote(uid, cookie);
if (!response.ok) {
e.reply("米游社接口错误");
return true;
}
res = await response.json();
if (res.retcode == 10102) {
if (!e.openDailyNote) {
e.openDailyNote = true;
await openDailyNote(cookie); //自动开启
dailyNote(e);
} else {
e.reply("请先开启实时便笺数据展示");
2022-06-23 22:30:41 +08:00
}
2022-07-24 11:46:05 +08:00
return true;
}
if (res.retcode != 0) {
if (res.message == "Please login") {
Bot.logger.mark(`体力cookie已失效`);
e.reply(`体力cookie已失效请重新配置\n注意退出米游社登录cookie将会失效`);
if (NoteCookie[e.user_id]) {
// await MysUser.delNote(NoteCookie[e.user_id]);
delete NoteCookie[e.user_id];
saveJson();
}
} else {
e.reply(`体力查询错误:${res.message}`);
Bot.logger.mark(`体力查询错误:${JSON.stringify(res)}`);
}
2023-01-14 11:08:36 +08:00
2022-07-24 11:46:05 +08:00
return true;
}
2023-01-14 11:08:36 +08:00
2022-07-24 11:46:05 +08:00
//redis保存uid
redis.set(`genshin:uid:${e.user_id}`, uid, {
EX: 2592000
});
2023-01-14 11:08:36 +08:00
2022-07-24 11:46:05 +08:00
//更新
if (NoteCookie[e.user_id]) {
NoteCookie[e.user_id].maxTime = new Date().getTime() + res.data.resin_recovery_time * 1000;
saveJson();
2022-06-23 22:30:41 +08:00
}
}
2023-01-14 11:08:36 +08:00
return await notes.getNote(cookie, uid, res, { render })
2022-06-23 22:30:41 +08:00
}
async function getDailyNote(uid, cookie) {
2022-10-12 21:16:44 +08:00
let mysApi = (await import(`file://${_path}/lib/app/mysApi.js`))
2022-06-23 22:30:41 +08:00
let {
url,
headers,
query,
body
2022-07-24 11:46:05 +08:00
} = mysApi.getUrl("dailyNote", uid);
2022-06-23 22:30:41 +08:00
headers.Cookie = cookie;
const response = await fetch(url, {
2022-10-31 13:57:07 +08:00
method: "get",
headers
2022-06-23 22:30:41 +08:00
});
return response;
}
export async function saveJson() {
let path = "data/NoteCookie/NoteCookie.json";
fs.writeFileSync(path, JSON.stringify(NoteCookie, "", "\t"));
}
2022-10-31 13:57:07 +08:00
export async function noteTask(e) {
if (e.isPrivate) {
return true;
}
let notes = new note(e);
let user = new User(e)
let {
cookie
} = await user.getCookie(e)
e.isbool = e.msg.includes("开启")
e.isgl = e.msg.includes("群")
if (!cookie && e.isbool && !e.isgl) {
e.reply("请先#绑定ck\n发送【体力帮助】获取教程")
return false;
}
if (e.isgl) {
notes.updNote(e)
} else if (e.isbool && e.msg.includes("体力推送")) {
notes.addNote()
} else {
notes.delNote()
}
return true;
}
//体力定时推送
export async function DailyNoteTask() {
//推送cd12小时一次
let sendCD = 12 * 3600;
2022-10-31 13:57:07 +08:00
let notes = new note();
for (let item in notes.noteCfg) {
let group = notes.noteCfg[item]
if (!group?.isTask) continue;
let taskUser = group.task;
for (let i of taskUser) {
let e = {
user_id: i,
qq: i,
msg: "体力",
sendResin: group.sendResin,
isTask: true,
}
//今天已经提醒
let sendkey = `xiaoyao:dailyNote:send:${i}`;
let send = await redis.get(sendkey);
if (!Bot.pickGroup(item).pickMember(i) || send) continue;
let sendMsg = [segment.at(i * 1), "哥哥(姐姐)你的体力快满了哦~"]
e.reply = (msg) => {
sendMsg.push(msg)
};
let render;
2022-10-12 16:51:51 +08:00
if (isV3) {
let {
getRender
2022-10-31 13:57:07 +08:00
} = await import(`file://${_path}/plugins/xiaoyao-cvs-plugin/adapter/render.js`);
render = await getRender()
2022-10-12 16:51:51 +08:00
} else {
let {
getPluginRender
} = await import(`file://${_path}/lib/render.js`);
2022-10-31 13:57:07 +08:00
render = await getPluginRender()
}
let task = await Note(e, {
render
});
2022-11-09 23:34:00 +08:00
if (!task) {
2022-10-31 13:57:07 +08:00
redis.set(sendkey, "1", {
EX: sendCD
2022-10-12 16:51:51 +08:00
});
2022-10-31 13:57:07 +08:00
Bot.pickGroup(item).sendMsg(sendMsg)
}
}
}
}
2022-07-04 23:32:26 +08:00
2022-10-12 16:51:51 +08:00
export async function pokeNote(e, {
render
}) {
2022-07-07 10:52:17 +08:00
if (!Cfg.get("note.poke")) {
return false;
}
2022-10-12 16:51:51 +08:00
return await Note(e, {
render
}, "poke");
2022-07-06 14:14:50 +08:00
}
2022-07-04 23:32:26 +08:00
export async function Note_appoint(e) {
2022-08-03 21:25:29 +08:00
let mbPath = `${_path}/plugins/xiaoyao-cvs-plugin/resources/dailyNote/`;
2023-01-14 11:08:36 +08:00
let isDel = e.msg.includes("移除")
2022-10-31 22:56:22 +08:00
let msg = e.msg.replace(/#|井|体力|模板|设置|移除/g, "");
let All = ["默认", "随机", "0"];
2022-07-04 23:32:26 +08:00
let urlType = note_file();
2022-10-31 13:57:07 +08:00
let keyType = Object.keys(urlType);
2022-08-03 21:25:29 +08:00
if (!isNaN(msg) && msg != 0) {
2022-10-31 13:57:07 +08:00
if (msg > keyType.length) {
2022-08-03 21:25:29 +08:00
e.reply(`没有${msg}的索引序号哦~`)
return true;
}
2022-10-31 13:57:07 +08:00
msg = keyType[msg - 1];
2022-08-03 21:25:29 +08:00
}
2022-07-04 23:32:26 +08:00
let type = 0;
if (msg.includes("列表")) {
2022-10-31 15:58:43 +08:00
let isUser = msg.includes('我的')
let temp = tempData[e.user_id]?.temp;
2023-01-14 11:08:36 +08:00
if ((!temp || temp?.length === 0) && isUser) {
2022-10-31 14:14:45 +08:00
e.reply("未获取到您设置的模板信息哦~")
return true;
}
2022-10-31 13:57:07 +08:00
let xlmsg = msg.replace(/列表|我的/g, "") * 1 || 1
2022-10-31 15:58:43 +08:00
let listLength = isUser ? temp.length : keyType.length
2022-10-31 13:57:07 +08:00
let sumCount = (listLength / 80 + 0.49).toFixed(0);
2022-10-12 16:51:51 +08:00
xlmsg = sumCount - xlmsg > -1 ? xlmsg : sumCount == 0 ? 1 : sumCount;
let xxmsg = (xlmsg - 1) <= 0 ? 0 : 80 * (xlmsg - 1)
let count = 0;
2022-10-31 13:57:07 +08:00
let msgData = [`模板列表共,第${xlmsg}页,共${listLength}张,\n您可通过【#体力模板设置1】来绑定你需要的体力模板~\n请选择序号~~\n当前支持选择的模板有:`];
for (let [index, item] of keyType.entries()) {
2022-08-03 21:25:29 +08:00
let msg_pass = [];
let imgurl;
2022-10-31 22:56:22 +08:00
let pathFile = urlType[item].replace(/\./, _path)
if (item.includes(".")) {
2023-04-14 21:28:35 +08:00
imgurl = await segment.image(`file://${pathFile}`);
item = item.split(".")[0];
} else {
imgurl = await segment.image(
2023-04-14 21:28:35 +08:00
`file://${pathFile}/icon/bg/${fs.readdirSync(`${pathFile}/icon/bg/`)[0]}`
)
}
if (isUser && !temp?.includes(item)) {
2022-10-31 13:57:07 +08:00
continue;
}
2022-10-12 16:51:51 +08:00
item = index + 1 + "." + item
2022-08-04 23:13:05 +08:00
count++;
2022-10-12 16:51:51 +08:00
if (msgData.length == 81) {
break;
}
2022-10-12 16:51:51 +08:00
if (index < xxmsg) {
continue;
}
2022-08-03 21:25:29 +08:00
msg_pass.push(item)
if (imgurl) {
msg_pass.push(imgurl)
}
msgData.push(msg_pass)
}
2022-10-12 16:51:51 +08:00
let endMsg = "";
2022-10-31 13:57:07 +08:00
if (count < listLength) {
2022-10-12 16:51:51 +08:00
endMsg = `更多内容请翻页查看\n如:#体力模板列表2`
} else {
endMsg = `已经到底了~~`
}
msgData.push(endMsg)
await utils.replyMake(e, msgData, 0)
2022-07-04 23:32:26 +08:00
return true;
}
2022-10-31 13:57:07 +08:00
if (keyType.includes(msg + ".png")) {
2022-10-12 16:51:51 +08:00
msg = msg + ".png";
2022-08-03 21:25:29 +08:00
}
2022-10-31 13:57:07 +08:00
if (!keyType.includes(msg) && !All.includes(msg)) {
2022-07-04 23:32:26 +08:00
e.reply("没有找到你想要的模板昵!可输入 【#体力模板列表】 查询当前支持的模板哦~~")
return true;
} else if (All.includes(msg)) {
type = -1;
2022-08-03 21:25:29 +08:00
} else {
type = 1
if (msg.includes(".")) {
type = 0
}
}
2022-10-31 13:57:07 +08:00
let temp = [];
2022-10-31 15:58:43 +08:00
if (!tempData[e.user_id]) {
2022-10-31 13:57:07 +08:00
tempData[e.user_id] = {
temp: [],
type: type,
}
}
if (typeof tempData[e.user_id]["temp"] === "string") {
temp = [tempData[e.user_id]["temp"], msg]
} else {
2023-01-14 11:08:36 +08:00
temp = tempData[e.user_id]["temp"]
if (!tempData[e.user_id]["temp"]?.includes(msg)) {
2022-10-31 22:56:22 +08:00
temp.push(msg)
}
}
2023-01-14 11:08:36 +08:00
let sendMsg = "诶~这是你选的模板吗,模板设置成功了快用指令来试试吧~"
if (isDel) {
if (temp.includes(msg)) {
temp.splice(temp.indexOf(msg), 1)
sendMsg = `模板${msg}已移除~`
2022-10-31 13:57:07 +08:00
}
}
tempData[e.user_id] = {
2022-10-31 13:57:07 +08:00
temp: temp,
type: type,
}
fs.writeFileSync(tempDataUrl + "/tempData.json", JSON.stringify(tempData));
2022-07-04 23:32:26 +08:00
init()
2022-10-31 22:56:22 +08:00
e.reply(sendMsg)
2022-07-04 23:32:26 +08:00
return true;
}
2023-01-14 11:08:36 +08:00
const note_file = function (xiaoyao) {
2022-10-31 13:57:07 +08:00
let url1 = `./plugins/xiaoyao-cvs-plugin/resources/dailyNote/Template/`
let url2 = `./plugins/xiaoyao-cvs-plugin/resources/BJT-Templet/` //冤种情况。。
let url3 = `./plugins/xiaoyao-cvs-plugin/resources/dailyNote/background_image/`
var urlFile = fs.readdirSync(url1);
var urlType = {};
2022-10-31 15:58:43 +08:00
for (let val of urlFile) {
if (val.includes(".")) continue;
urlType[val] = url1 + val
}
if (fs.existsSync(url2)) {
var bJTurlFile = fs.readdirSync(url2);
for (let val of bJTurlFile) {
if (!val.includes("Template")) continue;
let file = fs.readdirSync(`${url2}${val}`);
for (let va of file) {
if (va.includes(".")) continue;
urlType[va] = url2 + val + "/" + va
2022-10-31 14:14:45 +08:00
}
2022-10-31 13:57:07 +08:00
}
2022-10-31 15:58:43 +08:00
}
2022-10-12 16:51:51 +08:00
if (!xiaoyao) {
2022-10-31 13:57:07 +08:00
var urlFileOne = fs.readdirSync(url3);
2022-08-03 22:56:01 +08:00
for (let val of urlFileOne) {
if (!val.includes(".")) continue;
2022-10-31 15:58:43 +08:00
urlType[val] = url3 + val
2022-08-03 22:56:01 +08:00
}
2022-08-03 21:25:29 +08:00
}
2022-07-04 23:32:26 +08:00
return urlType;
}