From ed681b8adf6a1ace5e78251782ecd1c94d670d3f Mon Sep 17 00:00:00 2001 From: linyuchen Date: Fri, 15 Mar 2024 17:28:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BE=A4=E6=96=87=E4=BB=B6=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E4=BA=8B=E4=BB=B6=20feat:=20=E7=BE=A4=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=8A=E4=BC=A0=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/common/utils.ts | 10 +++-- src/ntqqapi/constructor.ts | 2 +- src/ntqqapi/ntcall.ts | 11 +++++- .../action/go-cqhttp/UploadGroupFile.ts | 37 +++++++++++++++++++ src/onebot11/action/index.ts | 2 + src/onebot11/action/types.ts | 1 + src/onebot11/constructor.ts | 10 ++++- .../notice/OB11GroupUploadNoticeEvent.ts | 19 ++++++++++ src/renderer/index.ts | 2 +- 10 files changed, 86 insertions(+), 10 deletions(-) create mode 100644 src/onebot11/action/go-cqhttp/UploadGroupFile.ts create mode 100644 src/onebot11/event/notice/OB11GroupUploadNoticeEvent.ts diff --git a/package.json b/package.json index 7fff19b..6628ec2 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "build-mac": "npm run build && npm run deploy-mac", "deploy-mac": "cp -r dist/* ~/Library/Containers/com.tencent.qq/Data/LiteLoaderQQNT/plugins/LLOneBot/", "build-win": "npm run build && npm run deploy-win", - "deploy-win": "cmd /c \"xcopy /S /Y dist\\* %USERPROFILE%\\documents\\LiteLoaderQQNT\\plugins\\LLOneBot\\\"" + "deploy-win": "cmd /c \"xcopy /C /S /Y dist\\* %USERPROFILE%\\documents\\LiteLoaderQQNT\\plugins\\LLOneBot\\\"" }, "author": "", "license": "ISC", diff --git a/src/common/utils.ts b/src/common/utils.ts index 9268cf2..81a126d 100644 --- a/src/common/utils.ts +++ b/src/common/utils.ts @@ -206,7 +206,7 @@ export async function encodeSilk(filePath: string) { if (ffmpegPath) { ffmpeg.setFfmpegPath(ffmpegPath); } - ffmpeg(filePath).toFormat("wav").on('end', function () { + ffmpeg(filePath).toFormat("wav").audioChannels(2).on('end', function () { log('wav转换完成'); }) .on('error', function (err) { @@ -221,11 +221,12 @@ export async function encodeSilk(filePath: string) { }) } // const sampleRate = await getAudioSampleRate(filePath) || 0; + // log("音频采样率", sampleRate) const pcm = fs.readFileSync(filePath); const silk = await encode(pcm, 0); fs.writeFileSync(pttPath, silk.data); - fs.unlink(wavPath, (err) => { - }); + // fs.unlink(wavPath, (err) => { + // }); log(`语音文件${filePath}转换成功!`, pttPath) return { converted: true, @@ -260,7 +261,7 @@ export async function encodeSilk(filePath: string) { export async function getVideoInfo(filePath: string) { const size = fs.statSync(filePath).size; return new Promise<{ width: number, height: number, time: number, format: string, size: number, filePath: string }>((resolve, reject) => { - ffmpeg.ffprobe(filePath, (err, metadata) => { + ffmpeg(filePath).ffprobe( (err, metadata) => { if (err) { reject(err); } else { @@ -282,6 +283,7 @@ export async function getVideoInfo(filePath: string) { }) } + export async function encodeMp4(filePath: string) { let videoInfo = await getVideoInfo(filePath); log("视频信息", videoInfo) diff --git a/src/ntqqapi/constructor.ts b/src/ntqqapi/constructor.ts index 28ce031..9efedf8 100644 --- a/src/ntqqapi/constructor.ts +++ b/src/ntqqapi/constructor.ts @@ -121,7 +121,7 @@ export class SendMsgElementConstructor { thumb = pathLib.dirname(thumb) // log("thumb 目录", thumb) const videoInfo = await getVideoInfo(path); - // log("视频信息", videoInfo) + log("视频信息", videoInfo) const createThumb = new Promise((resolve, reject) => { const thumbFileName = `${md5}_0.png` ffmpeg(filePath) diff --git a/src/ntqqapi/ntcall.ts b/src/ntqqapi/ntcall.ts index 65c2f8e..872de10 100644 --- a/src/ntqqapi/ntcall.ts +++ b/src/ntqqapi/ntcall.ts @@ -48,6 +48,7 @@ export enum NTQQApiClass { } export enum NTQQApiMethod { + SET_HEADER = "nodeIKernelProfileService/setHeader", LIKE_FRIEND = "nodeIKernelProfileLikeService/setBuddyProfileLike", SELF_INFO = "fetchAuthData", FRIENDS = "nodeIKernelBuddyService/getBuddyList", @@ -202,7 +203,12 @@ interface GeneralCallResult { export class NTQQApi { - // static likeFriend = defineNTQQApi(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.LIKE_FRIEND) + static async setHeader(path: string){ + return await callNTQQApi({ + methodName: NTQQApiMethod.SET_HEADER, + args: [path] + }) + } static async likeFriend(uid: string, count = 1) { return await callNTQQApi({ methodName: NTQQApiMethod.LIKE_FRIEND, @@ -728,8 +734,9 @@ export class NTQQApi { }) } - static async call(cmdName: string, args: any[],) { + static async call(className: NTQQApiClass, cmdName: string, args: any[],) { return await callNTQQApi({ + className, methodName: cmdName, args: [ ...args, diff --git a/src/onebot11/action/go-cqhttp/UploadGroupFile.ts b/src/onebot11/action/go-cqhttp/UploadGroupFile.ts new file mode 100644 index 0000000..1cd0024 --- /dev/null +++ b/src/onebot11/action/go-cqhttp/UploadGroupFile.ts @@ -0,0 +1,37 @@ +import BaseAction from "../BaseAction"; +import {getGroup} from "../../../common/data"; +import {ActionName} from "../types"; +import {SendMsgElementConstructor} from "../../../ntqqapi/constructor"; +import {ChatType, SendFileElement} from "../../../ntqqapi/types"; +import {NTQQApi} from "../../../ntqqapi/ntcall"; +import {uri2local} from "../../utils"; +import fs from "fs"; + +interface Payload{ + group_id: number + file: string + name: string + folder: string +} + +export default class GoCQHTTPUploadGroupFile extends BaseAction { + actionName = ActionName.GoCQHTTP_UploadGroupFile + + protected async _handle(payload: Payload): Promise { + const group = await getGroup(payload.group_id.toString()); + if (!group){ + throw new Error(`群组${payload.group_id}不存在`) + } + let file = payload.file; + if (fs.existsSync(file)){ + file = `file://${file}` + } + const downloadResult = await uri2local(file); + if (downloadResult.errMsg){ + throw new Error(downloadResult.errMsg) + } + let sendFileEle: SendFileElement = await SendMsgElementConstructor.file(downloadResult.path, payload.name); + await NTQQApi.sendMsg({chatType: ChatType.group, peerUid: group.groupCode}, [sendFileEle]); + return null + } +} \ No newline at end of file diff --git a/src/onebot11/action/index.ts b/src/onebot11/action/index.ts index ac63ef1..11e8f45 100644 --- a/src/onebot11/action/index.ts +++ b/src/onebot11/action/index.ts @@ -32,6 +32,7 @@ import GetImage from "./GetImage"; import GetRecord from "./GetRecord"; import GoCQHTTPMarkMsgAsRead from "./MarkMsgAsRead"; import CleanCache from "./CleanCache"; +import GoCQHTTPUploadGroupFile from "./go-cqhttp/UploadGroupFile"; export const actionHandlers = [ new Debug(), @@ -65,6 +66,7 @@ export const actionHandlers = [ new GoCQHTTPGetStrangerInfo(), new GetGuildList(), new GoCQHTTPMarkMsgAsRead(), + new GoCQHTTPUploadGroupFile(), ] diff --git a/src/onebot11/action/types.ts b/src/onebot11/action/types.ts index 9ab5f2c..fbbb022 100644 --- a/src/onebot11/action/types.ts +++ b/src/onebot11/action/types.ts @@ -49,4 +49,5 @@ export enum ActionName { GoCQHTTP_GetStrangerInfo = "get_stranger_info", GetGuildList = "get_guild_list", GoCQHTTP_MarkMsgAsRead = "mark_msg_as_read", + GoCQHTTP_UploadGroupFile = "upload_group_file", } \ No newline at end of file diff --git a/src/onebot11/constructor.ts b/src/onebot11/constructor.ts index 4ecf2fa..2820272 100644 --- a/src/onebot11/constructor.ts +++ b/src/onebot11/constructor.ts @@ -27,6 +27,8 @@ import {encodeCQCode} from "./cqcode"; import {dbUtil} from "../common/db"; import {OB11GroupIncreaseEvent} from "./event/notice/OB11GroupIncreaseEvent"; import {OB11GroupBanEvent} from "./event/notice/OB11GroupBanEvent"; +import {OB11GroupUploadNoticeEvent} from "./event/notice/OB11GroupUploadNoticeEvent"; +import {OB11GroupNoticeEvent} from "./event/notice/OB11GroupNoticeEvent"; export class OB11Constructor { @@ -244,7 +246,10 @@ export class OB11Constructor { return resMsg; } - static async GroupEvent(msg: RawMessage): Promise { + static async GroupEvent(msg: RawMessage): Promise { + if (msg.chatType !== ChatType.group) { + return; + } for (let element of msg.elements) { const groupElement = element.grayTipElement?.groupElement if (groupElement) { @@ -289,6 +294,9 @@ export class OB11Constructor { } } } + else if (element.fileElement){ + return new OB11GroupUploadNoticeEvent(parseInt(msg.peerUid), parseInt(msg.senderUin), {id: element.fileElement.fileName, name: element.fileElement.fileName, size: parseInt(element.fileElement.fileSize)}) + } } } diff --git a/src/onebot11/event/notice/OB11GroupUploadNoticeEvent.ts b/src/onebot11/event/notice/OB11GroupUploadNoticeEvent.ts new file mode 100644 index 0000000..4aecd66 --- /dev/null +++ b/src/onebot11/event/notice/OB11GroupUploadNoticeEvent.ts @@ -0,0 +1,19 @@ +import {OB11GroupNoticeEvent} from "./OB11GroupNoticeEvent"; + +export interface GroupUploadFile{ + id: string, + name: string, + size: number +} + +export class OB11GroupUploadNoticeEvent extends OB11GroupNoticeEvent { + notice_type = "group_upload" + file: GroupUploadFile + + constructor(groupId: number, userId: number, file: GroupUploadFile) { + super(); + this.group_id = groupId; + this.user_id = userId; + this.file = file + } +} \ No newline at end of file diff --git a/src/renderer/index.ts b/src/renderer/index.ts index ea36c6c..8dfd573 100644 --- a/src/renderer/index.ts +++ b/src/renderer/index.ts @@ -90,7 +90,7 @@ async function onSettingWindowCreated(view: Element) { ], 'ob11.messagePostFormat', config.ob11.messagePostFormat), ), SettingItem( - 'ffmpeg 路径, 用于发送语音时进行转码', `${!isEmpty(config.ffmpeg) ? config.ffmpeg : '未指定'}`, + 'ffmpeg 路径, 发送语音、视频需要,同时保证ffprobe和ffmpeg在一起', `${!isEmpty(config.ffmpeg) ? config.ffmpeg : '未指定'}`, SettingButton('选择', 'config-ffmpeg-select'), ), SettingItem(