ZZZ-Plugin/lib/plugin.js
2024-07-13 19:53:35 +08:00

82 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import MysZZZApi from './mysapi.js';
import { getCk } from './common.js';
import _ from 'lodash';
import NoteUser from '../../genshin/model/mys/NoteUser.js';
export class ZZZPlugin extends plugin {
/**
*
* @returns {Promise<string>}
*/
async getUID() {
const user = this.e;
// if (this.e.at) {
// user = this.e.at;
// }
this.User = await NoteUser.create(user);
// let uid = this.e.msg.match(/\d+/)?.[0];
const uid = this.User?.getUid('zzz');
if (!uid) {
await this.reply('uid为空米游社查询请先绑定cookie其他查询请携带uid');
return false;
}
return uid;
}
/**
*
* @returns {Promise<{api: MysZZZApi, uid: string, deviceFp: string}>}
*/
async getAPI() {
let uid = await this.getUID();
if (!uid) return false;
const ck = await getCk(this.e);
if (!ck || Object.keys(ck).filter(k => ck[k].ck).length === 0) {
await this.reply('尚未绑定cookie请先绑定cookie');
return false;
}
const api = new MysZZZApi(uid, ck);
let deviceFp = await redis.get(`ZZZ:DEVICE_FP:${uid}`);
if (!deviceFp) {
let sdk = api.getUrl('getFp');
let res = await fetch(sdk.url, {
headers: sdk.headers,
method: 'POST',
body: sdk.body,
});
let fpRes = await res.json();
deviceFp = fpRes?.data?.device_fp;
if (deviceFp) {
await redis.set(`ZZZ:DEVICE_FP:${uid}`, deviceFp, {
EX: 86400 * 7,
});
}
}
return { api, uid, deviceFp };
}
/**
*
* @returns {Promise<boolean | object>}
*/
async getPlayerInfo() {
const { api } = await this.getAPI();
if (!api) return false;
let userData = await api.getFinalData(this.e, 'zzzUser');
if (!userData) return false;
userData = userData?.list[0];
let avatar = this.e.bot.avatar;
// 头像
if (this.e.member?.getAvatarUrl) {
avatar = await this.e.member.getAvatarUrl();
} else if (this.e.friend?.getAvatarUrl) {
avatar = await this.e.friend.getAvatarUrl();
}
this.e.playerCard = {
avatar: avatar,
player: userData,
};
return userData;
}
}