From b176e929b9037344c6831bc5ac36255cb9d8cc60 Mon Sep 17 00:00:00 2001 From: H3CoF6 <1707889225@qq.com> Date: Thu, 14 May 2026 04:09:55 +0800 Subject: [PATCH] feat: implement upload group avatar --- .../core/src/bridge/actions/group-admin.ts | 29 +++++++++++++++++++ packages/core/src/bridge/bridge.ts | 2 ++ packages/core/src/bridge/proto/highway.ts | 10 +++++++ .../core/src/onebot/actions/group-admin.ts | 8 +++-- packages/core/src/onebot/api-handler.ts | 1 + packages/core/src/onebot/instance-context.ts | 1 + 6 files changed, 49 insertions(+), 2 deletions(-) diff --git a/packages/core/src/bridge/actions/group-admin.ts b/packages/core/src/bridge/actions/group-admin.ts index fcc27fdc..ec2927ac 100644 --- a/packages/core/src/bridge/actions/group-admin.ts +++ b/packages/core/src/bridge/actions/group-admin.ts @@ -151,3 +151,32 @@ export async function getGroupAtAllRemain( }; } +// ─────────────── group avatar ─────────────── + +export async function setGroupAvatar( + bridge: Bridge, + groupId: number, + source: string, +): Promise { + const { loadBinarySource, computeHashes } = await import('../highway/utils'); + const { fetchHighwaySession, uploadHighwayHttp } = await import('../highway/highway-client'); + const { protoEncode } = await import('../../protobuf/decode'); + const { GroupAvatarUploadExtSchema } = await import('../proto/highway'); + + const loaded = await loadBinarySource(source, 'group avatar'); + if (!loaded.bytes.length) throw new Error('group avatar file is empty'); + + const extend = protoEncode({ + field1: 101, + groupId: BigInt(groupId), + field3: new Uint8Array([0x08, 0x01, 0x12, 0x00]), + field5: 3, + field6: 0, + }, GroupAvatarUploadExtSchema); + + const hashes = computeHashes(loaded.bytes); + const session = await fetchHighwaySession(bridge); + + await uploadHighwayHttp(bridge, session, 3000, loaded.bytes, hashes.md5, extend); +} + diff --git a/packages/core/src/bridge/bridge.ts b/packages/core/src/bridge/bridge.ts index 67e22ca0..0cd1692f 100644 --- a/packages/core/src/bridge/bridge.ts +++ b/packages/core/src/bridge/bridge.ts @@ -43,6 +43,7 @@ import { setGroupSpecialTitle as setGroupSpecialTitle_, setGroupRemark as setGroupRemark_, getGroupAtAllRemain as getGroupAtAllRemain_, + setGroupAvatar as setGroupAvatar_, } from './actions/group-admin'; import { uploadGroupFile as uploadGroupFile_, @@ -631,6 +632,7 @@ export class Bridge { async markPrivateMsgAsRead(userId: number, sequence: number): Promise { return markPrivateMsgAsRead_(this, userId, sequence); } async setFriendRemark(userId: number, remark: string): Promise { return setFriendRemark_(this, userId, remark); } async setGroupRemark(groupId: number, remark: string): Promise { return setGroupRemark_(this, groupId, remark); } + async setGroupAvatar(groupId: number, source: string): Promise { return setGroupAvatar_(this, groupId, source); } async getGroupHonorInfo(groupId: number, type: WebHonorType | string): Promise { return getGroupHonorInfo_(this, groupId, type); } diff --git a/packages/core/src/bridge/proto/highway.ts b/packages/core/src/bridge/proto/highway.ts index 2346dc81..f61f30bf 100644 --- a/packages/core/src/bridge/proto/highway.ts +++ b/packages/core/src/bridge/proto/highway.ts @@ -334,3 +334,13 @@ export const NTV2UploadRichMediaRespSchema = { respHead: { field: 1, type: 'message' as const, schema: NTV2UploadRespHeadSchema }, upload: { field: 2, type: 'message' as const, schema: NTV2UploadRespBodySchema }, } satisfies ProtoSchema; + +// --- Group Avatar Upload Extend --- + +export const GroupAvatarUploadExtSchema = { + field1: { field: 1, type: 'uint32' as const }, + groupId: { field: 2, type: 'uint64' as const }, + field3: { field: 3, type: 'bytes' as const }, + field5: { field: 5, type: 'uint32' as const }, + field6: { field: 6, type: 'uint32' as const }, +} satisfies ProtoSchema; diff --git a/packages/core/src/onebot/actions/group-admin.ts b/packages/core/src/onebot/actions/group-admin.ts index 2feebbc4..871e7753 100644 --- a/packages/core/src/onebot/actions/group-admin.ts +++ b/packages/core/src/onebot/actions/group-admin.ts @@ -103,7 +103,11 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void { return okResponse(); }); - h.registerAction('set_group_portrait', async () => { - return failedResponse(RETCODE.ACTION_FAILED, 'not yet implemented'); + h.registerAction('set_group_portrait', async (params) => { + const groupId = asNumber(params.group_id); + const file = asString(params.file); + if (!groupId || !file) return failedResponse(RETCODE.BAD_REQUEST, 'group_id and file are required'); + await ctx.setGroupAvatar(groupId, file); + return okResponse(); }); } diff --git a/packages/core/src/onebot/api-handler.ts b/packages/core/src/onebot/api-handler.ts index f2815912..f9762d41 100644 --- a/packages/core/src/onebot/api-handler.ts +++ b/packages/core/src/onebot/api-handler.ts @@ -106,6 +106,7 @@ export interface ApiActionContext { // Extended NapCat-compatible setFriendRemark: (userId: number, remark: string) => Promise; setGroupRemark: (groupId: number, remark: string) => Promise; + setGroupAvatar: (groupId: number, source: string) => Promise; getGroupFileCount: (groupId: number) => Promise<{ fileCount: number; maxCount: number }>; setMsgEmojiLike: (messageId: number, emojiId: string, set: boolean) => Promise; markGroupMsgAsRead: (groupId: number, sequence: number) => Promise; diff --git a/packages/core/src/onebot/instance-context.ts b/packages/core/src/onebot/instance-context.ts index 3131a020..059fd985 100644 --- a/packages/core/src/onebot/instance-context.ts +++ b/packages/core/src/onebot/instance-context.ts @@ -171,6 +171,7 @@ export function buildApiContext(ref: OneBotInstanceContext): ApiActionContext { forceFetchClientKey: () => bridge.forceFetchClientKey(), setFriendRemark: (userId, remark) => bridge.setFriendRemark(userId, remark), setGroupRemark: (groupId, remark) => bridge.setGroupRemark(groupId, remark), + setGroupAvatar: (groupId, source) => bridge.setGroupAvatar(groupId, source), getGroupFileCount: (groupId) => bridge.fetchGroupFileCount(groupId), setMsgEmojiLike: async (messageId, emojiId, set) => { const meta = messageStore.findMeta(messageId);