From 1f1053f1b2b81a37c936b5ef4d8485bd43331332 Mon Sep 17 00:00:00 2001 From: duan602728596 Date: Thu, 1 Aug 2024 13:49:54 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=88=A0=E9=99=A4icqq=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/qqtools/src/QQ/QQBotModals/Basic.ts | 2 +- .../qqtools/src/QQ/QQBotModals/GoCQHttp.ts | 4 +- .../qqtools/src/QQ/QQBotModals/ModalTypes.ts | 4 +- packages/qqtools/src/QQ/QQBotModals/OicqQQ.ts | 179 ------------------ .../expand/weibo/weibo.worker/weibo.worker.ts | 2 +- .../weiboSuperTopic.worker.ts | 2 +- .../qqtools/src/QQ/function/formatConfig.ts | 2 - .../qqtools/src/QQ/function/parser/index.ts | 7 +- packages/qqtools/src/commonTypes.d.ts | 4 +- packages/qqtools/src/pages/Login/index.tsx | 10 +- .../pages/Options/EditV2/editv2.schema.json | 4 - packages/qqtools/src/services/oicq/index.ts | 29 --- .../qqtools/src/services/oicq/interface.d.ts | 0 13 files changed, 10 insertions(+), 239 deletions(-) delete mode 100644 packages/qqtools/src/QQ/QQBotModals/OicqQQ.ts delete mode 100644 packages/qqtools/src/services/oicq/index.ts delete mode 100644 packages/qqtools/src/services/oicq/interface.d.ts diff --git a/packages/qqtools/src/QQ/QQBotModals/Basic.ts b/packages/qqtools/src/QQ/QQBotModals/Basic.ts index 298204b7..54756096 100644 --- a/packages/qqtools/src/QQ/QQBotModals/Basic.ts +++ b/packages/qqtools/src/QQ/QQBotModals/Basic.ts @@ -44,7 +44,7 @@ interface KeyType { } abstract class Basic { - public protocol: QQProtocol; // mirai或者oicq + public protocol: QQProtocol; // 使用的机器人程序 public id: string; // 当前进程的唯一ID public config: OptionsItemValueV2; // 配置 public groupNumbers: Array; // 多个群号 diff --git a/packages/qqtools/src/QQ/QQBotModals/GoCQHttp.ts b/packages/qqtools/src/QQ/QQBotModals/GoCQHttp.ts index 3dbec133..4adf14f6 100644 --- a/packages/qqtools/src/QQ/QQBotModals/GoCQHttp.ts +++ b/packages/qqtools/src/QQ/QQBotModals/GoCQHttp.ts @@ -1,7 +1,7 @@ import type { IncomingMessage } from 'node:http'; import { setInterval } from 'node:timers'; import { WebSocket as WebSocketClient, WebSocketServer } from 'ws'; -import type { GroupMessage, MemberIncreaseEvent as OicqMemberIncreaseEvent } from 'icqq'; +import type { GroupMessage, MemberIncreaseEvent as IcqqMemberIncreaseEvent } from 'icqq'; import * as dayjs from 'dayjs'; import { renderString } from 'nunjucks'; import Basic, { type BasicImplement, type BasicArgs } from './Basic'; @@ -22,7 +22,7 @@ export interface HeartbeatMessage { meta_event_type: 'heartbeat'; } -export interface MemberIncreaseEvent extends Omit { +export interface MemberIncreaseEvent extends Omit { notice_type: 'group_increase'; sub_type: 'approve' | 'invite'; } diff --git a/packages/qqtools/src/QQ/QQBotModals/ModalTypes.ts b/packages/qqtools/src/QQ/QQBotModals/ModalTypes.ts index 665eb134..846dcee3 100644 --- a/packages/qqtools/src/QQ/QQBotModals/ModalTypes.ts +++ b/packages/qqtools/src/QQ/QQBotModals/ModalTypes.ts @@ -1,14 +1,12 @@ import type MiraiQQ from './MiraiQQ'; -import type OicqQQ from './OicqQQ'; import type GoCQHttp from './GoCQHttp'; import type ConsoleTest from './ConsoleTest'; /* 支持的qq机器人类型 */ export const enum QQProtocol { Mirai = 'mirai', - Oicq = 'oicq', GoCQHttp = 'go-cqhttp', ConsoleTest = 'console-test' } -export type QQModals = MiraiQQ | OicqQQ | GoCQHttp | ConsoleTest; \ No newline at end of file +export type QQModals = MiraiQQ | GoCQHttp | ConsoleTest; \ No newline at end of file diff --git a/packages/qqtools/src/QQ/QQBotModals/OicqQQ.ts b/packages/qqtools/src/QQ/QQBotModals/OicqQQ.ts deleted file mode 100644 index 03de302e..00000000 --- a/packages/qqtools/src/QQ/QQBotModals/OicqQQ.ts +++ /dev/null @@ -1,179 +0,0 @@ -import type { GroupMessage, MemberIncreaseEvent, Sendable } from 'icqq'; -import * as dayjs from 'dayjs'; -import { renderString } from 'nunjucks'; -import { requestSendGroupMessage } from '@qqtools-api/oicq'; -import Basic, { type BasicImplement, type MessageListener, type BasicArgs } from './Basic'; -import { QQProtocol } from './ModalTypes'; -import { - getGroupNumbers, - getSocketHost, - LogCommandData, - isGroupMessageEventData, - isMemberIncreaseEventData -} from '../function/qq/qqUtils'; -import parser, { type ParserResult } from '../function/parser/index'; -import * as CQ from '../function/parser/CQ'; -import type { OptionsItemValueV2, EditItem } from '../../commonTypes'; - -/* oicq的连接 */ -class OicqQQ extends Basic implements BasicImplement { - public protocol: QQProtocol = QQProtocol.Oicq; - public oicqSocket?: WebSocket; - - constructor(args: BasicArgs) { - super(args); - - const { id, config, membersList }: BasicArgs = args; - - this.id = id; // 当前登陆的唯一id - this.config = config; // 配置 - this.membersList = membersList; - this.groupNumbers = getGroupNumbers(this.config.groupNumber); - this.socketHost = getSocketHost(config.socketHost); - } - - // message事件监听 - handleMessageSocketMessage: MessageListener = async (event: MessageEvent): Promise => { - const { qqNumber, customCmd, groupWelcome, groupWelcomeSend }: OptionsItemValueV2 = this.config; - const groupNumbers: Array = this.groupNumbers; - const data: GroupMessage | MemberIncreaseEvent = JSON.parse(event.data); - - if (isGroupMessageEventData(data) && data.sender.user_id !== qqNumber && groupNumbers.includes(data.group_id)) { - const { raw_message: command, /* 当前命令 */ group_id: groupId /* 收到消息的群 */ }: GroupMessage = data; - - // 日志信息输出 - if (command === 'log') { - this.logCommandCallback(groupId); - - return; - } - - // 自定义信息处理 - if (customCmd?.length) { - const index: number = customCmd.findIndex((o: EditItem): boolean => { - if (o.isRegexp) { - return new RegExp(o.cmd, 'i').test(command); - } else { - return o.cmd === command; - } - }); - - if (index >= 0) { - await this.sendMessageText(customCmd[index].value, groupId); - } - } - - // mock - if (process.env.NODE_ENV === 'development') { - (await import('../function/mock/mock')).default(this, command, qqNumber, groupId); - } - } - - if (isMemberIncreaseEventData(data) && data.user_id !== qqNumber && groupNumbers.includes(data.group_id)) { - if (groupWelcome && groupWelcomeSend) { - const msg: string = renderString(groupWelcomeSend, { - at: CQ.at(data.user_id) - }); - - await this.sendMessageText(msg, data.group_id); - } - } - }; - - // websocket初始化 - initWebSocket(): void { - const { socketHost }: this = this; - const { socketPort }: OptionsItemValueV2 = this.config; - - this.oicqSocket = new WebSocket(`ws://${ socketHost }:${ socketPort }/oicq/ws`); - this.oicqSocket.addEventListener('message', this.handleMessageSocketMessage, false); - } - - // websocket销毁 - destroyWebsocket(): void { - if (this.oicqSocket) { - this.oicqSocket.removeEventListener('message', this.handleMessageSocketMessage); - this.oicqSocket.close(); - this.oicqSocket = undefined; - } - } - - /** - * 发送信息 - * @param { Sendable } value - 要发送的信息 - * @param { number } [groupId] - 单个群的群号 - */ - async sendMessage(value: Sendable, groupId?: number): Promise { - try { - const { socketHost }: this = this; - const { socketPort }: OptionsItemValueV2 = this.config; - const groupNumbers: Array = this.groupNumbers; - const sendValue: ParserResult | Sendable = typeof value === 'string' ? parser({ - text: value, - protocol: this.protocol - }) : value; - - if (typeof groupId === 'number') { - // 只发送到一个群 - await requestSendGroupMessage(groupId, socketHost, socketPort, sendValue); - } else { - // 发送到多个群 - await Promise.all( - groupNumbers.map((item: number, index: number): Promise => { - return requestSendGroupMessage(item, socketHost, socketPort, sendValue); - }) - ); - } - } catch (err) { - console.error(err); - } - } - - async sendMessageText(value: string, groupId?: number): Promise { - const sendValue: ParserResult = parser({ - text: value, - protocol: this.protocol - }); - - await this.sendMessage(sendValue as Sendable, groupId); - } - - // 日志回调函数 - async logCommandCallback(groupId: number): Promise { - const { qqNumber }: OptionsItemValueV2 = this.config; - const msg: string = LogCommandData(this.protocol, qqNumber, this.startTime); - - await this.sendMessageText(msg, groupId); - } - - // 项目初始化 - async init(): Promise { - try { - this.initWebSocket(); - await Basic.initExpand.call(this); - this.startTime = dayjs().format('YYYY-MM-DD HH:mm:ss'); - - return true; - } catch (err) { - console.error(err); - - return false; - } - } - - // 项目销毁 - destroy(): boolean { - try { - this.destroyWebsocket(); - Basic.destroyExpand.call(this); - - return true; - } catch (err) { - console.error(err); - - return false; - } - } -} - -export default OicqQQ; \ No newline at end of file diff --git a/packages/qqtools/src/QQ/function/expand/weibo/weibo.worker/weibo.worker.ts b/packages/qqtools/src/QQ/function/expand/weibo/weibo.worker/weibo.worker.ts index a4b40667..32fd095c 100644 --- a/packages/qqtools/src/QQ/function/expand/weibo/weibo.worker/weibo.worker.ts +++ b/packages/qqtools/src/QQ/function/expand/weibo/weibo.worker/weibo.worker.ts @@ -7,7 +7,7 @@ let lfid: string; // 账号的lfid let weiboTimer: number; // 轮询定时器 let weiboAtAll: boolean | undefined; // 是否at全体成员 let weiboId: bigint | null = null; // 记录查询位置 -let protocol: QQProtocol; // 协议:mirai或者oicq +let protocol: QQProtocol; // 使用的机器人程序 let port: number; // 端口号 /** diff --git a/packages/qqtools/src/QQ/function/expand/weibo/weiboSuperTopic.worker/weiboSuperTopic.worker.ts b/packages/qqtools/src/QQ/function/expand/weibo/weiboSuperTopic.worker/weiboSuperTopic.worker.ts index ffe24481..10e57383 100644 --- a/packages/qqtools/src/QQ/function/expand/weibo/weiboSuperTopic.worker/weiboSuperTopic.worker.ts +++ b/packages/qqtools/src/QQ/function/expand/weibo/weiboSuperTopic.worker/weiboSuperTopic.worker.ts @@ -13,7 +13,7 @@ let lfid: `${ string }_-_sort_time`; // 账号的lfid let weiboTimer: number; // 轮询定时器 let weiboId: bigint | null = null; // 记录查询位置 let superNick: string; // 超话名称 -let protocol: QQProtocol; // 协议:mirai或者oicq +let protocol: QQProtocol; // 协议:使用的机器人程序 let port: number; // 端口号 /** diff --git a/packages/qqtools/src/QQ/function/formatConfig.ts b/packages/qqtools/src/QQ/function/formatConfig.ts index bee18352..3f9e36df 100644 --- a/packages/qqtools/src/QQ/function/formatConfig.ts +++ b/packages/qqtools/src/QQ/function/formatConfig.ts @@ -73,8 +73,6 @@ export function formatOptionType(config: OptionsItemValueV2): OptionsItemValueV2 if (nextConfig.optionType === 0 || nextConfig.optionType === '0') { nextConfig.optionType = QQProtocol.Mirai; - } else if (nextConfig.optionType === '1') { - nextConfig.optionType = QQProtocol.Oicq; } else if (nextConfig.optionType === '2') { nextConfig.optionType = QQProtocol.GoCQHttp; } else if (nextConfig.optionType === '100') { diff --git a/packages/qqtools/src/QQ/function/parser/index.ts b/packages/qqtools/src/QQ/function/parser/index.ts index 601866eb..8bd51b62 100644 --- a/packages/qqtools/src/QQ/function/parser/index.ts +++ b/packages/qqtools/src/QQ/function/parser/index.ts @@ -24,14 +24,9 @@ function parser(args: ParserArgs): ParserResult { return text; } - // oicq解析 const message: Array = segment.fromCqcode(text); - if (protocol === QQProtocol.Oicq) { - return message; - } - - // mirai根据oicq的解析来处理 + // mirai根据icqq的解析来处理 const miraiMessageGroup: Array = message.map((ele: MessageElem): MiraiMT | undefined => { switch (ele.type) { case 'text': diff --git a/packages/qqtools/src/commonTypes.d.ts b/packages/qqtools/src/commonTypes.d.ts index 9d216918..e800c2e6 100644 --- a/packages/qqtools/src/commonTypes.d.ts +++ b/packages/qqtools/src/commonTypes.d.ts @@ -10,11 +10,11 @@ export interface EditItem { /* ========== 一些基础的配置 ========== */ /** @deprecated */ -type OldOptionType = '0' | '1' | '2' | '100' | 0; +type OldOptionType = '0' | '2' | '100' | 0; export interface OptionsItemBasic { optionName: string; - optionType: QQProtocol | OldOptionType; // 0: mirai-api-http,1: oicq,2: go-cqhttp + optionType: QQProtocol | OldOptionType; // 0: mirai-api-http,2: go-cqhttp qqNumber: number; groupNumber: string; socketHost?: string; diff --git a/packages/qqtools/src/pages/Login/index.tsx b/packages/qqtools/src/pages/Login/index.tsx index 9a5a65d8..a2a1a55f 100644 --- a/packages/qqtools/src/pages/Login/index.tsx +++ b/packages/qqtools/src/pages/Login/index.tsx @@ -21,7 +21,6 @@ import { queryOptionsList, OptionsInitialState } from '../Options/reducers/optio import { setAddLogin, setDeleteLogin, getRoomId, LoginInitialState } from './reducers/reducers'; import dbConfig from '../../utils/IDB/IDBConfig'; import MiraiQQ from '../../QQ/QQBotModals/MiraiQQ'; -import OicqQQ from '../../QQ/QQBotModals/OicqQQ'; import GoCQHttp from '../../QQ/QQBotModals/GoCQHttp'; import ConsoleTest from '../../QQ/QQBotModals/ConsoleTest'; import { formatToV2Config, formatOptionType } from '../../QQ/function/formatConfig'; @@ -76,14 +75,7 @@ function Index(props: {}): ReactElement { const id: string = randomUUID(); let qq: QQModals | null = null; - if (qqOptions.optionType === QQProtocol.Oicq) { - qq = new OicqQQ({ - id, - config: qqOptions, - membersList: roomIdResult?.value, - messageApi - }); - } else if (qqOptions.optionType === QQProtocol.GoCQHttp) { + if (qqOptions.optionType === QQProtocol.GoCQHttp) { qq = new GoCQHttp({ id, config: qqOptions, diff --git a/packages/qqtools/src/pages/Options/EditV2/editv2.schema.json b/packages/qqtools/src/pages/Options/EditV2/editv2.schema.json index e6e07ca9..3cb29423 100644 --- a/packages/qqtools/src/pages/Options/EditV2/editv2.schema.json +++ b/packages/qqtools/src/pages/Options/EditV2/editv2.schema.json @@ -33,10 +33,6 @@ "label": "go-cqhttp", "value": "go-cqhttp" }, - { - "label": "oicq", - "value": "oicq" - }, { "label": "console-test", "value": "console-test" diff --git a/packages/qqtools/src/services/oicq/index.ts b/packages/qqtools/src/services/oicq/index.ts deleted file mode 100644 index 25ebabad..00000000 --- a/packages/qqtools/src/services/oicq/index.ts +++ /dev/null @@ -1,29 +0,0 @@ -import type { Sendable } from 'icqq'; -import type { ParserResult } from '../../QQ/function/parser'; - -/** - * 发送群消息 - * @param { number } groupNumber - 群号 - * @param { string } socketHost - * @param { number } port - 端口号 - * @param { string } message - 发送信息 - */ -export async function requestSendGroupMessage( - groupNumber: number, - socketHost: string, - port: number, - message: ParserResult | Sendable -): Promise { - const res: Response = await fetch(`http://${ socketHost }:${ port }/oicq/action`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - type: 'sendGroupMsg', - payload: [groupNumber, message] - }) - }); - - return await res.json(); -} \ No newline at end of file diff --git a/packages/qqtools/src/services/oicq/interface.d.ts b/packages/qqtools/src/services/oicq/interface.d.ts deleted file mode 100644 index e69de29b..00000000