diff --git a/src/common/utils.ts b/src/common/utils.ts index b34b1f2..af781e8 100644 --- a/src/common/utils.ts +++ b/src/common/utils.ts @@ -193,4 +193,8 @@ export async function encodeSilk(filePath: string) { log("convert silk failed", error.stack); return {}; } +} + +export function isNull(value: any) { + return value === undefined || value === null; } \ No newline at end of file diff --git a/src/onebot11/action/BaseAction.ts b/src/onebot11/action/BaseAction.ts index 373bc45..efadd9b 100644 --- a/src/onebot11/action/BaseAction.ts +++ b/src/onebot11/action/BaseAction.ts @@ -23,7 +23,7 @@ class BaseAction { } } - public async websocketHandle(payload: PayloadType, echo: string): Promise> { + public async websocketHandle(payload: PayloadType, echo: any): Promise> { const result = await this.check(payload) if (!result.valid) { return OB11Response.error(result.message, 1400) diff --git a/src/onebot11/action/utils.ts b/src/onebot11/action/utils.ts index b7f71d7..3f4e590 100644 --- a/src/onebot11/action/utils.ts +++ b/src/onebot11/action/utils.ts @@ -1,4 +1,5 @@ import {OB11Return} from '../types'; +import {isNull} from '../../common/utils'; export class OB11Response { static res(data: T, status: string, retcode: number, message: string = ""): OB11Return { @@ -8,21 +9,21 @@ export class OB11Response { data: data, message: message, wording: message, - echo: "" + echo: null } } - static ok(data: T, echo: string = "") { + static ok(data: T, echo: any = null) { let res = OB11Response.res(data, "ok", 0) - if (echo) { + if (!isNull(echo)) { res.echo = echo; } return res; } - static error(err: string, retcode: number, echo: string = "") { + static error(err: string, retcode: number, echo: any = null) { let res = OB11Response.res(null, "failed", retcode, err) - if (echo) { + if (!isNull(echo)) { res.echo = echo; } return res; diff --git a/src/onebot11/server/ws/ReverseWebsocket.ts b/src/onebot11/server/ws/ReverseWebsocket.ts index d95a9a4..0a6721e 100644 --- a/src/onebot11/server/ws/ReverseWebsocket.ts +++ b/src/onebot11/server/ws/ReverseWebsocket.ts @@ -33,8 +33,8 @@ export class ReverseWebsocket { } public async onmessage(msg: string) { - let receiveData: { action: ActionName, params: any, echo?: string } = {action: null, params: {}} - let echo = "" + let receiveData: { action: ActionName, params: any, echo?: any } = {action: null, params: {}} + let echo = null try { receiveData = JSON.parse(msg.toString()) echo = receiveData.echo diff --git a/src/onebot11/server/ws/WebsocketServer.ts b/src/onebot11/server/ws/WebsocketServer.ts index 9bfb761..4690265 100644 --- a/src/onebot11/server/ws/WebsocketServer.ts +++ b/src/onebot11/server/ws/WebsocketServer.ts @@ -18,7 +18,7 @@ class OB11WebsocketServer extends WebsocketServerBase { wsClient.send(JSON.stringify(OB11Response.res(null, "failed", 1403, "token验证失败"))) } - async handleAction(wsClient: WebSocket, actionName: string, params: any, echo?: string) { + async handleAction(wsClient: WebSocket, actionName: string, params: any, echo?: any) { const action: BaseAction = actionMap.get(actionName); if (!action) { return wsReply(wsClient, OB11Response.error("不支持的api " + actionName, 1404, echo)) @@ -34,8 +34,8 @@ class OB11WebsocketServer extends WebsocketServerBase { onConnect(wsClient: WebSocket, url: string, req: IncomingMessage) { if (url == "/api" || url == "/api/" || url == "/") { wsClient.on("message", async (msg) => { - let receiveData: { action: ActionName, params: any, echo?: string } = {action: null, params: {}} - let echo = "" + let receiveData: { action: ActionName, params: any, echo?: any } = {action: null, params: {}} + let echo = null try { receiveData = JSON.parse(msg.toString()) echo = receiveData.echo diff --git a/src/onebot11/server/ws/reply.ts b/src/onebot11/server/ws/reply.ts index f4acaca..ce85622 100644 --- a/src/onebot11/server/ws/reply.ts +++ b/src/onebot11/server/ws/reply.ts @@ -1,13 +1,13 @@ import * as websocket from "ws"; import {OB11Response} from "../../action/utils"; import {PostEventType} from "../postevent"; -import {log} from "../../../common/utils"; +import {isNull, log} from "../../../common/utils"; export function wsReply(wsClient: websocket.WebSocket, data: OB11Response | PostEventType) { try { let packet = Object.assign({ }, data); - if (!packet["echo"]){ + if (isNull(packet["echo"])){ delete packet["echo"]; } wsClient.send(JSON.stringify(packet)) diff --git a/src/onebot11/types.ts b/src/onebot11/types.ts index ab23fbd..1919dac 100644 --- a/src/onebot11/types.ts +++ b/src/onebot11/types.ts @@ -77,7 +77,7 @@ export interface OB11Return { retcode: number data: DataType message: string, - echo?: string, // ws调用api才有此字段 + echo?: any, // ws调用api才有此字段 wording?: string, // go-cqhttp字段,错误信息 }