Files
NapCatQQ/packages/napcat-onebot/action/go-cqhttp/SendGroupNotice.ts
T
吴天一 deae7fd467 fix: 对齐 OneBot schema 与合并转发消息处理行为 (#1694)
* fix: make defaulted request fields optional

* fix: align onebot schemas with runtime behavior

* fix: tighten schema and unsupported action metadata

* fix: reject empty forward reference ids

* fix: update GoCQHTTPSendForwardMsg to handle messages correctly and adjust payload schema

* fix: refactor message handling in GoCQHTTPSendForwardMsg and add test for message normalization

* fix: update compatibility actions to return appropriate responses and align schemas with runtime behavior

* fix: ensure payload properties are non-null with default values in action handlers

* fix: update StreamStatus comments for clarity and enforce nickname as required in message schema

* fix: integrate sleep function in GetOnlineClient and update tests to verify API call

* fix: add production test checklist for OneBot schema alignment and message handling

* fix: remove outdated production test checklist for OneBot schema alignment
2026-03-12 01:54:33 +08:00

77 lines
3.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { checkFileExist, uriToLocalFile } from 'napcat-common/src/file';
import { OneBotAction } from '@/napcat-onebot/action/OneBotAction';
import { ActionName } from '@/napcat-onebot/action/router';
import { unlink } from 'node:fs/promises';
import { Static, Type } from '@sinclair/typebox';
import { GoCQHTTPActionsExamples } from '../example/GoCQHTTPActionsExamples';
export const SendGroupNoticePayloadSchema = Type.Object({
group_id: Type.String({ description: '群号' }),
content: Type.String({ description: '公告内容' }),
image: Type.Optional(Type.String({ description: '公告图片路径或 URL' })),
pinned: Type.Optional(Type.Union([Type.Number(), Type.String()], { default: 0, description: '是否置顶 (0/1)' })),
type: Type.Optional(Type.Union([Type.Number(), Type.String()], { default: 1, description: '类型 (默认为 1)' })),
confirm_required: Type.Optional(Type.Union([Type.Number(), Type.String()], { default: 1, description: '是否需要确认 (0/1)' })),
is_show_edit_card: Type.Optional(Type.Union([Type.Number(), Type.String()], { default: 0, description: '是否显示修改群名片引导 (0/1)' })),
tip_window_type: Type.Optional(Type.Union([Type.Number(), Type.String()], { default: 0, description: '弹窗类型 (默认为 0)' })),
});
export type SendGroupNoticePayload = Static<typeof SendGroupNoticePayloadSchema>;
export class SendGroupNotice extends OneBotAction<SendGroupNoticePayload, void> {
override actionName = ActionName.GoCQHTTP_SendGroupNotice;
override payloadSchema = SendGroupNoticePayloadSchema;
override returnSchema = Type.Null();
override actionSummary = '发送群公告';
override actionDescription = '在指定群聊中发布新的公告';
override actionTags = ['Go-CQHTTP'];
override payloadExample = GoCQHTTPActionsExamples.SendGroupNotice.payload;
override returnExample = GoCQHTTPActionsExamples.SendGroupNotice.response;
async _handle (payload: SendGroupNoticePayload) {
const pinned = payload.pinned!;
const type = payload.type!;
const confirmRequired = payload.confirm_required!;
const showEditCard = payload.is_show_edit_card!;
const tipWindowType = payload.tip_window_type!;
let UploadImage: { id: string, width: number, height: number; } | undefined;
if (payload.image) {
// 公告图逻辑
const {
path,
success,
} = (await uriToLocalFile(this.core.NapCatTempPath, payload.image));
if (!success) {
throw new Error(`群公告${payload.image}设置失败,image字段可能格式不正确`);
}
if (!path) {
throw new Error(`群公告${payload.image}设置失败,获取资源失败`);
}
await checkFileExist(path, 5000);
const ImageUploadResult = await this.core.apis.GroupApi.uploadGroupBulletinPic(payload.group_id.toString(), path);
if (ImageUploadResult.errCode !== 0) {
throw new Error(`群公告${payload.image}设置失败,图片上传失败 , 错误信息:${ImageUploadResult.errMsg}`);
}
unlink(path).catch(() => { });
UploadImage = ImageUploadResult.picInfo;
}
const publishGroupBulletinResult = await this.core.apis.WebApi.setGroupNotice(
payload.group_id.toString(),
payload.content,
+pinned,
+type,
+showEditCard,
+tipWindowType,
+confirmRequired,
UploadImage?.id,
UploadImage?.width,
UploadImage?.height
);
if (!publishGroupBulletinResult || publishGroupBulletinResult.ec !== 0) {
throw new Error(`设置群公告失败,错误信息:${publishGroupBulletinResult?.em}`);
}
}
}