From dc25d837780431cbf45ac7d758434ecf0d7a5550 Mon Sep 17 00:00:00 2001 From: idranme Date: Sun, 8 Sep 2024 20:50:22 +0800 Subject: [PATCH] feat --- package.json | 2 +- src/ntqqapi/api/msg.ts | 4 +++ src/ntqqapi/entities.ts | 15 +++++----- .../action/go-cqhttp/MarkMsgAsRead.ts | 13 +++++++-- .../action/go-cqhttp/SendGroupNotice.ts | 3 ++ src/onebot11/action/go-cqhttp/UploadFile.ts | 28 ++++++------------- 6 files changed, 36 insertions(+), 29 deletions(-) diff --git a/package.json b/package.json index 81a0bbb..cb75938 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "cosmokit": "^1.6.2", "express": "^4.19.2", "fast-xml-parser": "^4.5.0", - "file-type": "^19.4.1", + "file-type": "^19.5.0", "fluent-ffmpeg": "^2.1.3", "minato": "^3.5.1", "silk-wasm": "^3.6.1", diff --git a/src/ntqqapi/api/msg.ts b/src/ntqqapi/api/msg.ts index bd60398..3698ac8 100644 --- a/src/ntqqapi/api/msg.ts +++ b/src/ntqqapi/api/msg.ts @@ -252,4 +252,8 @@ export class NTQQMsgApi extends Service { } }, null]) } + + async setMsgRead(peer: Peer) { + return await invoke('nodeIKernelMsgService/setMsgRead', [{ peer }, null]) + } } diff --git a/src/ntqqapi/entities.ts b/src/ntqqapi/entities.ts index 451a6f3..2dd2d2a 100644 --- a/src/ntqqapi/entities.ts +++ b/src/ntqqapi/entities.ts @@ -100,18 +100,19 @@ export namespace SendElementEntities { } export async function file(ctx: Context, filePath: string, fileName = '', folderId = ''): Promise { - const { fileName: _fileName, path, fileSize } = await ctx.ntFileApi.uploadFile(filePath, ElementType.FILE) - if (fileSize === 0) { - throw '文件异常,大小为 0' + const fileSize = (await stat(filePath)).size.toString() + if (fileSize === '0') { + ctx.logger.warn(`文件${fileName}异常,大小为 0`) + throw new Error('文件异常,大小为 0') } const element: SendFileElement = { elementType: ElementType.FILE, elementId: '', fileElement: { - fileName: fileName || _fileName, - folderId: folderId, - filePath: path!, - fileSize: fileSize.toString(), + fileName, + folderId, + filePath, + fileSize, }, } return element diff --git a/src/onebot11/action/go-cqhttp/MarkMsgAsRead.ts b/src/onebot11/action/go-cqhttp/MarkMsgAsRead.ts index fef4d2d..6fd9490 100644 --- a/src/onebot11/action/go-cqhttp/MarkMsgAsRead.ts +++ b/src/onebot11/action/go-cqhttp/MarkMsgAsRead.ts @@ -1,14 +1,23 @@ import BaseAction from '../BaseAction' import { ActionName } from '../types' +import { MessageUnique } from '@/common/utils/messageUnique' interface Payload { - message_id: number + message_id: number | string } export class MarkMsgAsRead extends BaseAction { actionName = ActionName.GoCQHTTP_MarkMsgAsRead - protected async _handle() { + protected async _handle(payload: Payload) { + if (!payload.message_id) { + throw new Error('参数 message_id 不能为空') + } + const msg = await MessageUnique.getMsgIdAndPeerByShortId(+payload.message_id) + if (!msg) { + throw new Error('msg not found') + } + await this.ctx.ntMsgApi.setMsgRead(msg.Peer) return null } } diff --git a/src/onebot11/action/go-cqhttp/SendGroupNotice.ts b/src/onebot11/action/go-cqhttp/SendGroupNotice.ts index 9c13e6d..9fe8995 100644 --- a/src/onebot11/action/go-cqhttp/SendGroupNotice.ts +++ b/src/onebot11/action/go-cqhttp/SendGroupNotice.ts @@ -15,6 +15,9 @@ export class SendGroupNotice extends BaseAction { actionName = ActionName.GoCQHTTP_SendGroupNotice async _handle(payload: Payload) { + if(!payload.content){ + throw new Error('参数 content 不能为空') + } const groupCode = payload.group_id.toString() const pinned = Number(payload.pinned ?? 0) const confirmRequired = Number(payload.confirm_required ?? 1) diff --git a/src/onebot11/action/go-cqhttp/UploadFile.ts b/src/onebot11/action/go-cqhttp/UploadFile.ts index 3502d2f..e10d598 100644 --- a/src/onebot11/action/go-cqhttp/UploadFile.ts +++ b/src/onebot11/action/go-cqhttp/UploadFile.ts @@ -1,8 +1,6 @@ -import fs from 'node:fs' import BaseAction from '../BaseAction' import { ActionName } from '../types' import { SendElementEntities } from '@/ntqqapi/entities' -import { SendFileElement } from '@/ntqqapi/types' import { uri2local } from '@/common/utils' import { sendMsg, createPeer, CreatePeerMode } from '../../helper/createMessage' @@ -18,15 +16,11 @@ export class UploadGroupFile extends BaseAction { actionName = ActionName.GoCQHTTP_UploadGroupFile protected async _handle(payload: UploadGroupFilePayload): Promise { - let file = payload.file - if (fs.existsSync(file)) { - file = `file://${file}` + const { success, errMsg, path } = await uri2local(payload.file) + if (!success) { + throw new Error(errMsg) } - const downloadResult = await uri2local(file) - if (!downloadResult.success) { - throw new Error(downloadResult.errMsg) - } - const sendFileEle = await SendElementEntities.file(this.ctx, downloadResult.path, payload.name, payload.folder_id) + const sendFileEle = await SendElementEntities.file(this.ctx, path, payload.name, payload.folder_id) const peer = await createPeer(this.ctx, payload, CreatePeerMode.Group) await sendMsg(this.ctx, peer, [sendFileEle], []) return null @@ -43,16 +37,12 @@ export class UploadPrivateFile extends BaseAction { + const { success, errMsg, path } = await uri2local(payload.file) + if (!success) { + throw new Error(errMsg) + } + const sendFileEle = await SendElementEntities.file(this.ctx, path, payload.name) const peer = await createPeer(this.ctx, payload, CreatePeerMode.Private) - let file = payload.file - if (fs.existsSync(file)) { - file = `file://${file}` - } - const downloadResult = await uri2local(file) - if (!downloadResult.success) { - throw new Error(downloadResult.errMsg) - } - const sendFileEle: SendFileElement = await SendElementEntities.file(this.ctx, downloadResult.path, payload.name) await sendMsg(this.ctx, peer, [sendFileEle], []) return null }