From 1bcdbba29a9505dc2146695688d600c98e03a202 Mon Sep 17 00:00:00 2001 From: idranme Date: Sun, 8 Sep 2024 15:56:05 +0800 Subject: [PATCH] feat --- src/common/utils/file.ts | 27 +++++++++++-- src/main/main.ts | 2 +- src/ntqqapi/api/group.ts | 14 ++++++- src/ntqqapi/api/user.ts | 4 ++ src/ntqqapi/core.ts | 2 +- .../services/NodeIKernelGroupService.ts | 9 +++-- src/ntqqapi/types/group.ts | 12 ++++++ .../action/go-cqhttp/SendGroupNotice.ts | 38 +++++++++++++------ 8 files changed, 85 insertions(+), 23 deletions(-) diff --git a/src/common/utils/file.ts b/src/common/utils/file.ts index 3fa61c6..fa7fd7d 100644 --- a/src/common/utils/file.ts +++ b/src/common/utils/file.ts @@ -4,6 +4,7 @@ import path from 'node:path' import { TEMP_DIR } from '../globalVars' import { randomUUID, createHash } from 'node:crypto' import { fileURLToPath } from 'node:url' +import { fileTypeFromFile } from 'file-type' export function isGIF(path: string) { const buffer = Buffer.alloc(4) @@ -116,7 +117,7 @@ type Uri2LocalRes = { isLocal: boolean } -export async function uri2local(uri: string, filename?: string): Promise { +export async function uri2local(uri: string, filename?: string, needExt?: boolean): Promise { const { type } = checkUriType(uri) if (type === FileUriType.FileURL) { @@ -139,8 +140,14 @@ export async function uri2local(uri: string, filename?: string): Promise void 'nt/group-notify': (input: GroupNotify[]) => void 'nt/friend-request': (input: FriendRequest[]) => void - 'nt/group-member-info-updated': (input: { groupCode: string; members: GroupMember[] }) => void + 'nt/group-member-info-updated': (input: { groupCode: string, members: GroupMember[] }) => void } } diff --git a/src/ntqqapi/services/NodeIKernelGroupService.ts b/src/ntqqapi/services/NodeIKernelGroupService.ts index 72bb096..0f8ef1e 100644 --- a/src/ntqqapi/services/NodeIKernelGroupService.ts +++ b/src/ntqqapi/services/NodeIKernelGroupService.ts @@ -45,7 +45,7 @@ export interface NodeIKernelGroupService { errMsg: string, uids: Map }> - + //26702(其实更早 但是我不知道) checkGroupMemberCache(arrayList: Array): Promise @@ -202,11 +202,12 @@ export interface NodeIKernelGroupService { publishInstructionForNewcomers(groupCode: string, arg: unknown): void - uploadGroupBulletinPic(groupCode: string, pskey: string, imagePath: string): Promise diff --git a/src/ntqqapi/types/group.ts b/src/ntqqapi/types/group.ts index b7d860b..9f93304 100644 --- a/src/ntqqapi/types/group.ts +++ b/src/ntqqapi/types/group.ts @@ -65,3 +65,15 @@ export interface GroupMember { joinTime: string lastSpeakTime: string } + +export interface PublishGroupBulletinReq { + text: string + picInfo?: { + id: string + width: number + height: number + } + oldFeedsId: '' + pinned: number + confirmRequired: number +} \ No newline at end of file diff --git a/src/onebot11/action/go-cqhttp/SendGroupNotice.ts b/src/onebot11/action/go-cqhttp/SendGroupNotice.ts index 1e54a82..9c13e6d 100644 --- a/src/onebot11/action/go-cqhttp/SendGroupNotice.ts +++ b/src/onebot11/action/go-cqhttp/SendGroupNotice.ts @@ -1,5 +1,7 @@ import BaseAction from '../BaseAction' import { ActionName } from '../types' +import { unlink } from 'fs/promises' +import { checkFileReceived, uri2local } from '@/common/utils/file' interface Payload { group_id: number | string @@ -13,24 +15,36 @@ export class SendGroupNotice extends BaseAction { actionName = ActionName.GoCQHTTP_SendGroupNotice async _handle(payload: Payload) { - const type = 1 - const isShowEditCard = 0 - const tipWindowType = 0 + const groupCode = payload.group_id.toString() const pinned = Number(payload.pinned ?? 0) const confirmRequired = Number(payload.confirm_required ?? 1) - const result = await this.ctx.ntWebApi.setGroupNotice({ - groupCode: payload.group_id.toString(), - content: payload.content, + let picInfo: { id: string, width: number, height: number } | undefined + if (payload.image) { + const { path, isLocal, success, errMsg } = await uri2local(payload.image, undefined, true) + if (!success) { + throw new Error(`设置群公告失败, 错误信息: uri2local: ${errMsg}`) + } + await checkFileReceived(path, 5000) // 文件不存在QQ会崩溃,需要提前判断 + const result = await this.ctx.ntGroupApi.uploadGroupBulletinPic(groupCode, path) + if (result.errCode !== 0) { + throw new Error(`设置群公告失败, 错误信息: uploadGroupBulletinPic: ${result.errMsg}`) + } + if (!isLocal) { + unlink(path) + } + picInfo = result.picInfo + } + + const res = await this.ctx.ntGroupApi.publishGroupBulletin(groupCode, { + text: encodeURIComponent(payload.content), + oldFeedsId: '', pinned, - type, - isShowEditCard, - tipWindowType, confirmRequired, - picId: '' + picInfo }) - if (result.ec !== 0) { - throw new Error(`设置群公告失败, 错误信息: ${result.em}`) + if (res.result !== 0) { + throw new Error(`设置群公告失败, 错误信息: ${res.errMsg}`) } return null }