refactor onebot action context modules
This commit is contained in:
8 files changed
+871
-729
No files matched your search
@@ -1,27 +1,49 @@
|
||||
import type { WebHonorType } from '@/bridge/web/group-honor';
|
||||
import type { Bridge } from '../bridge/bridge';
|
||||
import type { ForwardNodePayload, MessageElement } from '../bridge/events';
|
||||
import type { QQInfo } from '../bridge/qq-info';
|
||||
import type { ApiActionContext, MessageSendResult } from './api-handler';
|
||||
import type { ApiActionContext } from './api-handler';
|
||||
import type { EventConverter } from './event-converter';
|
||||
import { elementsToOneBotSegments } from './event-converter';
|
||||
import { MessageStore } from './message-store';
|
||||
import { parseMessage } from './message-parser';
|
||||
import { GROUP_MESSAGE_EVENT, PRIVATE_MESSAGE_EVENT, hashMessageIdInt32 } from './message-id';
|
||||
import type { MediaStore } from './media-store';
|
||||
import type { JsonObject, JsonValue, MessageMeta, OneBotConfig } from './types';
|
||||
import { createLogger } from '../utils/logger';
|
||||
import {WebHonorType} from "@/bridge/web/group-honor";
|
||||
|
||||
const log = createLogger('OneBot');
|
||||
import type { MessageStore } from './message-store';
|
||||
import type { JsonObject, MessageMeta, OneBotConfig } from './types';
|
||||
import {
|
||||
getDownloadRKeys,
|
||||
getFriendList,
|
||||
getGroupFiles,
|
||||
getGroupInfo,
|
||||
getGroupList,
|
||||
getGroupMemberInfo,
|
||||
getGroupMemberList,
|
||||
getGroupSystemMessages,
|
||||
getLoginInfo,
|
||||
getStrangerInfo,
|
||||
} from './modules/contact-actions';
|
||||
import {
|
||||
deleteMessage,
|
||||
getForwardMessage,
|
||||
getFriendMsgHistory,
|
||||
getGroupMsgHistory,
|
||||
sendGroupForwardMessage,
|
||||
sendGroupMessage,
|
||||
sendPrivateForwardMessage,
|
||||
sendPrivateMessage,
|
||||
setEssenceMessage,
|
||||
uploadForwardMessage,
|
||||
} from './modules/message-actions';
|
||||
import {
|
||||
getImageInfo as getCachedImageInfo,
|
||||
getRecordInfo as getCachedRecordInfo,
|
||||
} from './modules/media-actions';
|
||||
import { handleGroupAddRequest } from './modules/request-actions';
|
||||
|
||||
/**
|
||||
* Single shared context bag that flows through every OneBot instance-internal
|
||||
* subsystem: API builder and the per-kind event pipeline.
|
||||
*
|
||||
* Only fields that are actually read through this bag live here — the api
|
||||
* Only fields that are actually read through this bag live here. The API
|
||||
* handler and network manager are owned by `OneBotInstance` directly because
|
||||
* nothing reads them via ctx, and including them here would force a
|
||||
* chicken-and-egg `late-bound` field dance during construction.
|
||||
* chicken-and-egg late-bound field dance during construction.
|
||||
*
|
||||
* `dispatchEvent` is the indirection used by the event pipeline to hand a
|
||||
* converted OneBot event back to the instance for caching + adapter fan-out;
|
||||
@@ -60,17 +82,21 @@ export function buildApiContext(ref: OneBotInstanceContext): ApiActionContext {
|
||||
getMessageMeta: (messageId) => messageStore.findMeta(messageId),
|
||||
canSendImage: () => true,
|
||||
canSendRecord: () => true,
|
||||
sendPrivateMessage: (userId, message, autoEscape) => handleSendPrivate(ref, userId, message, autoEscape),
|
||||
sendGroupMessage: (groupId, message, autoEscape) => handleSendGroup(ref, groupId, message, autoEscape),
|
||||
deleteMessage: (messageId, meta) => handleDeleteMessage(bridge, meta),
|
||||
// Info retrieval (async, triggers OIDB fetch)
|
||||
getFriendList: () => handleGetFriendList(bridge, qqInfo),
|
||||
getGroupList: (noCache) => handleGetGroupList(bridge, qqInfo, noCache),
|
||||
getGroupInfo: (groupId, noCache) => handleGetGroupInfo(bridge, qqInfo, groupId, noCache),
|
||||
getGroupMemberList: (groupId, noCache) => handleGetGroupMemberList(bridge, qqInfo, groupId, noCache),
|
||||
getGroupMemberInfo: (groupId, userId, noCache) => handleGetGroupMemberInfo(bridge, qqInfo, groupId, userId, noCache),
|
||||
getStrangerInfo: (userId) => handleGetStrangerInfo(bridge, qqInfo, userId),
|
||||
// Group admin
|
||||
|
||||
// OneBot11 message actions.
|
||||
sendPrivateMessage: (userId, message, autoEscape) => sendPrivateMessage(ref, userId, message, autoEscape),
|
||||
sendGroupMessage: (groupId, message, autoEscape) => sendGroupMessage(ref, groupId, message, autoEscape),
|
||||
deleteMessage: (_messageId, meta) => deleteMessage(bridge, meta),
|
||||
|
||||
// OneBot11 info actions.
|
||||
getFriendList: () => getFriendList(bridge, qqInfo),
|
||||
getGroupList: (noCache) => getGroupList(bridge, qqInfo, noCache),
|
||||
getGroupInfo: (groupId, noCache) => getGroupInfo(bridge, qqInfo, groupId, noCache),
|
||||
getGroupMemberList: (groupId, noCache) => getGroupMemberList(bridge, qqInfo, groupId, noCache),
|
||||
getGroupMemberInfo: (groupId, userId, noCache) => getGroupMemberInfo(bridge, qqInfo, groupId, userId, noCache),
|
||||
getStrangerInfo: (userId) => getStrangerInfo(bridge, qqInfo, userId),
|
||||
|
||||
// Group admin.
|
||||
setGroupKick: (groupId, userId, reject) => bridge.kickGroupMember(groupId, userId, reject),
|
||||
setGroupKickMembers: (groupId, userIds, reject) => bridge.kickGroupMembers(groupId, userIds, reject),
|
||||
setGroupBan: (groupId, userId, duration) => bridge.muteGroupMember(groupId, userId, duration),
|
||||
@@ -83,7 +109,8 @@ export function buildApiContext(ref: OneBotInstanceContext): ApiActionContext {
|
||||
setGroupLeave: (groupId) => bridge.leaveGroup(groupId),
|
||||
setGroupSpecialTitle: (groupId, userId, title) => bridge.setGroupSpecialTitle(groupId, userId, title),
|
||||
getGroupAtAllRemain: (groupId) => bridge.getGroupAtAllRemain(groupId),
|
||||
// Group file
|
||||
|
||||
// Group file.
|
||||
uploadGroupFile: async (groupId, file, name, folderId, uploadFile) => {
|
||||
const result = await bridge.uploadGroupFile(groupId, file, name ?? '', folderId ?? '/', uploadFile ?? true);
|
||||
return result.fileId;
|
||||
@@ -93,71 +120,53 @@ export function buildApiContext(ref: OneBotInstanceContext): ApiActionContext {
|
||||
return result.fileId;
|
||||
},
|
||||
getGroupFileUrl: (groupId, fileId, busId) => bridge.fetchGroupFileUrl(groupId, fileId, busId ?? 102),
|
||||
getGroupFiles: (groupId, folderId) => handleGetGroupFiles(bridge, groupId, folderId),
|
||||
getGroupFiles: (groupId, folderId) => getGroupFiles(bridge, groupId, folderId),
|
||||
deleteGroupFile: (groupId, fileId) => bridge.deleteGroupFile(groupId, fileId),
|
||||
moveGroupFile: (groupId, fileId, parentDirectory, targetDirectory) => bridge.moveGroupFile(groupId, fileId, parentDirectory, targetDirectory),
|
||||
moveGroupFile: (groupId, fileId, parentDirectory, targetDirectory) => {
|
||||
return bridge.moveGroupFile(groupId, fileId, parentDirectory, targetDirectory);
|
||||
},
|
||||
createGroupFileFolder: (groupId, name, parentId) => bridge.createGroupFileFolder(groupId, name, parentId ?? '/'),
|
||||
deleteGroupFileFolder: (groupId, folderId) => bridge.deleteGroupFileFolder(groupId, folderId),
|
||||
renameGroupFileFolder: (groupId, folderId, newName) => bridge.renameGroupFileFolder(groupId, folderId, newName),
|
||||
getPrivateFileUrl: (userId, fileId, fileHash) => bridge.fetchPrivateFileUrl(userId, fileId, fileHash),
|
||||
// Requests
|
||||
|
||||
// Requests.
|
||||
handleFriendRequest: (flag, approve) => bridge.setFriendAddRequest(flag, approve),
|
||||
handleGroupRequest: (flag, _subType, approve, reason) => handleGroupAddRequest(bridge, flag, approve, reason),
|
||||
// Extended
|
||||
|
||||
// Extended.
|
||||
sendLike: (userId, times) => bridge.sendLike(userId, times),
|
||||
sendFriendPoke: (userId, targetId) => bridge.sendPoke(false, userId, targetId),
|
||||
sendGroupPoke: (groupId, userId) => bridge.sendPoke(true, groupId, userId),
|
||||
setEssenceMsg: (messageId) => handleSetEssence(bridge, messageStore, messageId, true),
|
||||
deleteEssenceMsg: (messageId) => handleSetEssence(bridge, messageStore, messageId, false),
|
||||
getProfileLike: (userId?: number, start: number = 0, limit: number = 10) => bridge.getProfileLike(userId, start, limit),
|
||||
fetchCustomFace: (count?: number) => bridge.fetchCustomFace(count),
|
||||
getEmojiLikes: (groupId, sequence, emojiId, emojiType, count, cookie) => bridge.getEmojiLikes(groupId, sequence, emojiId, emojiType, count, cookie),
|
||||
setEssenceMsg: (messageId) => setEssenceMessage(bridge, messageStore, messageId, true),
|
||||
deleteEssenceMsg: (messageId) => setEssenceMessage(bridge, messageStore, messageId, false),
|
||||
getProfileLike: (userId = undefined, start = 0, limit = 10) => bridge.getProfileLike(userId, start, limit),
|
||||
fetchCustomFace: (count) => bridge.fetchCustomFace(count),
|
||||
getEmojiLikes: (groupId, sequence, emojiId, emojiType, count, cookie) => {
|
||||
return bridge.getEmojiLikes(groupId, sequence, emojiId, emojiType, count, cookie);
|
||||
},
|
||||
getUnidirectionalFriendList: () => bridge.getUnidirectionalFriendList(),
|
||||
setSelfLongNick: (longNick) => bridge.setSelfLongNick(longNick),
|
||||
setInputStatus: (userId, eventType) => bridge.setInputStatus(userId, eventType),
|
||||
translateEn2Zh: (words) => bridge.translateEn2Zh(words),
|
||||
getMiniAppArk: (type, title, desc, picUrl, jumpUrl) => bridge.getMiniAppArk(type, title, desc, picUrl, jumpUrl),
|
||||
clickInlineKeyboardButton: (groupId, botAppid, buttonId, callbackData, msgSeq) => bridge.clickInlineKeyboardButton(groupId, botAppid, buttonId, callbackData, msgSeq),
|
||||
clickInlineKeyboardButton: (groupId, botAppid, buttonId, callbackData, msgSeq) => {
|
||||
return bridge.clickInlineKeyboardButton(groupId, botAppid, buttonId, callbackData, msgSeq);
|
||||
},
|
||||
sendGroupSign: (groupId) => bridge.sendGroupSign(groupId),
|
||||
// New extended
|
||||
|
||||
// NapCat-compatible extended actions.
|
||||
setGroupReaction: (groupId, sequence, code, isSet) => bridge.setGroupReaction(groupId, sequence, code, isSet),
|
||||
handleDeleteFriend: (userId, block) => bridge.deleteFriend(userId, !!block),
|
||||
getGroupMsgHistory: (groupId, messageId, count) => handleGetGroupMsgHistory(messageStore, groupId, messageId, count),
|
||||
getFriendMsgHistory: (userId, messageId, count) => handleGetFriendMsgHistory(messageStore, userId, messageId, count),
|
||||
handleGetGroupSystemMsg: async () => {
|
||||
try {
|
||||
const reqs = await bridge.fetchGroupRequests();
|
||||
return reqs.map(r => ({
|
||||
group_id: r.groupId, group_name: r.groupName,
|
||||
request_id: r.sequence, requester_uin: r.targetUin,
|
||||
requester_nick: r.targetName, message: r.comment,
|
||||
flag: `${r.eventType}:${r.groupId}:${r.targetUid}`,
|
||||
} as JsonObject));
|
||||
} catch { return []; }
|
||||
},
|
||||
getDownloadRKeys: async () => {
|
||||
try {
|
||||
const rkeys = await bridge.fetchDownloadRKeys();
|
||||
return rkeys.map(r => ({
|
||||
rkey: r.rkey, type: r.type, ttl: r.ttlSeconds, create_time: r.createTime,
|
||||
} as JsonObject));
|
||||
} catch { return []; }
|
||||
},
|
||||
sendGroupForwardMsg: async (groupId, messages) => {
|
||||
return handleSendGroupForward(ref, groupId, messages);
|
||||
},
|
||||
sendPrivateForwardMsg: async (userId, messages) => {
|
||||
return handleSendPrivateForward(ref, userId, messages);
|
||||
},
|
||||
sendForwardMsg: async (messages) => {
|
||||
return handleUploadForward(ref, messages);
|
||||
},
|
||||
getForwardMsg: async (resId) => {
|
||||
return handleGetForward(ref, resId);
|
||||
},
|
||||
forceFetchClientKey: async () => bridge.forceFetchClientKey(),
|
||||
|
||||
// Extended NapCat-compatible
|
||||
getGroupMsgHistory: (groupId, messageId, count) => getGroupMsgHistory(messageStore, groupId, messageId, count),
|
||||
getFriendMsgHistory: (userId, messageId, count) => getFriendMsgHistory(messageStore, userId, messageId, count),
|
||||
handleGetGroupSystemMsg: () => getGroupSystemMessages(bridge),
|
||||
getDownloadRKeys: () => getDownloadRKeys(bridge),
|
||||
sendGroupForwardMsg: (groupId, messages) => sendGroupForwardMessage(ref, groupId, messages),
|
||||
sendPrivateForwardMsg: (userId, messages) => sendPrivateForwardMessage(ref, userId, messages),
|
||||
sendForwardMsg: (messages) => uploadForwardMessage(ref, messages),
|
||||
getForwardMsg: (resId) => getForwardMessage(ref, resId),
|
||||
forceFetchClientKey: () => bridge.forceFetchClientKey(),
|
||||
setFriendRemark: (userId, remark) => bridge.setFriendRemark(userId, remark),
|
||||
setGroupRemark: (groupId, remark) => bridge.setGroupRemark(groupId, remark),
|
||||
getGroupFileCount: (groupId) => bridge.fetchGroupFileCount(groupId),
|
||||
@@ -166,663 +175,27 @@ export function buildApiContext(ref: OneBotInstanceContext): ApiActionContext {
|
||||
if (!meta || !meta.isGroup) throw new Error('message not found or not a group message');
|
||||
await bridge.setGroupReaction(meta.targetId, meta.sequence, emojiId, set);
|
||||
},
|
||||
markGroupMsgAsRead: (groupId: number, sequence: number) => bridge.markGroupMsgAsRead(groupId, sequence),
|
||||
markPrivateMsgAsRead: (userId: number, sequence: number) => bridge.markPrivateMsgAsRead(userId, sequence),
|
||||
setOnlineStatus: (status: number, extStatus?: number, batteryStatus?: number) => bridge.setOnlineStatus(status, extStatus, batteryStatus),
|
||||
setProfile: (nickname?: string, personalNote?: string) => bridge.setProfile(nickname, personalNote),
|
||||
// web
|
||||
getGroupHonorInfo: (groupId: number, type: WebHonorType | string)=> bridge.getGroupHonorInfo(groupId, type),
|
||||
getGroupEssence: (groupId: number, pageStart: number = 0, pageLimit: number = 50) => bridge.getGroupEssence(groupId, pageStart, pageLimit),
|
||||
getGroupEssenceAll: (groupId: number) => bridge.getGroupEssenceAll(groupId),
|
||||
sendGroupNotice: (groupId: number, content: string, options?: any) => bridge.sendGroupNotice(groupId, content, options),
|
||||
getGroupNotice: (groupId: number) => bridge.getGroupNotice(groupId),
|
||||
deleteGroupNotice: (groupId: number, fid: string) => bridge.deleteGroupNotice(groupId, fid),
|
||||
getCookiesStr: (domain: string) => bridge.getCookiesStr(domain),
|
||||
markGroupMsgAsRead: (groupId, sequence) => bridge.markGroupMsgAsRead(groupId, sequence),
|
||||
markPrivateMsgAsRead: (userId, sequence) => bridge.markPrivateMsgAsRead(userId, sequence),
|
||||
setOnlineStatus: (status, extStatus, batteryStatus) => bridge.setOnlineStatus(status, extStatus, batteryStatus),
|
||||
setProfile: (nickname, personalNote) => bridge.setProfile(nickname, personalNote),
|
||||
|
||||
// Web-backed actions.
|
||||
getGroupHonorInfo: (groupId: number, type: WebHonorType | string) => bridge.getGroupHonorInfo(groupId, type),
|
||||
getGroupEssence: (groupId, pageStart = 0, pageLimit = 50) => bridge.getGroupEssence(groupId, pageStart, pageLimit),
|
||||
getGroupEssenceAll: (groupId) => bridge.getGroupEssenceAll(groupId),
|
||||
sendGroupNotice: (groupId, content, options) => bridge.sendGroupNotice(groupId, content, options),
|
||||
getGroupNotice: (groupId) => bridge.getGroupNotice(groupId),
|
||||
deleteGroupNotice: (groupId, fid) => bridge.deleteGroupNotice(groupId, fid),
|
||||
getCookiesStr: (domain) => bridge.getCookiesStr(domain),
|
||||
getCsrfToken: () => bridge.getCsrfToken(),
|
||||
getCredentials: (domain: string) => bridge.getCredentials(domain),
|
||||
// Media lookup
|
||||
getImageInfo: (file) => handleGetImageInfo(mediaStore, file),
|
||||
getRecordInfo: (file) => handleGetRecordInfo(bridge, mediaStore, file),
|
||||
// Avatar
|
||||
setAvatar: (source: string) => bridge.setAvatar(source),
|
||||
getCredentials: (domain) => bridge.getCredentials(domain),
|
||||
|
||||
// Media lookup.
|
||||
getImageInfo: (file) => getCachedImageInfo(mediaStore, file),
|
||||
getRecordInfo: (file) => getCachedRecordInfo(bridge, mediaStore, file),
|
||||
|
||||
// Avatar.
|
||||
setAvatar: (source) => bridge.setAvatar(source),
|
||||
};
|
||||
}
|
||||
|
||||
// --- Media lookup ---
|
||||
|
||||
async function handleGetImageInfo(
|
||||
mediaStore: MediaStore,
|
||||
file: string,
|
||||
): Promise<JsonObject | null> {
|
||||
const cached = mediaStore.findImage(file);
|
||||
if (!cached) return null;
|
||||
const url = cached.url || cached.imageUrl || '';
|
||||
return {
|
||||
file: url || cached.file,
|
||||
url,
|
||||
file_size: String(cached.fileSize ?? 0),
|
||||
file_name: cached.fileName || cached.file,
|
||||
};
|
||||
}
|
||||
|
||||
async function handleGetRecordInfo(
|
||||
bridge: Bridge,
|
||||
mediaStore: MediaStore,
|
||||
file: string,
|
||||
): Promise<JsonObject | null> {
|
||||
const cached = mediaStore.findRecord(file);
|
||||
if (!cached) return null;
|
||||
|
||||
// Re-resolve via OIDB if the cached URL is missing or empty.
|
||||
// Mirrors NapCat's getPttUrl path: GetGroupPttUrl / GetPttUrl by fileUuid.
|
||||
let url = cached.url;
|
||||
if (!url && cached.mediaNode) {
|
||||
try {
|
||||
url = cached.isGroup
|
||||
? await bridge.fetchGroupPttUrlByNode(cached.sessionId, cached.mediaNode)
|
||||
: await bridge.fetchPrivatePttUrlByNode(cached.mediaNode);
|
||||
if (url) {
|
||||
mediaStore.updateRecordUrl(file, url);
|
||||
}
|
||||
} catch (err) {
|
||||
log.warn('get_record url refetch failed: %s', err instanceof Error ? err.message : String(err));
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
file: url || cached.file,
|
||||
url: url || '',
|
||||
file_size: String(cached.fileSize ?? 0),
|
||||
file_name: cached.fileName || cached.file,
|
||||
};
|
||||
}
|
||||
|
||||
// --- Login info ---
|
||||
|
||||
function getLoginInfo(ref: OneBotInstanceContext): { userId: number; nickname: string } {
|
||||
const userId = parseInt(ref.uin, 10) || 0;
|
||||
const nickname = ref.qqInfo.nickname || ref.uin;
|
||||
return { userId, nickname };
|
||||
}
|
||||
|
||||
// NOTE: do NOT iterate all groups here. Calling fetchGroupMemberList for
|
||||
// every group on demand causes a fan-out (1 incoming request -> N OIDB
|
||||
// 0xfe7_3 calls where N = group count). When a busy OneBot client (e.g.
|
||||
// MaiBot) issues `get_group_member_info(no_cache=true)` per inbound
|
||||
// message, this amplifier produced ~hundreds of OIDB calls per chat
|
||||
// message and triggered Tencent risk-control / 7-day account ban.
|
||||
// Each handler below now refreshes only the resource it actually needs.
|
||||
|
||||
async function refreshGroupListOnly(bridge: Bridge, qqInfo: QQInfo): Promise<void> {
|
||||
try {
|
||||
await bridge.fetchGroupList();
|
||||
} catch { /* use cached */ }
|
||||
void qqInfo;
|
||||
}
|
||||
|
||||
async function refreshSingleGroupMembers(bridge: Bridge, groupId: number): Promise<void> {
|
||||
try {
|
||||
await bridge.fetchGroupMemberList(groupId);
|
||||
} catch { /* use cached */ }
|
||||
}
|
||||
|
||||
function normalizeHistoryCount(count?: number): number {
|
||||
if (!Number.isFinite(count)) return 20;
|
||||
const n = Math.trunc(count as number);
|
||||
if (n <= 0) return 20;
|
||||
if (n > 200) return 200;
|
||||
return n;
|
||||
}
|
||||
|
||||
function sanitizeMessageEventForApi(event: JsonObject): JsonObject {
|
||||
const result: JsonObject = { ...event };
|
||||
delete result.post_type;
|
||||
delete result.self_id;
|
||||
result.real_id = (result.message_id ?? 0) as JsonValue;
|
||||
return result;
|
||||
}
|
||||
|
||||
// --- Info retrieval ---
|
||||
|
||||
async function handleGetFriendList(bridge: Bridge, qqInfo: QQInfo): Promise<JsonObject[]> {
|
||||
try {
|
||||
const friends = await bridge.fetchFriendList();
|
||||
return friends.map(f => ({
|
||||
user_id: f.uin as any,
|
||||
nickname: f.nickname as any,
|
||||
remark: f.remark as any,
|
||||
}));
|
||||
} catch {
|
||||
return qqInfo.friends.map(f => ({
|
||||
user_id: f.uin as any,
|
||||
nickname: f.nickname as any,
|
||||
remark: f.remark as any,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
async function handleGetGroupList(bridge: Bridge, qqInfo: QQInfo, noCache?: boolean): Promise<JsonObject[]> {
|
||||
try {
|
||||
if (noCache || qqInfo.groups.length === 0) {
|
||||
await bridge.fetchGroupList();
|
||||
}
|
||||
} catch { /* use cached */ }
|
||||
return qqInfo.groups.map(g => ({
|
||||
group_id: g.groupId as any,
|
||||
group_name: g.groupName as any,
|
||||
member_count: g.memberCount as any,
|
||||
max_member_count: g.memberMax as any,
|
||||
}));
|
||||
}
|
||||
|
||||
async function handleGetGroupInfo(bridge: Bridge, qqInfo: QQInfo, groupId: number, noCache?: boolean): Promise<JsonObject | null> {
|
||||
if (noCache || !qqInfo.findGroup(groupId)) {
|
||||
try { await bridge.fetchGroupList(); } catch { /* use cached */ }
|
||||
}
|
||||
const g = qqInfo.findGroup(groupId);
|
||||
if (!g) return null;
|
||||
return {
|
||||
group_id: g.groupId as any,
|
||||
group_name: g.groupName as any,
|
||||
member_count: g.memberCount as any,
|
||||
max_member_count: g.memberMax as any,
|
||||
};
|
||||
}
|
||||
|
||||
async function handleGetGroupMemberList(bridge: Bridge, qqInfo: QQInfo, groupId: number, noCache?: boolean): Promise<JsonObject[]> {
|
||||
if (noCache) {
|
||||
await refreshSingleGroupMembers(bridge, groupId);
|
||||
|
||||
const g = qqInfo.findGroup(groupId);
|
||||
if (!g) return [];
|
||||
const result: JsonObject[] = [];
|
||||
for (const [, m] of g.members) {
|
||||
result.push({
|
||||
group_id: groupId as any, user_id: m.uin as any,
|
||||
nickname: m.nickname as any, card: m.card as any,
|
||||
sex: 'unknown' as any, age: 0 as any,
|
||||
join_time: m.joinTime as any, last_sent_time: m.lastSentTime as any,
|
||||
level: String(m.level) as any, role: m.role as any, title: m.title as any,
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
try {
|
||||
const members = await bridge.fetchGroupMemberList(groupId);
|
||||
return members.map(m => ({
|
||||
group_id: groupId as any,
|
||||
user_id: m.uin as any,
|
||||
nickname: m.nickname as any,
|
||||
card: m.card as any,
|
||||
sex: 'unknown' as any,
|
||||
age: 0 as any,
|
||||
join_time: m.joinTime as any,
|
||||
last_sent_time: m.lastSentTime as any,
|
||||
level: String(m.level) as any,
|
||||
role: m.role as any,
|
||||
title: m.title as any,
|
||||
}));
|
||||
} catch {
|
||||
const g = qqInfo.findGroup(groupId);
|
||||
if (!g) return [];
|
||||
const result: JsonObject[] = [];
|
||||
for (const [, m] of g.members) {
|
||||
result.push({
|
||||
group_id: groupId as any, user_id: m.uin as any,
|
||||
nickname: m.nickname as any, card: m.card as any,
|
||||
sex: 'unknown' as any, age: 0 as any,
|
||||
join_time: m.joinTime as any, last_sent_time: m.lastSentTime as any,
|
||||
level: String(m.level) as any, role: m.role as any, title: m.title as any,
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
async function handleGetGroupMemberInfo(bridge: Bridge, qqInfo: QQInfo, groupId: number, userId: number, noCache?: boolean): Promise<JsonObject | null> {
|
||||
if (noCache || !qqInfo.findGroupMember(groupId, userId)) {
|
||||
await refreshSingleGroupMembers(bridge, groupId);
|
||||
}
|
||||
const m = qqInfo.findGroupMember(groupId, userId);
|
||||
if (!m) return null;
|
||||
return {
|
||||
group_id: groupId as any, user_id: m.uin as any,
|
||||
nickname: m.nickname as any, card: m.card as any,
|
||||
sex: 'unknown' as any, age: 0 as any,
|
||||
join_time: m.joinTime as any, last_sent_time: m.lastSentTime as any,
|
||||
level: String(m.level) as any, role: m.role as any, title: m.title as any,
|
||||
};
|
||||
}
|
||||
|
||||
async function handleGetGroupFiles(bridge: Bridge, groupId: number, folderId?: string): Promise<JsonObject> {
|
||||
const result = await bridge.fetchGroupFiles(groupId, folderId ?? '/');
|
||||
return {
|
||||
files: result.files.map((file) => ({
|
||||
group_id: groupId as any,
|
||||
file_id: file.fileId as any,
|
||||
file_name: file.fileName as any,
|
||||
busid: file.busId as any,
|
||||
file_size: file.fileSize as any,
|
||||
upload_time: file.uploadTime as any,
|
||||
dead_time: file.deadTime as any,
|
||||
modify_time: file.modifyTime as any,
|
||||
download_times: file.downloadTimes as any,
|
||||
uploader: file.uploader as any,
|
||||
uploader_name: file.uploaderName as any,
|
||||
} as JsonObject)) as any,
|
||||
folders: result.folders.map((folder) => ({
|
||||
group_id: groupId as any,
|
||||
folder_id: folder.folderId as any,
|
||||
folder_name: folder.folderName as any,
|
||||
create_time: folder.createTime as any,
|
||||
creator: folder.creator as any,
|
||||
create_name: folder.creatorName as any,
|
||||
total_file_count: folder.totalFileCount as any,
|
||||
} as JsonObject)) as any,
|
||||
};
|
||||
}
|
||||
|
||||
async function handleGetStrangerInfo(bridge: Bridge, qqInfo: QQInfo, userId: number): Promise<JsonObject | null> {
|
||||
try {
|
||||
const p = await bridge.fetchUserProfile(userId);
|
||||
return {
|
||||
user_id: p.uin as any,
|
||||
nickname: p.nickname as any,
|
||||
sex: p.sex as any,
|
||||
age: p.age as any,
|
||||
};
|
||||
} catch {
|
||||
const p = qqInfo.findUserProfile(userId);
|
||||
if (!p) return null;
|
||||
return { user_id: p.uin as any, nickname: p.nickname as any, sex: p.sex as any, age: p.age as any };
|
||||
}
|
||||
}
|
||||
|
||||
// --- Message history ---
|
||||
|
||||
async function handleGetGroupMsgHistory(
|
||||
messageStore: MessageStore,
|
||||
groupId: number,
|
||||
messageId?: number,
|
||||
count?: number,
|
||||
): Promise<JsonObject[]> {
|
||||
if (!Number.isInteger(groupId) || groupId <= 0) return [];
|
||||
const limit = normalizeHistoryCount(count);
|
||||
|
||||
let anchorSequence: number | undefined;
|
||||
if (Number.isInteger(messageId) && messageId !== 0) {
|
||||
const meta = messageStore.findMeta(messageId as number);
|
||||
if (!meta || !meta.isGroup || meta.targetId !== groupId || meta.sequence <= 0) return [];
|
||||
anchorSequence = meta.sequence;
|
||||
}
|
||||
|
||||
const events = messageStore.listSessionEvents(true, groupId, limit, anchorSequence);
|
||||
return events
|
||||
.filter((event) => {
|
||||
if (event.message_type !== 'group') return false;
|
||||
const gid = Number(event.group_id ?? 0);
|
||||
return Number.isFinite(gid) && Math.trunc(gid) === groupId;
|
||||
})
|
||||
.map(sanitizeMessageEventForApi);
|
||||
}
|
||||
|
||||
async function handleGetFriendMsgHistory(
|
||||
messageStore: MessageStore,
|
||||
userId: number,
|
||||
messageId?: number,
|
||||
count?: number,
|
||||
): Promise<JsonObject[]> {
|
||||
if (!Number.isInteger(userId) || userId <= 0) return [];
|
||||
const limit = normalizeHistoryCount(count);
|
||||
|
||||
let anchorSequence: number | undefined;
|
||||
if (Number.isInteger(messageId) && messageId !== 0) {
|
||||
const meta = messageStore.findMeta(messageId as number);
|
||||
if (!meta || meta.isGroup || meta.targetId !== userId || meta.sequence <= 0) return [];
|
||||
anchorSequence = meta.sequence;
|
||||
}
|
||||
|
||||
const events = messageStore.listSessionEvents(false, userId, limit, anchorSequence);
|
||||
return events
|
||||
.filter((event) => {
|
||||
if (event.message_type !== 'private') return false;
|
||||
const uid = Number(event.user_id ?? 0);
|
||||
return Number.isFinite(uid) && Math.trunc(uid) === userId;
|
||||
})
|
||||
.map(sanitizeMessageEventForApi);
|
||||
}
|
||||
|
||||
// --- Delete message ---
|
||||
|
||||
async function handleDeleteMessage(bridge: Bridge, meta: MessageMeta): Promise<void> {
|
||||
if (meta.isGroup) {
|
||||
await bridge.recallGroupMessage(meta.targetId, meta.sequence);
|
||||
} else {
|
||||
await bridge.recallPrivateMessage(
|
||||
meta.targetId, meta.clientSequence, meta.sequence, meta.random, meta.timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
// --- Group request ---
|
||||
|
||||
async function handleGroupAddRequest(bridge: Bridge, flag: string, approve: boolean, reason: string): Promise<void> {
|
||||
// flag format: "add:groupId:uid" or "invite:groupId:uid" (from event-converter)
|
||||
const parts = flag.split(':');
|
||||
if (parts.length < 3) throw new Error('invalid group request flag');
|
||||
const groupId = parseInt(parts[1], 10);
|
||||
if (!groupId) throw new Error('invalid group_id in flag');
|
||||
// We need sequence and eventType -- fetch from group requests
|
||||
const requests = await bridge.fetchGroupRequests();
|
||||
const matching = requests.find(r => r.groupId === groupId);
|
||||
if (matching) {
|
||||
await bridge.setGroupAddRequest(groupId, matching.sequence, matching.eventType, approve, reason, matching.filtered);
|
||||
} else {
|
||||
throw new Error('matching group request not found');
|
||||
}
|
||||
}
|
||||
|
||||
// --- Essence ---
|
||||
|
||||
async function handleSetEssence(bridge: Bridge, messageStore: MessageStore, messageId: number, enable: boolean): Promise<void> {
|
||||
const meta = messageStore.findMeta(messageId);
|
||||
if (!meta || !meta.isGroup) throw new Error('message not found or not a group message');
|
||||
await bridge.setGroupEssence(meta.targetId, meta.sequence, meta.random, enable);
|
||||
}
|
||||
|
||||
// --- Send message logging ---
|
||||
|
||||
function logSentMessage(isGroup: boolean, targetId: number, elements: MessageElement[]): void {
|
||||
const type = isGroup ? '群聊' : '私聊';
|
||||
const parts: string[] = [];
|
||||
|
||||
// Check for reply element
|
||||
const replyElem = elements.find(e => e.type === 'reply');
|
||||
if (replyElem && replyElem.replyMessageId) {
|
||||
parts.push(`[回复:${replyElem.replyMessageId}]`);
|
||||
}
|
||||
|
||||
// Build message content preview
|
||||
for (const elem of elements) {
|
||||
if (elem.type === 'reply') continue; // Already handled
|
||||
|
||||
switch (elem.type) {
|
||||
case 'text':
|
||||
if (elem.text) {
|
||||
const preview = elem.text.length > 50 ? elem.text.substring(0, 50) + '...' : elem.text;
|
||||
parts.push(preview);
|
||||
}
|
||||
break;
|
||||
case 'image':
|
||||
parts.push('[图片]');
|
||||
break;
|
||||
case 'face':
|
||||
parts.push('[表情]');
|
||||
break;
|
||||
case 'at':
|
||||
if (elem.text) parts.push(elem.text.trim());
|
||||
break;
|
||||
case 'record':
|
||||
parts.push('[语音]');
|
||||
break;
|
||||
case 'video':
|
||||
parts.push('[视频]');
|
||||
break;
|
||||
case 'json':
|
||||
parts.push('[JSON消息]');
|
||||
break;
|
||||
case 'xml':
|
||||
parts.push('[XML消息]');
|
||||
break;
|
||||
case 'markdown':
|
||||
parts.push('[Markdown]');
|
||||
break;
|
||||
case 'forward':
|
||||
parts.push('[转发消息]');
|
||||
break;
|
||||
case 'poke':
|
||||
parts.push('[戳一戳]');
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const content = parts.join(' ').trim() || '[空消息]';
|
||||
log.info(`${type} ${targetId} | 发送:${content}`);
|
||||
}
|
||||
|
||||
// --- Send message ---
|
||||
|
||||
async function handleSendPrivate(
|
||||
ref: OneBotInstanceContext,
|
||||
userId: number,
|
||||
message: JsonValue,
|
||||
autoEscape: boolean,
|
||||
): Promise<MessageSendResult> {
|
||||
const elements = await parseMessage(message, autoEscape, {
|
||||
resolveReplySequence: (replyMessageId) => {
|
||||
return ref.messageStore.resolveReplySequence(false, userId, replyMessageId);
|
||||
},
|
||||
resolveReplyMeta: (replyMessageId) => {
|
||||
const meta = ref.messageStore.findMeta(replyMessageId);
|
||||
if (meta) {
|
||||
return {
|
||||
senderUin: meta.targetId, // For received messages, targetId is the sender
|
||||
time: meta.timestamp,
|
||||
random: meta.random,
|
||||
};
|
||||
}
|
||||
return null;
|
||||
},
|
||||
resolveMentionUid: (targetUin) => ref.bridge.resolveUserUid(targetUin),
|
||||
musicSignUrl: ref.musicSignUrl,
|
||||
});
|
||||
if (elements.length === 0) throw new Error('message is empty');
|
||||
|
||||
const receipt = await ref.bridge.sendPrivateMessage(userId, elements);
|
||||
const messageId = hashMessageIdInt32(receipt.sequence, userId, PRIVATE_MESSAGE_EVENT);
|
||||
|
||||
// Log sent message
|
||||
logSentMessage(false, userId, elements);
|
||||
|
||||
// Cache the sent message meta
|
||||
ref.cacheMessageMeta(messageId, {
|
||||
isGroup: false,
|
||||
targetId: userId,
|
||||
sequence: receipt.sequence,
|
||||
eventName: PRIVATE_MESSAGE_EVENT,
|
||||
clientSequence: receipt.clientSequence,
|
||||
random: receipt.random,
|
||||
timestamp: receipt.timestamp,
|
||||
});
|
||||
|
||||
return { messageId };
|
||||
}
|
||||
|
||||
async function handleSendGroup(
|
||||
ref: OneBotInstanceContext,
|
||||
groupId: number,
|
||||
message: JsonValue,
|
||||
autoEscape: boolean,
|
||||
): Promise<MessageSendResult> {
|
||||
const elements = await parseMessage(message, autoEscape, {
|
||||
resolveReplySequence: (replyMessageId) => {
|
||||
return ref.messageStore.resolveReplySequence(true, groupId, replyMessageId);
|
||||
},
|
||||
resolveReplyMeta: (replyMessageId) => {
|
||||
const event = ref.messageStore.findEvent(replyMessageId);
|
||||
if (event) {
|
||||
return {
|
||||
senderUin: typeof event.user_id === 'number' ? event.user_id : parseInt(String(event.user_id || '0'), 10),
|
||||
time: typeof event.time === 'number' ? event.time : parseInt(String(event.time || '0'), 10),
|
||||
random: 0, // Group messages might not have random field in event
|
||||
};
|
||||
}
|
||||
return null;
|
||||
},
|
||||
resolveMentionUid: (targetUin) => ref.bridge.resolveUserUid(targetUin, groupId),
|
||||
musicSignUrl: ref.musicSignUrl,
|
||||
});
|
||||
if (elements.length === 0) throw new Error('message is empty');
|
||||
|
||||
const receipt = await ref.bridge.sendGroupMessage(groupId, elements);
|
||||
const messageId = hashMessageIdInt32(receipt.sequence, groupId, GROUP_MESSAGE_EVENT);
|
||||
|
||||
// Log sent message
|
||||
logSentMessage(true, groupId, elements);
|
||||
|
||||
// Cache the sent message meta
|
||||
ref.cacheMessageMeta(messageId, {
|
||||
isGroup: true,
|
||||
targetId: groupId,
|
||||
sequence: receipt.sequence,
|
||||
eventName: GROUP_MESSAGE_EVENT,
|
||||
clientSequence: receipt.clientSequence,
|
||||
random: receipt.random,
|
||||
timestamp: receipt.timestamp,
|
||||
});
|
||||
|
||||
return { messageId };
|
||||
}
|
||||
|
||||
async function handleSendGroupForward(
|
||||
ref: OneBotInstanceContext,
|
||||
groupId: number,
|
||||
messages: JsonValue,
|
||||
): Promise<{ messageId: number; forwardId: string }> {
|
||||
const nodes = await parseForwardNodes(ref, messages);
|
||||
const forwardId = await ref.bridge.uploadForwardNodes(nodes, groupId);
|
||||
const receipt = await ref.bridge.sendGroupMessage(groupId, [{ type: 'forward', resId: forwardId }]);
|
||||
const messageId = hashMessageIdInt32(receipt.sequence, groupId, GROUP_MESSAGE_EVENT);
|
||||
|
||||
ref.cacheMessageMeta(messageId, {
|
||||
isGroup: true,
|
||||
targetId: groupId,
|
||||
sequence: receipt.sequence,
|
||||
eventName: GROUP_MESSAGE_EVENT,
|
||||
clientSequence: receipt.clientSequence,
|
||||
random: receipt.random,
|
||||
timestamp: receipt.timestamp,
|
||||
});
|
||||
|
||||
return { messageId, forwardId };
|
||||
}
|
||||
|
||||
async function handleSendPrivateForward(
|
||||
ref: OneBotInstanceContext,
|
||||
userId: number,
|
||||
messages: JsonValue,
|
||||
): Promise<{ messageId: number; forwardId: string }> {
|
||||
const nodes = await parseForwardNodes(ref, messages);
|
||||
const forwardId = await ref.bridge.uploadForwardNodes(nodes);
|
||||
const receipt = await ref.bridge.sendPrivateMessage(userId, [{ type: 'forward', resId: forwardId }]);
|
||||
const messageId = hashMessageIdInt32(receipt.sequence, userId, PRIVATE_MESSAGE_EVENT);
|
||||
|
||||
ref.cacheMessageMeta(messageId, {
|
||||
isGroup: false,
|
||||
targetId: userId,
|
||||
sequence: receipt.sequence,
|
||||
eventName: PRIVATE_MESSAGE_EVENT,
|
||||
clientSequence: receipt.clientSequence,
|
||||
random: receipt.random,
|
||||
timestamp: receipt.timestamp,
|
||||
});
|
||||
|
||||
return { messageId, forwardId };
|
||||
}
|
||||
|
||||
async function handleUploadForward(
|
||||
ref: OneBotInstanceContext,
|
||||
messages: JsonValue,
|
||||
): Promise<{ forwardId: string }> {
|
||||
const nodes = await parseForwardNodes(ref, messages);
|
||||
const forwardId = await ref.bridge.uploadForwardNodes(nodes);
|
||||
return { forwardId };
|
||||
}
|
||||
|
||||
async function handleGetForward(
|
||||
ref: OneBotInstanceContext,
|
||||
resId: string,
|
||||
): Promise<JsonObject[]> {
|
||||
const nodes = await ref.bridge.fetchForwardNodes(resId);
|
||||
const results: JsonObject[] = [];
|
||||
for (const node of nodes) {
|
||||
results.push({
|
||||
type: 'node' as any,
|
||||
data: {
|
||||
user_id: node.userUin as any,
|
||||
nickname: node.nickname as any,
|
||||
uin: String(node.userUin) as any,
|
||||
name: node.nickname as any,
|
||||
content: await elementsToOneBotSegments(node.elements, false, node.userUin) as any,
|
||||
} as any,
|
||||
} as JsonObject);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
async function parseForwardNodes(ref: OneBotInstanceContext, messages: JsonValue): Promise<ForwardNodePayload[]> {
|
||||
if (!Array.isArray(messages)) {
|
||||
throw new Error('forward messages must be an array');
|
||||
}
|
||||
|
||||
const nodes: ForwardNodePayload[] = [];
|
||||
for (const item of messages) {
|
||||
const segment = asJsonObject(item);
|
||||
if (!segment) continue;
|
||||
|
||||
let nodeData: JsonObject | null = null;
|
||||
if (String(segment.type ?? '') === 'node') {
|
||||
nodeData = asJsonObject(segment.data);
|
||||
} else if (segment.content !== undefined || segment.message !== undefined) {
|
||||
nodeData = segment;
|
||||
}
|
||||
if (!nodeData) continue;
|
||||
|
||||
const messageId = toPositiveInt(nodeData.id ?? nodeData.message_id);
|
||||
if (messageId > 0) {
|
||||
const event = ref.messageStore.findEvent(messageId);
|
||||
if (!event) throw new Error(`forward node message_id not found: ${messageId}`);
|
||||
|
||||
const eventSender = asJsonObject(event.sender) ?? {};
|
||||
const nickname = String(eventSender.card ?? eventSender.nickname ?? nodeData.nickname ?? nodeData.name ?? '');
|
||||
const userUin = toPositiveInt(event.user_id);
|
||||
const content = (event.message ?? event.raw_message ?? '') as JsonValue;
|
||||
const elements = await parseMessage(content, false);
|
||||
if (userUin > 0 && elements.length > 0) {
|
||||
nodes.push({ userUin, nickname: nickname || String(userUin), elements });
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
const userUin = toPositiveInt(nodeData.user_id ?? nodeData.uin);
|
||||
if (userUin <= 0) throw new Error('forward node user_id/uin is required');
|
||||
|
||||
const nickname = String(nodeData.nickname ?? nodeData.name ?? userUin);
|
||||
const content = (nodeData.content ?? nodeData.message ?? '') as JsonValue;
|
||||
const elements = await parseMessage(content, false);
|
||||
if (elements.length === 0) throw new Error(`forward node content is empty: ${userUin}`);
|
||||
|
||||
nodes.push({ userUin, nickname, elements });
|
||||
}
|
||||
|
||||
if (nodes.length === 0) {
|
||||
throw new Error('forward node list is empty');
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
function asJsonObject(value: JsonValue | undefined): JsonObject | null {
|
||||
if (typeof value !== 'object' || value === null || Array.isArray(value)) return null;
|
||||
return value as JsonObject;
|
||||
}
|
||||
|
||||
function toPositiveInt(value: JsonValue | undefined): number {
|
||||
if (typeof value === 'number' && Number.isFinite(value)) return Math.max(0, Math.trunc(value));
|
||||
if (typeof value === 'string' && value.trim()) {
|
||||
const parsed = Number(value);
|
||||
if (Number.isFinite(parsed)) return Math.max(0, Math.trunc(parsed));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
import type { Bridge } from '../../bridge/bridge';
|
||||
import type { QQInfo } from '../../bridge/qq-info';
|
||||
import type { OneBotInstanceContext } from '../instance-context';
|
||||
import type { JsonObject } from '../types';
|
||||
|
||||
export function getLoginInfo(ref: OneBotInstanceContext): { userId: number; nickname: string } {
|
||||
const userId = parseInt(ref.uin, 10) || 0;
|
||||
const nickname = ref.qqInfo.nickname || ref.uin;
|
||||
return { userId, nickname };
|
||||
}
|
||||
|
||||
// Keep refreshes scoped. A broad "refresh all members in all groups" call can
|
||||
// turn one OneBot request into N OIDB calls, which is risky for busy clients.
|
||||
async function refreshSingleGroupMembers(bridge: Bridge, groupId: number): Promise<void> {
|
||||
try {
|
||||
await bridge.fetchGroupMemberList(groupId);
|
||||
} catch {
|
||||
// Use cached data.
|
||||
}
|
||||
}
|
||||
|
||||
export async function getFriendList(bridge: Bridge, qqInfo: QQInfo): Promise<JsonObject[]> {
|
||||
try {
|
||||
const friends = await bridge.fetchFriendList();
|
||||
return friends.map(f => ({
|
||||
user_id: f.uin as any,
|
||||
nickname: f.nickname as any,
|
||||
remark: f.remark as any,
|
||||
}));
|
||||
} catch {
|
||||
return qqInfo.friends.map(f => ({
|
||||
user_id: f.uin as any,
|
||||
nickname: f.nickname as any,
|
||||
remark: f.remark as any,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
export async function getGroupList(
|
||||
bridge: Bridge,
|
||||
qqInfo: QQInfo,
|
||||
noCache?: boolean,
|
||||
): Promise<JsonObject[]> {
|
||||
try {
|
||||
if (noCache || qqInfo.groups.length === 0) {
|
||||
await bridge.fetchGroupList();
|
||||
}
|
||||
} catch {
|
||||
// Use cached data.
|
||||
}
|
||||
return qqInfo.groups.map(g => ({
|
||||
group_id: g.groupId as any,
|
||||
group_name: g.groupName as any,
|
||||
member_count: g.memberCount as any,
|
||||
max_member_count: g.memberMax as any,
|
||||
}));
|
||||
}
|
||||
|
||||
export async function getGroupInfo(
|
||||
bridge: Bridge,
|
||||
qqInfo: QQInfo,
|
||||
groupId: number,
|
||||
noCache?: boolean,
|
||||
): Promise<JsonObject | null> {
|
||||
if (noCache || !qqInfo.findGroup(groupId)) {
|
||||
try {
|
||||
await bridge.fetchGroupList();
|
||||
} catch {
|
||||
// Use cached data.
|
||||
}
|
||||
}
|
||||
const g = qqInfo.findGroup(groupId);
|
||||
if (!g) return null;
|
||||
return {
|
||||
group_id: g.groupId as any,
|
||||
group_name: g.groupName as any,
|
||||
member_count: g.memberCount as any,
|
||||
max_member_count: g.memberMax as any,
|
||||
};
|
||||
}
|
||||
|
||||
export async function getGroupMemberList(
|
||||
bridge: Bridge,
|
||||
qqInfo: QQInfo,
|
||||
groupId: number,
|
||||
noCache?: boolean,
|
||||
): Promise<JsonObject[]> {
|
||||
if (noCache) {
|
||||
await refreshSingleGroupMembers(bridge, groupId);
|
||||
return getCachedGroupMembers(qqInfo, groupId);
|
||||
}
|
||||
|
||||
try {
|
||||
const members = await bridge.fetchGroupMemberList(groupId);
|
||||
return members.map(m => formatGroupMember(groupId, m));
|
||||
} catch {
|
||||
return getCachedGroupMembers(qqInfo, groupId);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getGroupMemberInfo(
|
||||
bridge: Bridge,
|
||||
qqInfo: QQInfo,
|
||||
groupId: number,
|
||||
userId: number,
|
||||
noCache?: boolean,
|
||||
): Promise<JsonObject | null> {
|
||||
if (noCache || !qqInfo.findGroupMember(groupId, userId)) {
|
||||
await refreshSingleGroupMembers(bridge, groupId);
|
||||
}
|
||||
const m = qqInfo.findGroupMember(groupId, userId);
|
||||
if (!m) return null;
|
||||
return formatGroupMember(groupId, m);
|
||||
}
|
||||
|
||||
export async function getGroupFiles(
|
||||
bridge: Bridge,
|
||||
groupId: number,
|
||||
folderId?: string,
|
||||
): Promise<JsonObject> {
|
||||
const result = await bridge.fetchGroupFiles(groupId, folderId ?? '/');
|
||||
return {
|
||||
files: result.files.map((file) => ({
|
||||
group_id: groupId as any,
|
||||
file_id: file.fileId as any,
|
||||
file_name: file.fileName as any,
|
||||
busid: file.busId as any,
|
||||
file_size: file.fileSize as any,
|
||||
upload_time: file.uploadTime as any,
|
||||
dead_time: file.deadTime as any,
|
||||
modify_time: file.modifyTime as any,
|
||||
download_times: file.downloadTimes as any,
|
||||
uploader: file.uploader as any,
|
||||
uploader_name: file.uploaderName as any,
|
||||
} as JsonObject)) as any,
|
||||
folders: result.folders.map((folder) => ({
|
||||
group_id: groupId as any,
|
||||
folder_id: folder.folderId as any,
|
||||
folder_name: folder.folderName as any,
|
||||
create_time: folder.createTime as any,
|
||||
creator: folder.creator as any,
|
||||
create_name: folder.creatorName as any,
|
||||
total_file_count: folder.totalFileCount as any,
|
||||
} as JsonObject)) as any,
|
||||
};
|
||||
}
|
||||
|
||||
export async function getStrangerInfo(
|
||||
bridge: Bridge,
|
||||
qqInfo: QQInfo,
|
||||
userId: number,
|
||||
): Promise<JsonObject | null> {
|
||||
try {
|
||||
const p = await bridge.fetchUserProfile(userId);
|
||||
return {
|
||||
user_id: p.uin as any,
|
||||
nickname: p.nickname as any,
|
||||
sex: p.sex as any,
|
||||
age: p.age as any,
|
||||
};
|
||||
} catch {
|
||||
const p = qqInfo.findUserProfile(userId);
|
||||
if (!p) return null;
|
||||
return {
|
||||
user_id: p.uin as any,
|
||||
nickname: p.nickname as any,
|
||||
sex: p.sex as any,
|
||||
age: p.age as any,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export async function getGroupSystemMessages(bridge: Bridge): Promise<JsonObject[]> {
|
||||
try {
|
||||
const reqs = await bridge.fetchGroupRequests();
|
||||
return reqs.map(r => ({
|
||||
group_id: r.groupId,
|
||||
group_name: r.groupName,
|
||||
request_id: r.sequence,
|
||||
requester_uin: r.targetUin,
|
||||
requester_nick: r.targetName,
|
||||
message: r.comment,
|
||||
flag: `${r.eventType}:${r.groupId}:${r.targetUid}`,
|
||||
} as JsonObject));
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export async function getDownloadRKeys(bridge: Bridge): Promise<JsonObject[]> {
|
||||
try {
|
||||
const rkeys = await bridge.fetchDownloadRKeys();
|
||||
return rkeys.map(r => ({
|
||||
rkey: r.rkey,
|
||||
type: r.type,
|
||||
ttl: r.ttlSeconds,
|
||||
create_time: r.createTime,
|
||||
} as JsonObject));
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function getCachedGroupMembers(qqInfo: QQInfo, groupId: number): JsonObject[] {
|
||||
const g = qqInfo.findGroup(groupId);
|
||||
if (!g) return [];
|
||||
const result: JsonObject[] = [];
|
||||
for (const [, member] of g.members) {
|
||||
result.push(formatGroupMember(groupId, member));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function formatGroupMember(
|
||||
groupId: number,
|
||||
member: {
|
||||
uin: number;
|
||||
nickname: string;
|
||||
card: string;
|
||||
joinTime: number;
|
||||
lastSentTime: number;
|
||||
level: number;
|
||||
role: string;
|
||||
title: string;
|
||||
},
|
||||
): JsonObject {
|
||||
return {
|
||||
group_id: groupId as any,
|
||||
user_id: member.uin as any,
|
||||
nickname: member.nickname as any,
|
||||
card: member.card as any,
|
||||
sex: 'unknown' as any,
|
||||
age: 0 as any,
|
||||
join_time: member.joinTime as any,
|
||||
last_sent_time: member.lastSentTime as any,
|
||||
level: String(member.level) as any,
|
||||
role: member.role as any,
|
||||
title: member.title as any,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import type { Bridge } from '../../bridge/bridge';
|
||||
import { createLogger } from '../../utils/logger';
|
||||
import type { MediaStore } from '../media-store';
|
||||
import type { JsonObject } from '../types';
|
||||
|
||||
const log = createLogger('OneBot');
|
||||
|
||||
export async function getImageInfo(
|
||||
mediaStore: MediaStore,
|
||||
file: string,
|
||||
): Promise<JsonObject | null> {
|
||||
const cached = mediaStore.findImage(file);
|
||||
if (!cached) return null;
|
||||
const url = cached.url || cached.imageUrl || '';
|
||||
return {
|
||||
file: url || cached.file,
|
||||
url,
|
||||
file_size: String(cached.fileSize ?? 0),
|
||||
file_name: cached.fileName || cached.file,
|
||||
};
|
||||
}
|
||||
|
||||
export async function getRecordInfo(
|
||||
bridge: Bridge,
|
||||
mediaStore: MediaStore,
|
||||
file: string,
|
||||
): Promise<JsonObject | null> {
|
||||
const cached = mediaStore.findRecord(file);
|
||||
if (!cached) return null;
|
||||
|
||||
// Re-resolve via OIDB if the cached URL is missing or empty.
|
||||
// Mirrors NapCat's getPttUrl path: GetGroupPttUrl / GetPttUrl by fileUuid.
|
||||
let url = cached.url;
|
||||
if (!url && cached.mediaNode) {
|
||||
try {
|
||||
url = cached.isGroup
|
||||
? await bridge.fetchGroupPttUrlByNode(cached.sessionId, cached.mediaNode)
|
||||
: await bridge.fetchPrivatePttUrlByNode(cached.mediaNode);
|
||||
if (url) {
|
||||
mediaStore.updateRecordUrl(file, url);
|
||||
}
|
||||
} catch (err) {
|
||||
log.warn('get_record url refetch failed: %s', err instanceof Error ? err.message : String(err));
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
file: url || cached.file,
|
||||
url: url || '',
|
||||
file_size: String(cached.fileSize ?? 0),
|
||||
file_name: cached.fileName || cached.file,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,399 @@
|
||||
import type { Bridge } from '../../bridge/bridge';
|
||||
import type { ForwardNodePayload, MessageElement } from '../../bridge/events';
|
||||
import { createLogger } from '../../utils/logger';
|
||||
import type { MessageSendResult } from '../api-handler';
|
||||
import { elementsToOneBotSegments } from '../event-converter';
|
||||
import { GROUP_MESSAGE_EVENT, PRIVATE_MESSAGE_EVENT, hashMessageIdInt32 } from '../message-id';
|
||||
import { parseMessage } from '../message-parser';
|
||||
import type { MessageStore } from '../message-store';
|
||||
import type { JsonObject, JsonValue, MessageMeta } from '../types';
|
||||
import type { OneBotInstanceContext } from '../instance-context';
|
||||
|
||||
const log = createLogger('OneBot');
|
||||
|
||||
export async function getGroupMsgHistory(
|
||||
messageStore: MessageStore,
|
||||
groupId: number,
|
||||
messageId?: number,
|
||||
count?: number,
|
||||
): Promise<JsonObject[]> {
|
||||
if (!Number.isInteger(groupId) || groupId <= 0) return [];
|
||||
const limit = normalizeHistoryCount(count);
|
||||
|
||||
let anchorSequence: number | undefined;
|
||||
if (Number.isInteger(messageId) && messageId !== 0) {
|
||||
const meta = messageStore.findMeta(messageId as number);
|
||||
if (!meta || !meta.isGroup || meta.targetId !== groupId || meta.sequence <= 0) return [];
|
||||
anchorSequence = meta.sequence;
|
||||
}
|
||||
|
||||
const events = messageStore.listSessionEvents(true, groupId, limit, anchorSequence);
|
||||
return events
|
||||
.filter((event) => {
|
||||
if (event.message_type !== 'group') return false;
|
||||
const gid = Number(event.group_id ?? 0);
|
||||
return Number.isFinite(gid) && Math.trunc(gid) === groupId;
|
||||
})
|
||||
.map(sanitizeMessageEventForApi);
|
||||
}
|
||||
|
||||
export async function getFriendMsgHistory(
|
||||
messageStore: MessageStore,
|
||||
userId: number,
|
||||
messageId?: number,
|
||||
count?: number,
|
||||
): Promise<JsonObject[]> {
|
||||
if (!Number.isInteger(userId) || userId <= 0) return [];
|
||||
const limit = normalizeHistoryCount(count);
|
||||
|
||||
let anchorSequence: number | undefined;
|
||||
if (Number.isInteger(messageId) && messageId !== 0) {
|
||||
const meta = messageStore.findMeta(messageId as number);
|
||||
if (!meta || meta.isGroup || meta.targetId !== userId || meta.sequence <= 0) return [];
|
||||
anchorSequence = meta.sequence;
|
||||
}
|
||||
|
||||
const events = messageStore.listSessionEvents(false, userId, limit, anchorSequence);
|
||||
return events
|
||||
.filter((event) => {
|
||||
if (event.message_type !== 'private') return false;
|
||||
const uid = Number(event.user_id ?? 0);
|
||||
return Number.isFinite(uid) && Math.trunc(uid) === userId;
|
||||
})
|
||||
.map(sanitizeMessageEventForApi);
|
||||
}
|
||||
|
||||
export async function deleteMessage(bridge: Bridge, meta: MessageMeta): Promise<void> {
|
||||
if (meta.isGroup) {
|
||||
await bridge.recallGroupMessage(meta.targetId, meta.sequence);
|
||||
} else {
|
||||
await bridge.recallPrivateMessage(
|
||||
meta.targetId,
|
||||
meta.clientSequence,
|
||||
meta.sequence,
|
||||
meta.random,
|
||||
meta.timestamp,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function setEssenceMessage(
|
||||
bridge: Bridge,
|
||||
messageStore: MessageStore,
|
||||
messageId: number,
|
||||
enable: boolean,
|
||||
): Promise<void> {
|
||||
const meta = messageStore.findMeta(messageId);
|
||||
if (!meta || !meta.isGroup) throw new Error('message not found or not a group message');
|
||||
await bridge.setGroupEssence(meta.targetId, meta.sequence, meta.random, enable);
|
||||
}
|
||||
|
||||
export async function sendPrivateMessage(
|
||||
ref: OneBotInstanceContext,
|
||||
userId: number,
|
||||
message: JsonValue,
|
||||
autoEscape: boolean,
|
||||
): Promise<MessageSendResult> {
|
||||
const elements = await parseMessage(message, autoEscape, {
|
||||
resolveReplySequence: (replyMessageId) => {
|
||||
return ref.messageStore.resolveReplySequence(false, userId, replyMessageId);
|
||||
},
|
||||
resolveReplyMeta: (replyMessageId) => {
|
||||
const meta = ref.messageStore.findMeta(replyMessageId);
|
||||
if (meta) {
|
||||
return {
|
||||
senderUin: meta.targetId,
|
||||
time: meta.timestamp,
|
||||
random: meta.random,
|
||||
};
|
||||
}
|
||||
return null;
|
||||
},
|
||||
resolveMentionUid: (targetUin) => ref.bridge.resolveUserUid(targetUin),
|
||||
musicSignUrl: ref.musicSignUrl,
|
||||
});
|
||||
if (elements.length === 0) throw new Error('message is empty');
|
||||
|
||||
const receipt = await ref.bridge.sendPrivateMessage(userId, elements);
|
||||
const messageId = hashMessageIdInt32(receipt.sequence, userId, PRIVATE_MESSAGE_EVENT);
|
||||
|
||||
logSentMessage(false, userId, elements);
|
||||
|
||||
ref.cacheMessageMeta(messageId, {
|
||||
isGroup: false,
|
||||
targetId: userId,
|
||||
sequence: receipt.sequence,
|
||||
eventName: PRIVATE_MESSAGE_EVENT,
|
||||
clientSequence: receipt.clientSequence,
|
||||
random: receipt.random,
|
||||
timestamp: receipt.timestamp,
|
||||
});
|
||||
|
||||
return { messageId };
|
||||
}
|
||||
|
||||
export async function sendGroupMessage(
|
||||
ref: OneBotInstanceContext,
|
||||
groupId: number,
|
||||
message: JsonValue,
|
||||
autoEscape: boolean,
|
||||
): Promise<MessageSendResult> {
|
||||
const elements = await parseMessage(message, autoEscape, {
|
||||
resolveReplySequence: (replyMessageId) => {
|
||||
return ref.messageStore.resolveReplySequence(true, groupId, replyMessageId);
|
||||
},
|
||||
resolveReplyMeta: (replyMessageId) => {
|
||||
const event = ref.messageStore.findEvent(replyMessageId);
|
||||
if (event) {
|
||||
return {
|
||||
senderUin: typeof event.user_id === 'number'
|
||||
? event.user_id
|
||||
: parseInt(String(event.user_id || '0'), 10),
|
||||
time: typeof event.time === 'number'
|
||||
? event.time
|
||||
: parseInt(String(event.time || '0'), 10),
|
||||
random: 0,
|
||||
};
|
||||
}
|
||||
return null;
|
||||
},
|
||||
resolveMentionUid: (targetUin) => ref.bridge.resolveUserUid(targetUin, groupId),
|
||||
musicSignUrl: ref.musicSignUrl,
|
||||
});
|
||||
if (elements.length === 0) throw new Error('message is empty');
|
||||
|
||||
const receipt = await ref.bridge.sendGroupMessage(groupId, elements);
|
||||
const messageId = hashMessageIdInt32(receipt.sequence, groupId, GROUP_MESSAGE_EVENT);
|
||||
|
||||
logSentMessage(true, groupId, elements);
|
||||
|
||||
ref.cacheMessageMeta(messageId, {
|
||||
isGroup: true,
|
||||
targetId: groupId,
|
||||
sequence: receipt.sequence,
|
||||
eventName: GROUP_MESSAGE_EVENT,
|
||||
clientSequence: receipt.clientSequence,
|
||||
random: receipt.random,
|
||||
timestamp: receipt.timestamp,
|
||||
});
|
||||
|
||||
return { messageId };
|
||||
}
|
||||
|
||||
export async function sendGroupForwardMessage(
|
||||
ref: OneBotInstanceContext,
|
||||
groupId: number,
|
||||
messages: JsonValue,
|
||||
): Promise<{ messageId: number; forwardId: string }> {
|
||||
const nodes = await parseForwardNodes(ref, messages);
|
||||
const forwardId = await ref.bridge.uploadForwardNodes(nodes, groupId);
|
||||
const receipt = await ref.bridge.sendGroupMessage(groupId, [{ type: 'forward', resId: forwardId }]);
|
||||
const messageId = hashMessageIdInt32(receipt.sequence, groupId, GROUP_MESSAGE_EVENT);
|
||||
|
||||
ref.cacheMessageMeta(messageId, {
|
||||
isGroup: true,
|
||||
targetId: groupId,
|
||||
sequence: receipt.sequence,
|
||||
eventName: GROUP_MESSAGE_EVENT,
|
||||
clientSequence: receipt.clientSequence,
|
||||
random: receipt.random,
|
||||
timestamp: receipt.timestamp,
|
||||
});
|
||||
|
||||
return { messageId, forwardId };
|
||||
}
|
||||
|
||||
export async function sendPrivateForwardMessage(
|
||||
ref: OneBotInstanceContext,
|
||||
userId: number,
|
||||
messages: JsonValue,
|
||||
): Promise<{ messageId: number; forwardId: string }> {
|
||||
const nodes = await parseForwardNodes(ref, messages);
|
||||
const forwardId = await ref.bridge.uploadForwardNodes(nodes);
|
||||
const receipt = await ref.bridge.sendPrivateMessage(userId, [{ type: 'forward', resId: forwardId }]);
|
||||
const messageId = hashMessageIdInt32(receipt.sequence, userId, PRIVATE_MESSAGE_EVENT);
|
||||
|
||||
ref.cacheMessageMeta(messageId, {
|
||||
isGroup: false,
|
||||
targetId: userId,
|
||||
sequence: receipt.sequence,
|
||||
eventName: PRIVATE_MESSAGE_EVENT,
|
||||
clientSequence: receipt.clientSequence,
|
||||
random: receipt.random,
|
||||
timestamp: receipt.timestamp,
|
||||
});
|
||||
|
||||
return { messageId, forwardId };
|
||||
}
|
||||
|
||||
export async function uploadForwardMessage(
|
||||
ref: OneBotInstanceContext,
|
||||
messages: JsonValue,
|
||||
): Promise<{ forwardId: string }> {
|
||||
const nodes = await parseForwardNodes(ref, messages);
|
||||
const forwardId = await ref.bridge.uploadForwardNodes(nodes);
|
||||
return { forwardId };
|
||||
}
|
||||
|
||||
export async function getForwardMessage(
|
||||
ref: OneBotInstanceContext,
|
||||
resId: string,
|
||||
): Promise<JsonObject[]> {
|
||||
const nodes = await ref.bridge.fetchForwardNodes(resId);
|
||||
const results: JsonObject[] = [];
|
||||
for (const node of nodes) {
|
||||
results.push({
|
||||
type: 'node' as any,
|
||||
data: {
|
||||
user_id: node.userUin as any,
|
||||
nickname: node.nickname as any,
|
||||
uin: String(node.userUin) as any,
|
||||
name: node.nickname as any,
|
||||
content: await elementsToOneBotSegments(node.elements, false, node.userUin) as any,
|
||||
} as any,
|
||||
} as JsonObject);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
function normalizeHistoryCount(count?: number): number {
|
||||
if (!Number.isFinite(count)) return 20;
|
||||
const n = Math.trunc(count as number);
|
||||
if (n <= 0) return 20;
|
||||
if (n > 200) return 200;
|
||||
return n;
|
||||
}
|
||||
|
||||
function sanitizeMessageEventForApi(event: JsonObject): JsonObject {
|
||||
const result: JsonObject = { ...event };
|
||||
delete result.post_type;
|
||||
delete result.self_id;
|
||||
result.real_id = (result.message_id ?? 0) as JsonValue;
|
||||
return result;
|
||||
}
|
||||
|
||||
function logSentMessage(isGroup: boolean, targetId: number, elements: MessageElement[]): void {
|
||||
const type = isGroup ? '群聊' : '私聊';
|
||||
const parts: string[] = [];
|
||||
|
||||
const replyElem = elements.find(e => e.type === 'reply');
|
||||
if (replyElem?.replyMessageId) {
|
||||
parts.push(`[回复:${replyElem.replyMessageId}]`);
|
||||
}
|
||||
|
||||
for (const elem of elements) {
|
||||
if (elem.type === 'reply') continue;
|
||||
|
||||
switch (elem.type) {
|
||||
case 'text':
|
||||
if (elem.text) {
|
||||
const preview = elem.text.length > 50 ? `${elem.text.substring(0, 50)}...` : elem.text;
|
||||
parts.push(preview);
|
||||
}
|
||||
break;
|
||||
case 'image':
|
||||
parts.push('[图片]');
|
||||
break;
|
||||
case 'face':
|
||||
parts.push('[表情]');
|
||||
break;
|
||||
case 'at':
|
||||
if (elem.text) parts.push(elem.text.trim());
|
||||
break;
|
||||
case 'record':
|
||||
parts.push('[语音]');
|
||||
break;
|
||||
case 'video':
|
||||
parts.push('[视频]');
|
||||
break;
|
||||
case 'json':
|
||||
parts.push('[JSON消息]');
|
||||
break;
|
||||
case 'xml':
|
||||
parts.push('[XML消息]');
|
||||
break;
|
||||
case 'markdown':
|
||||
parts.push('[Markdown]');
|
||||
break;
|
||||
case 'forward':
|
||||
parts.push('[转发消息]');
|
||||
break;
|
||||
case 'poke':
|
||||
parts.push('[戳一戳]');
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const content = parts.join(' ').trim() || '[空消息]';
|
||||
log.info(`${type} ${targetId} | 发送:${content}`);
|
||||
}
|
||||
|
||||
async function parseForwardNodes(
|
||||
ref: OneBotInstanceContext,
|
||||
messages: JsonValue,
|
||||
): Promise<ForwardNodePayload[]> {
|
||||
if (!Array.isArray(messages)) {
|
||||
throw new Error('forward messages must be an array');
|
||||
}
|
||||
|
||||
const nodes: ForwardNodePayload[] = [];
|
||||
for (const item of messages) {
|
||||
const segment = asJsonObject(item);
|
||||
if (!segment) continue;
|
||||
|
||||
let nodeData: JsonObject | null = null;
|
||||
if (String(segment.type ?? '') === 'node') {
|
||||
nodeData = asJsonObject(segment.data);
|
||||
} else if (segment.content !== undefined || segment.message !== undefined) {
|
||||
nodeData = segment;
|
||||
}
|
||||
if (!nodeData) continue;
|
||||
|
||||
const messageId = toPositiveInt(nodeData.id ?? nodeData.message_id);
|
||||
if (messageId > 0) {
|
||||
const event = ref.messageStore.findEvent(messageId);
|
||||
if (!event) throw new Error(`forward node message_id not found: ${messageId}`);
|
||||
|
||||
const eventSender = asJsonObject(event.sender) ?? {};
|
||||
const nickname = String(eventSender.card ?? eventSender.nickname ?? nodeData.nickname ?? nodeData.name ?? '');
|
||||
const userUin = toPositiveInt(event.user_id);
|
||||
const content = (event.message ?? event.raw_message ?? '') as JsonValue;
|
||||
const elements = await parseMessage(content, false);
|
||||
if (userUin > 0 && elements.length > 0) {
|
||||
nodes.push({ userUin, nickname: nickname || String(userUin), elements });
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
const userUin = toPositiveInt(nodeData.user_id ?? nodeData.uin);
|
||||
if (userUin <= 0) throw new Error('forward node user_id/uin is required');
|
||||
|
||||
const nickname = String(nodeData.nickname ?? nodeData.name ?? userUin);
|
||||
const content = (nodeData.content ?? nodeData.message ?? '') as JsonValue;
|
||||
const elements = await parseMessage(content, false);
|
||||
if (elements.length === 0) throw new Error(`forward node content is empty: ${userUin}`);
|
||||
|
||||
nodes.push({ userUin, nickname, elements });
|
||||
}
|
||||
|
||||
if (nodes.length === 0) {
|
||||
throw new Error('forward node list is empty');
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
function asJsonObject(value: JsonValue | undefined): JsonObject | null {
|
||||
if (typeof value !== 'object' || value === null || Array.isArray(value)) return null;
|
||||
return value as JsonObject;
|
||||
}
|
||||
|
||||
function toPositiveInt(value: JsonValue | undefined): number {
|
||||
if (typeof value === 'number' && Number.isFinite(value)) return Math.max(0, Math.trunc(value));
|
||||
if (typeof value === 'string' && value.trim()) {
|
||||
const parsed = Number(value);
|
||||
if (Number.isFinite(parsed)) return Math.max(0, Math.trunc(parsed));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import type { Bridge } from '../../bridge/bridge';
|
||||
|
||||
export async function handleGroupAddRequest(
|
||||
bridge: Bridge,
|
||||
flag: string,
|
||||
approve: boolean,
|
||||
reason: string,
|
||||
): Promise<void> {
|
||||
// flag format: "add:groupId:uid" or "invite:groupId:uid" (from event-converter)
|
||||
const parts = flag.split(':');
|
||||
if (parts.length < 3) throw new Error('invalid group request flag');
|
||||
const groupId = parseInt(parts[1], 10);
|
||||
if (!groupId) throw new Error('invalid group_id in flag');
|
||||
|
||||
const requests = await bridge.fetchGroupRequests();
|
||||
const matching = requests.find(r => r.groupId === groupId);
|
||||
if (!matching) {
|
||||
throw new Error('matching group request not found');
|
||||
}
|
||||
|
||||
await bridge.setGroupAddRequest(
|
||||
groupId,
|
||||
matching.sequence,
|
||||
matching.eventType,
|
||||
approve,
|
||||
reason,
|
||||
matching.filtered,
|
||||
);
|
||||
}
|
||||
@@ -21,8 +21,11 @@ export const SNOWLUMA_ACTIONS = [
|
||||
'get_group_honor_info',
|
||||
'get_group_system_msg',
|
||||
'set_group_kick',
|
||||
'set_group_kick_members',
|
||||
'set_group_ban',
|
||||
'set_group_whole_ban',
|
||||
'set_group_add_option',
|
||||
'set_group_search',
|
||||
'set_group_admin',
|
||||
'set_group_card',
|
||||
'set_group_name',
|
||||
@@ -78,6 +81,7 @@ export const SNOWLUMA_ACTIONS = [
|
||||
'clean_cache',
|
||||
'.handle_quick_operation',
|
||||
'set_friend_remark',
|
||||
'set_group_remark',
|
||||
'set_msg_emoji_like',
|
||||
'_mark_all_as_read',
|
||||
'get_group_file_system_info',
|
||||
@@ -91,6 +95,9 @@ export const SNOWLUMA_ACTIONS = [
|
||||
'forward_group_single_msg',
|
||||
'get_recent_contact',
|
||||
'get_profile_like',
|
||||
'fetch_custom_face',
|
||||
'get_emoji_likes',
|
||||
'fetch_emoji_like',
|
||||
'get_friends_with_category',
|
||||
'get_online_clients',
|
||||
'_get_model_show',
|
||||
|
||||
@@ -6,8 +6,11 @@ import type {
|
||||
DeleteGroupFileParams,
|
||||
DomainParams,
|
||||
DownloadFileParams,
|
||||
FetchCustomFaceParams,
|
||||
FetchEmojiLikeParams,
|
||||
ForwardMessageParams,
|
||||
FriendPokeParams,
|
||||
GetEmojiLikesParams,
|
||||
GetForwardMsgParams,
|
||||
GetFriendMessageHistoryParams,
|
||||
GetGroupFilesParams,
|
||||
@@ -39,11 +42,14 @@ import type {
|
||||
SetFriendAddRequestParams,
|
||||
SetFriendRemarkParams,
|
||||
SetGroupAddRequestParams,
|
||||
SetGroupAddOptionParams,
|
||||
SetGroupAdminParams,
|
||||
SetGroupBanParams,
|
||||
SetGroupCardParams,
|
||||
SetGroupKickParams,
|
||||
SetGroupKickMembersParams,
|
||||
SetGroupNameParams,
|
||||
SetGroupRemarkParams,
|
||||
SetGroupReactionParams,
|
||||
SetGroupSpecialTitleParams,
|
||||
SetGroupWholeBanParams,
|
||||
@@ -103,8 +109,11 @@ export interface SnowLumaActionMap {
|
||||
get_group_honor_info: { params: GetGroupHonorInfoParams; data: JsonValue };
|
||||
get_group_system_msg: { params: JsonObject; data: JsonObject[] };
|
||||
set_group_kick: { params: SetGroupKickParams; data: EmptyData };
|
||||
set_group_kick_members: { params: SetGroupKickMembersParams; data: EmptyData };
|
||||
set_group_ban: { params: SetGroupBanParams; data: EmptyData };
|
||||
set_group_whole_ban: { params: SetGroupWholeBanParams; data: EmptyData };
|
||||
set_group_add_option: { params: SetGroupAddOptionParams; data: EmptyData };
|
||||
set_group_search: { params: GroupIdParams; data: EmptyData };
|
||||
set_group_admin: { params: SetGroupAdminParams; data: EmptyData };
|
||||
set_group_card: { params: SetGroupCardParams; data: EmptyData };
|
||||
set_group_name: { params: SetGroupNameParams; data: EmptyData };
|
||||
@@ -160,6 +169,7 @@ export interface SnowLumaActionMap {
|
||||
clean_cache: { params: JsonObject; data: EmptyData };
|
||||
'.handle_quick_operation': { params: QuickOperationParams; data: EmptyData };
|
||||
set_friend_remark: { params: SetFriendRemarkParams; data: EmptyData };
|
||||
set_group_remark: { params: SetGroupRemarkParams; data: EmptyData };
|
||||
set_msg_emoji_like: { params: SetMsgEmojiLikeParams; data: EmptyData };
|
||||
_mark_all_as_read: { params: JsonObject; data: EmptyData };
|
||||
get_group_file_system_info: { params: GroupIdParams; data: GroupFileSystemInfo };
|
||||
@@ -173,6 +183,9 @@ export interface SnowLumaActionMap {
|
||||
forward_group_single_msg: { params: JsonObject; data: JsonValue };
|
||||
get_recent_contact: { params: JsonObject; data: JsonArray };
|
||||
get_profile_like: { params: JsonObject; data: JsonObject };
|
||||
fetch_custom_face: { params: FetchCustomFaceParams; data: string[] };
|
||||
get_emoji_likes: { params: GetEmojiLikesParams; data: JsonObject };
|
||||
fetch_emoji_like: { params: FetchEmojiLikeParams; data: JsonObject };
|
||||
get_friends_with_category: { params: JsonObject; data: JsonObject[] };
|
||||
get_online_clients: { params: JsonObject; data: OnlineClientsInfo };
|
||||
_get_model_show: { params: JsonObject; data: { variants: JsonArray } };
|
||||
|
||||
@@ -64,6 +64,11 @@ export interface SetGroupKickParams extends GroupIdParams {
|
||||
reject_add_request?: boolean;
|
||||
}
|
||||
|
||||
export interface SetGroupKickMembersParams extends GroupIdParams {
|
||||
user_id: number[];
|
||||
reject_add_request?: boolean;
|
||||
}
|
||||
|
||||
export interface SetGroupBanParams extends GroupIdParams {
|
||||
user_id: number;
|
||||
duration?: number;
|
||||
@@ -73,6 +78,10 @@ export interface SetGroupWholeBanParams extends GroupIdParams {
|
||||
enable?: boolean;
|
||||
}
|
||||
|
||||
export interface SetGroupAddOptionParams extends GroupIdParams {
|
||||
add_type: number;
|
||||
}
|
||||
|
||||
export interface SetGroupAdminParams extends GroupIdParams {
|
||||
user_id: number;
|
||||
enable?: boolean;
|
||||
@@ -254,12 +263,31 @@ export interface SetFriendRemarkParams extends UserIdParams {
|
||||
remark: string;
|
||||
}
|
||||
|
||||
export interface SetGroupRemarkParams extends GroupIdParams {
|
||||
remark: string;
|
||||
}
|
||||
|
||||
export interface SetMsgEmojiLikeParams extends JsonObject {
|
||||
message_id: number;
|
||||
emoji_id: string;
|
||||
set?: boolean;
|
||||
}
|
||||
|
||||
export interface FetchCustomFaceParams extends JsonObject {
|
||||
count?: number;
|
||||
}
|
||||
|
||||
export interface GetEmojiLikesParams extends MessageIdParams {
|
||||
emoji_id: string;
|
||||
}
|
||||
|
||||
export interface FetchEmojiLikeParams extends MessageIdParams {
|
||||
emojiId: string;
|
||||
emojiType?: number;
|
||||
count?: number;
|
||||
cookie?: string;
|
||||
}
|
||||
|
||||
export interface DownloadFileParams extends JsonObject {
|
||||
url?: string;
|
||||
base64?: string;
|
||||
|
||||
Reference in new issue
Block a user