mirror of
https://github.com/Jerryplusy/crystelf-plugin.git
synced 2025-12-05 15:41:56 +00:00
删删删
This commit is contained in:
parent
a0f93ab3cd
commit
51923ee3a2
@ -1,38 +0,0 @@
|
|||||||
import systemControl from '../lib/core/systemControl.js';
|
|
||||||
import tools from '../components/tool.js';
|
|
||||||
import configControl from '../lib/config/configControl.js';
|
|
||||||
|
|
||||||
export default class CoreRestart extends plugin {
|
|
||||||
constructor() {
|
|
||||||
super({
|
|
||||||
name: 'crystelf重启核心',
|
|
||||||
dsc: '实现核心的重启功能',
|
|
||||||
rule: [
|
|
||||||
{
|
|
||||||
reg: '^#core重启$',
|
|
||||||
fnc: 'restart',
|
|
||||||
permission: 'master',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async restart(e) {
|
|
||||||
if (!configControl.get('core')) {
|
|
||||||
return e.reply(`晶灵核心未启用..`, true);
|
|
||||||
}
|
|
||||||
const returnData = await systemControl.systemRestart();
|
|
||||||
if (returnData?.data?.success) {
|
|
||||||
await e.reply(`操作成功:${returnData?.data?.data}..`, true);
|
|
||||||
} else {
|
|
||||||
await e.reply(`操作失败:${returnData?.data?.data}..`, true);
|
|
||||||
}
|
|
||||||
await tools.sleep(8000);
|
|
||||||
const restartTime = await systemControl.getRestartTime();
|
|
||||||
if (restartTime) {
|
|
||||||
await e.reply(`晶灵核心重启成功!耗时${restartTime?.data?.data}秒..`, true);
|
|
||||||
} else {
|
|
||||||
await e.reply(`核心重启花的时间有点久了呢..${restartTime?.data?.data}`, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -2,7 +2,7 @@ import fs from 'node:fs';
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import chokidar from 'chokidar';
|
import chokidar from 'chokidar';
|
||||||
import ConfigControl from '../lib/config/configControl.js';
|
import ConfigControl from '../lib/config/configControl.js';
|
||||||
import Fanqie from '../models/apps/fanqie/fanqie.js';
|
import Fanqie from '../modules/apps/fanqie/fanqie.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 本功能由 y68(github@yeqiu6080) 提供技术支持
|
* 本功能由 y68(github@yeqiu6080) 提供技术支持
|
||||||
|
|||||||
@ -1,69 +0,0 @@
|
|||||||
import botControl from '../lib/core/botControl.js';
|
|
||||||
import configControl from '../lib/config/configControl.js';
|
|
||||||
import schedule from 'node-schedule';
|
|
||||||
import axios from 'axios';
|
|
||||||
|
|
||||||
export default class ReportBots extends plugin {
|
|
||||||
constructor() {
|
|
||||||
super({
|
|
||||||
name: 'crystelf Bot状态上报',
|
|
||||||
dsc: '一些操作bot的功能',
|
|
||||||
rule: [
|
|
||||||
{
|
|
||||||
reg: '^#crystelf同步$',
|
|
||||||
fnc: 'manualReport',
|
|
||||||
permission: 'master',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
reg: '^#crystelf广播(.+)$',
|
|
||||||
fnc: 'broadcast',
|
|
||||||
permission: 'master',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
schedule.scheduleJob('*/30 * * * *', () => this.autoReport());
|
|
||||||
}
|
|
||||||
|
|
||||||
async autoReport() {
|
|
||||||
logger.mark(`正在自动同步bot数据到晶灵核心..`);
|
|
||||||
if (configControl.get('core')) {
|
|
||||||
await botControl.reportBots();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async manualReport(e) {
|
|
||||||
if (!configControl.get('core')) {
|
|
||||||
return e.reply(`晶灵核心未启用..`, true);
|
|
||||||
}
|
|
||||||
let success = await botControl.reportBots();
|
|
||||||
if (success) {
|
|
||||||
await e.reply('crystelf Bot信息已同步到核心..', true);
|
|
||||||
} else {
|
|
||||||
await e.reply('crystelf Bot同步失败:核心未连接..', true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async broadcast(e) {
|
|
||||||
const msg = e?.msg?.match(/^#crystelf广播(.+)$/)?.[1]?.trim();
|
|
||||||
if (!msg) {
|
|
||||||
return e.reply('广播内容不能为空');
|
|
||||||
}
|
|
||||||
await e.reply(`开始广播消息到所有群..`);
|
|
||||||
try {
|
|
||||||
const sendData = {
|
|
||||||
token: configControl.get('coreConfig')?.token,
|
|
||||||
message: msg.toString(),
|
|
||||||
};
|
|
||||||
const url = configControl.get('coreConfig')?.coreUrl;
|
|
||||||
const returnData = await axios.post(`${url}/api/bot/broadcast`, sendData);
|
|
||||||
if (returnData?.data?.success) {
|
|
||||||
return await e.reply(`操作成功:${returnData?.data.data?.toString()}`);
|
|
||||||
} else {
|
|
||||||
return await e.reply(`广播出现错误,请检查日志..`);
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
logger.error(`广播执行异常: ${err.message}`);
|
|
||||||
return await e.reply('广播过程中发生错误,请检查日志..');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,5 +1,5 @@
|
|||||||
import configControl from '../lib/config/configControl.js';
|
import configControl from '../lib/config/configControl.js';
|
||||||
import rssTools from '../models/rss/rss.js';
|
import rssTools from '../modules/rss/rss.js';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import screenshot from '../lib/rss/screenshot.js';
|
import screenshot from '../lib/rss/screenshot.js';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
|||||||
@ -3,10 +3,6 @@
|
|||||||
"core": true,
|
"core": true,
|
||||||
"coreConfig": {
|
"coreConfig": {
|
||||||
"coreUrl": "",
|
"coreUrl": "",
|
||||||
"wsUrl": "",
|
|
||||||
"wsClientId": "",
|
|
||||||
"wsSecret": "",
|
|
||||||
"wsReConnectInterval": "5000",
|
|
||||||
"token": ""
|
"token": ""
|
||||||
},
|
},
|
||||||
"maxFeed": 10,
|
"maxFeed": 10,
|
||||||
|
|||||||
@ -17,12 +17,12 @@ function init() {
|
|||||||
if (!fs.existsSync(configPath)) {
|
if (!fs.existsSync(configPath)) {
|
||||||
fs.mkdirSync(configPath, { recursive: true });
|
fs.mkdirSync(configPath, { recursive: true });
|
||||||
fs.mkdirSync(dataPath, { recursive: true });
|
fs.mkdirSync(dataPath, { recursive: true });
|
||||||
logger.mark(`crystelf 配置文件夹创建成功,位于 ${configPath}..`);
|
logger.mark(`crystelf-plugin 配置文件夹创建成功,位于 ${configPath}..`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fs.existsSync(configFile)) {
|
if (!fs.existsSync(configFile)) {
|
||||||
fs.writeFileSync(configFile, JSON.stringify(defaultConfig, null, 4), 'utf8');
|
fs.writeFileSync(configFile, JSON.stringify(defaultConfig, null, 4), 'utf8');
|
||||||
logger.mark('crystelf 配置文件创建成功..');
|
logger.mark('crystelf-plugin 配置文件创建成功..');
|
||||||
} else {
|
} else {
|
||||||
const cfgFile = fs.readFileSync(configFile, 'utf8');
|
const cfgFile = fs.readFileSync(configFile, 'utf8');
|
||||||
const loadedConfig = JSON.parse(cfgFile);
|
const loadedConfig = JSON.parse(cfgFile);
|
||||||
@ -30,7 +30,7 @@ function init() {
|
|||||||
|
|
||||||
if (JSON.stringify(cfg) !== JSON.stringify(loadedConfig)) {
|
if (JSON.stringify(cfg) !== JSON.stringify(loadedConfig)) {
|
||||||
fs.writeFileSync(configFile, JSON.stringify(cfg, null, 4), 'utf8');
|
fs.writeFileSync(configFile, JSON.stringify(cfg, null, 4), 'utf8');
|
||||||
logger.mark('crystelf 配置文件已更新,补充配置项..');
|
logger.mark('crystelf-plugin 配置文件已更新,补充配置项..');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,98 +0,0 @@
|
|||||||
import wsClient from '../../models/ws/wsClient.js';
|
|
||||||
import configControl from '../config/configControl.js';
|
|
||||||
|
|
||||||
const botControl = {
|
|
||||||
/**
|
|
||||||
* 获取全部bot信息并同步到core
|
|
||||||
* @returns {Promise<boolean>}
|
|
||||||
*/
|
|
||||||
async reportBots() {
|
|
||||||
const bots = [{ client: configControl.get('coreConfig').wsClientId }];
|
|
||||||
|
|
||||||
for (const bot of Object.values(Bot)) {
|
|
||||||
if (!bot || !bot.uin) continue;
|
|
||||||
|
|
||||||
const botInfo = {
|
|
||||||
uin: bot.uin,
|
|
||||||
nickName: bot.nickname.replace(/[\u200E-\u200F\u202A-\u202E\u2066-\u2069]/g, ''),
|
|
||||||
groups: [],
|
|
||||||
};
|
|
||||||
|
|
||||||
let groupsMap = bot.gl;
|
|
||||||
if (groupsMap) {
|
|
||||||
for (const [groupId, groupInfo] of groupsMap) {
|
|
||||||
botInfo.groups.push({
|
|
||||||
group_id: groupId,
|
|
||||||
group_name: groupInfo.group_name || '未知',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bots.push(botInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
const message = {
|
|
||||||
type: 'reportBots',
|
|
||||||
data: bots,
|
|
||||||
};
|
|
||||||
|
|
||||||
return await wsClient.sendMessage(message);
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取群聊信息
|
|
||||||
* @param botId
|
|
||||||
* @param groupId
|
|
||||||
* @returns {Promise<*|null>}
|
|
||||||
*/
|
|
||||||
async getGroupInfo(botId, groupId) {
|
|
||||||
const bot = Bot[botId];
|
|
||||||
if (!bot) {
|
|
||||||
logger.warn(`未找到bot: ${botId}`);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const group = bot.pickGroup(groupId);
|
|
||||||
if (!group) {
|
|
||||||
logger.warn(`Bot ${botId}中未找到群${groupId}`);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
return await group.getInfo();
|
|
||||||
} catch (e) {
|
|
||||||
logger.error(`获取群聊信息失败:${groupId}..`);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 发送信息到群
|
|
||||||
* @param botId bot账号
|
|
||||||
* @param message 发送的信息
|
|
||||||
* @param groupId 群号
|
|
||||||
* @returns {Promise<boolean>}
|
|
||||||
*/
|
|
||||||
async sendMessage(botId, message, groupId) {
|
|
||||||
const bot = Bot[botId];
|
|
||||||
if (!bot) {
|
|
||||||
logger.warn(`未找到bot: ${botId}`);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const group = bot.pickGroup(groupId);
|
|
||||||
if (!group) {
|
|
||||||
logger.warn(`Bot ${botId}中未找到群${groupId}`);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
return !!(await group.send(message));
|
|
||||||
} catch (e) {
|
|
||||||
logger.error(`发送群信息失败:${groupId}..`);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
export default botControl;
|
|
||||||
@ -1,20 +0,0 @@
|
|||||||
import configControl from '../config/configControl.js';
|
|
||||||
import axios from 'axios';
|
|
||||||
|
|
||||||
let systemControl = {
|
|
||||||
async systemRestart() {
|
|
||||||
const token = configControl.get('coreConfig')?.token;
|
|
||||||
const coreUrl = configControl.get('coreConfig')?.coreUrl;
|
|
||||||
const postUrl = coreUrl + '/api/system/restart';
|
|
||||||
//logger.info(returnData);
|
|
||||||
return await axios.post(postUrl, { token: token });
|
|
||||||
},
|
|
||||||
|
|
||||||
async getRestartTime() {
|
|
||||||
const token = configControl.get('coreConfig')?.token;
|
|
||||||
const coreUrl = configControl.get('coreConfig')?.coreUrl;
|
|
||||||
const postUrl = coreUrl + '/api/system/getRestartTime';
|
|
||||||
return axios.post(postUrl, { token: token });
|
|
||||||
},
|
|
||||||
};
|
|
||||||
export default systemControl;
|
|
||||||
@ -1,14 +1,10 @@
|
|||||||
import configControl from '../config/configControl.js';
|
import configControl from '../config/configControl.js';
|
||||||
import wsClient from '../../models/ws/wsClient.js';
|
|
||||||
import rssCache from '../rss/rssCache.js';
|
import rssCache from '../rss/rssCache.js';
|
||||||
|
|
||||||
export const crystelfInit = {
|
export const crystelfInit = {
|
||||||
async CSH() {
|
async CSH() {
|
||||||
await configControl.init();
|
await configControl.init();
|
||||||
await rssCache.init();
|
await rssCache.init();
|
||||||
if (configControl.get('core')) {
|
logger.mark('crystelf-plugin 完成初始化');
|
||||||
await wsClient.initialize();
|
|
||||||
}
|
|
||||||
logger.mark('crystelf 完成初始化');
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,95 +0,0 @@
|
|||||||
import botControl from '../../lib/core/botControl.js';
|
|
||||||
import wsClient from './wsClient.js';
|
|
||||||
|
|
||||||
class Handler {
|
|
||||||
constructor() {
|
|
||||||
this.handlers = new Map([
|
|
||||||
['auth', this.handleAuth.bind(this)],
|
|
||||||
['ping', this.handlePing.bind(this)],
|
|
||||||
['message', this.handleMessageFromServer.bind(this)],
|
|
||||||
['error', this.handleError.bind(this)],
|
|
||||||
['getGroupInfo', this.handleGetGroupInfo.bind(this)],
|
|
||||||
['sendMessage', this.handleSendMessage.bind(this)],
|
|
||||||
['reportBots', this.reportBots.bind(this)],
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
async handle(client, msg) {
|
|
||||||
const handler = this.handlers.get(msg.type);
|
|
||||||
if (handler) {
|
|
||||||
await handler(client, msg);
|
|
||||||
} else {
|
|
||||||
logger.warn(`未知消息类型: ${msg.type}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async handleAuth(client, msg) {
|
|
||||||
if (msg.success) {
|
|
||||||
logger.mark('crystelf WS 认证成功..');
|
|
||||||
} else {
|
|
||||||
logger.error('crystelf WS 认证失败,关闭连接..');
|
|
||||||
client.ws.close(4001, '认证失败');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async handlePing(client, msg) {
|
|
||||||
await client.sendMessage({ type: 'pong' });
|
|
||||||
}
|
|
||||||
|
|
||||||
async handleMessageFromServer(client, msg) {
|
|
||||||
logger.mark(`crystelf 服务端消息: ${msg.data}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
async handleError(client, msg) {
|
|
||||||
logger.warn(`crystelf WS 错误:${msg.data}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
获取群聊信息,自动回调
|
|
||||||
@examples 请求示例
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
requestId: 114514,
|
|
||||||
type: 'getGroupInfo',
|
|
||||||
data: {
|
|
||||||
botId: 114514,
|
|
||||||
groupId: 114514,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
```
|
|
||||||
**/
|
|
||||||
async handleGetGroupInfo(client, msg) {
|
|
||||||
const requestId = msg?.requestId;
|
|
||||||
const botId = msg.data?.botId;
|
|
||||||
const groupId = msg.data?.groupId;
|
|
||||||
const type = msg.type + 'Return';
|
|
||||||
const groupData = await botControl.getGroupInfo(botId, groupId);
|
|
||||||
const returnData = {
|
|
||||||
type: type,
|
|
||||||
requestId: requestId,
|
|
||||||
data: groupData,
|
|
||||||
};
|
|
||||||
await wsClient.sendMessage(returnData);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 发送信息到群聊
|
|
||||||
* @param client
|
|
||||||
* @param msg
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
// TODO 测试可用性
|
|
||||||
async handleSendMessage(client, msg) {
|
|
||||||
const botId = Number(msg.data?.botId);
|
|
||||||
const groupId = Number(msg.data?.groupId);
|
|
||||||
const message = msg.data?.message?.toString();
|
|
||||||
await botControl.sendMessage(botId, message, groupId);
|
|
||||||
}
|
|
||||||
|
|
||||||
async reportBots(client, msg) {
|
|
||||||
await botControl.reportBots();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const handler = new Handler();
|
|
||||||
export default handler;
|
|
||||||
@ -1,95 +0,0 @@
|
|||||||
import WebSocket from 'ws';
|
|
||||||
import configControl from '../../lib/config/configControl.js';
|
|
||||||
import handler from './handler.js';
|
|
||||||
|
|
||||||
class WsClient {
|
|
||||||
constructor() {
|
|
||||||
this.ws = null;
|
|
||||||
this.wsURL = null;
|
|
||||||
this.secret = null;
|
|
||||||
this.clientId = null;
|
|
||||||
this.reconnectInterval = null;
|
|
||||||
this.isReconnecting = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
async initialize() {
|
|
||||||
try {
|
|
||||||
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
||||||
logger.mark('crystelf WS 客户端已连接..');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.wsURL = configControl.get('coreConfig')?.wsUrl;
|
|
||||||
this.secret = configControl.get('coreConfig')?.wsSecret;
|
|
||||||
this.clientId = configControl.get('coreConfig')?.wsClientId;
|
|
||||||
this.reconnectInterval = configControl.get('coreConfig')?.wsReConnectInterval;
|
|
||||||
|
|
||||||
//logger.info(this.wsURL);
|
|
||||||
this.ws = new WebSocket(this.wsURL);
|
|
||||||
|
|
||||||
this.ws.on('open', () => {
|
|
||||||
logger.mark('crystelf WS 客户端连接成功..');
|
|
||||||
this.authenticate();
|
|
||||||
});
|
|
||||||
|
|
||||||
this.ws.on('message', (raw) => {
|
|
||||||
try {
|
|
||||||
const data = JSON.parse(raw);
|
|
||||||
handler.handle(this, data);
|
|
||||||
} catch (err) {
|
|
||||||
logger.err(err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
this.ws.on('error', (err) => {
|
|
||||||
logger.error('WS 连接错误:', err);
|
|
||||||
});
|
|
||||||
|
|
||||||
this.ws.on('close', (code, reason) => {
|
|
||||||
logger.warn(`crystelf WS 客户端连接断开:${code} - ${reason}`);
|
|
||||||
this.reconnect();
|
|
||||||
});
|
|
||||||
} catch (err) {
|
|
||||||
logger.error(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async authenticate() {
|
|
||||||
const authMsg = {
|
|
||||||
type: 'auth',
|
|
||||||
secret: this.secret,
|
|
||||||
clientId: this.clientId,
|
|
||||||
};
|
|
||||||
await this.sendMessage(authMsg);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 发送信息到ws服务端,自动格式化
|
|
||||||
* @param msg
|
|
||||||
* @returns {Promise<boolean>}
|
|
||||||
*/
|
|
||||||
async sendMessage(msg) {
|
|
||||||
if (this.ws?.readyState === WebSocket.OPEN) {
|
|
||||||
this.ws.send(JSON.stringify(msg));
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
logger.warn('crystelf WS 服务器未连接,无法发送消息..');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async reconnect() {
|
|
||||||
if (this.isReconnecting) return;
|
|
||||||
this.isReconnecting = true;
|
|
||||||
|
|
||||||
logger.mark('crystelf WS 客户端尝试重连..');
|
|
||||||
setTimeout(() => {
|
|
||||||
this.isReconnecting = false;
|
|
||||||
this.initialize();
|
|
||||||
}, this.reconnectInterval);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const wsClient = new WsClient();
|
|
||||||
|
|
||||||
export default wsClient;
|
|
||||||
Loading…
x
Reference in New Issue
Block a user