chore Api

This commit is contained in:
手瓜一十雪 2024-08-09 18:17:45 +08:00
parent fbe2d78331
commit 173f83808e
5 changed files with 40 additions and 45 deletions

View File

@ -19,8 +19,11 @@ export default class SetGroupBan extends BaseAction<Payload, null> {
PayloadSchema = SchemaData; PayloadSchema = SchemaData;
protected async _handle(payload: Payload): Promise<null> { protected async _handle(payload: Payload): Promise<null> {
const NTQQGroupApi = this.CoreContext.getApiContext().GroupApi; const NTQQGroupApi = this.CoreContext.getApiContext().GroupApi;
const NTQQUserApi = this.CoreContext.getApiContext().UserApi;
const uid = await NTQQUserApi.getUidByUin(payload.user_id.toString());
if(!uid) throw new Error('uid error');
await NTQQGroupApi.banMember(payload.group_id.toString(), await NTQQGroupApi.banMember(payload.group_id.toString(),
[{ uid: member.uid, timeStamp: parseInt(payload.duration.toString()) }]); [{ uid: uid, timeStamp: parseInt(payload.duration.toString()) }]);
return null; return null;
} }
} }

View File

@ -17,6 +17,7 @@ type PlayloadType = FromSchema<typeof SchemaData>;
class MarkMsgAsRead extends BaseAction<PlayloadType, null> { class MarkMsgAsRead extends BaseAction<PlayloadType, null> {
async getPeer(payload: PlayloadType): Promise<Peer> { async getPeer(payload: PlayloadType): Promise<Peer> {
const NTQQUserApi = this.CoreContext.getApiContext().UserApi; const NTQQUserApi = this.CoreContext.getApiContext().UserApi;
const NTQQFriendApi = this.CoreContext.getApiContext().FriendApi;
if (payload.user_id) { if (payload.user_id) {
const peerUid = await NTQQUserApi.getUidByUin(payload.user_id.toString()); const peerUid = await NTQQUserApi.getUidByUin(payload.user_id.toString());
if (!peerUid) { if (!peerUid) {

View File

@ -1,21 +1,20 @@
import BaseAction from '@/onebot11/action/BaseAction';
import { import {
OB11MessageData, OB11MessageData,
OB11MessageDataType, OB11MessageDataType,
OB11MessageMixType, OB11MessageMixType,
OB11MessageNode, OB11MessageNode,
OB11PostSendMsg OB11PostSendMsg
} from '@/onebot11/types'; } from '@/onebot/types';
import { ActionName, BaseCheckResult } from '@/onebot11/action/types'; import { ActionName, BaseCheckResult } from '@/onebot/action/types';
import { getGroup } from '@/core/data';
import { ChatType, ElementType, Group, NTQQFileApi, NTQQFriendApi, NTQQMsgApi, NTQQUserApi, Peer, SendMessageElement, } from '@/core';
import fs from 'node:fs'; import fs from 'node:fs';
import fsPromise from 'node:fs/promises'; import fsPromise from 'node:fs/promises';
import { logDebug, logError } from '@/common/utils/log'; import { decodeCQCode } from '@/onebot/helper/cqcode';
import { decodeCQCode } from '@/onebot11/cqcode';
import createSendElements from './create-send-elements'; import createSendElements from './create-send-elements';
import { handleForwardNode } from '@/onebot11/action/msg/SendMsg/handle-forward-node';
import { MessageUnique } from '@/common/utils/MessageUnique'; import { MessageUnique } from '@/common/utils/MessageUnique';
import { ChatType, ElementType, NapCatCore, Peer, SendMessageElement } from '@/core';
import BaseAction from '../../BaseAction';
import { handleForwardNode } from './handle-forward-node';
export interface ReturnDataType { export interface ReturnDataType {
message_id: number; message_id: number;
@ -36,7 +35,9 @@ export function normalize(message: OB11MessageMixType, autoEscape = false): OB11
export { createSendElements }; export { createSendElements };
export async function sendMsg(peer: Peer, sendElements: SendMessageElement[], deleteAfterSentFiles: string[], waitComplete = true) { export async function sendMsg(coreContext: NapCatCore, peer: Peer, sendElements: SendMessageElement[], deleteAfterSentFiles: string[], waitComplete = true) {
const NTQQMsgApi = coreContext.getApiContext().MsgApi;
const logger = coreContext.context.logger;
if (!sendElements.length) { if (!sendElements.length) {
throw ('消息体无法解析, 请检查是否发送了不支持的消息类型'); throw ('消息体无法解析, 请检查是否发送了不支持的消息类型');
} }
@ -63,29 +64,31 @@ export async function sendMsg(peer: Peer, sendElements: SendMessageElement[], de
timeout += PredictTime;// 10S Basic Timeout + PredictTime( For File 512kb/s ) timeout += PredictTime;// 10S Basic Timeout + PredictTime( For File 512kb/s )
} }
} catch (e) { } catch (e) {
logError('发送消息计算预计时间异常', e); logger.logError('发送消息计算预计时间异常', e);
} }
const returnMsg = await NTQQMsgApi.sendMsg(peer, sendElements, waitComplete, timeout); const returnMsg = await NTQQMsgApi.sendMsg(peer, sendElements, waitComplete, timeout);
try { try {
returnMsg!.id = await MessageUnique.createMsg({ chatType: peer.chatType, guildId: '', peerUid: peer.peerUid }, returnMsg!.msgId); returnMsg!.id = MessageUnique.createMsg({ chatType: peer.chatType, guildId: '', peerUid: peer.peerUid }, returnMsg!.msgId);
} catch (e: any) { } catch (e: any) {
logDebug('发送消息id获取失败', e); logger.logDebug('发送消息id获取失败', e);
returnMsg!.id = 0; returnMsg!.id = 0;
} }
deleteAfterSentFiles.map((f) => { fsPromise.unlink(f).then().catch(e => logError('发送消息删除文件失败', e)); }); deleteAfterSentFiles.map((f) => { fsPromise.unlink(f).then().catch(e => logger.logError('发送消息删除文件失败', e)); });
return returnMsg; return returnMsg;
} }
async function createContext(payload: OB11PostSendMsg, contextMode: ContextMode): Promise<Peer> { async function createContext(coreContext: NapCatCore, payload: OB11PostSendMsg, contextMode: ContextMode): Promise<Peer> {
// This function determines the type of message by the existence of user_id / group_id, // This function determines the type of message by the existence of user_id / group_id,
// not message_type. // not message_type.
// This redundant design of Ob11 here should be blamed. // This redundant design of Ob11 here should be blamed.
const NTQQGroupApi = coreContext.getApiContext().GroupApi;
const NTQQFriendApi = coreContext.getApiContext().FriendApi;
const NTQQUserApi = coreContext.getApiContext().UserApi;
if ((contextMode === ContextMode.Group || contextMode === ContextMode.Normal) && payload.group_id) { if ((contextMode === ContextMode.Group || contextMode === ContextMode.Normal) && payload.group_id) {
const group = (await getGroup(payload.group_id))!; // checked before const group = (await NTQQGroupApi.getGroups()).find(e => e.groupCode == payload.group_id?.toString())
return { return {
chatType: ChatType.group, chatType: ChatType.group,
peerUid: group.groupCode peerUid: payload.group_id.toString()
}; };
} }
if ((contextMode === ContextMode.Private || contextMode === ContextMode.Normal) && payload.user_id) { if ((contextMode === ContextMode.Private || contextMode === ContextMode.Normal) && payload.user_id) {
@ -112,14 +115,17 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
contextMode = ContextMode.Normal; contextMode = ContextMode.Normal;
protected async check(payload: OB11PostSendMsg): Promise<BaseCheckResult> { protected async check(payload: OB11PostSendMsg): Promise<BaseCheckResult> {
const NTQQGroupApi = this.CoreContext.getApiContext().GroupApi;
const NTQQFriendApi = this.CoreContext.getApiContext().FriendApi;
const NTQQUserApi = this.CoreContext.getApiContext().UserApi;
const messages = normalize(payload.message); const messages = normalize(payload.message);
const nodeElementLength = getSpecialMsgNum(payload, OB11MessageDataType.node); const nodeElementLength = getSpecialMsgNum(payload, OB11MessageDataType.node);
if (nodeElementLength > 0 && nodeElementLength != messages.length) { if (nodeElementLength > 0 && nodeElementLength != messages.length) {
return { valid: false, message: '转发消息不能和普通消息混在一起发送,转发需要保证message只有type为node的元素' }; return { valid: false, message: '转发消息不能和普通消息混在一起发送,转发需要保证message只有type为node的元素' };
} }
if (payload.message_type !== 'private' && payload.group_id && !(await getGroup(payload.group_id))) { // if (payload.message_type !== 'private' && payload.group_id && !(await getGroup(payload.group_id))) {
return { valid: false, message: `${payload.group_id}不存在` }; // return { valid: false, message: `群${payload.group_id}不存在` };
} // }
if (payload.user_id && payload.message_type !== 'group') { if (payload.user_id && payload.message_type !== 'group') {
const uid = await NTQQUserApi.getUidByUin(payload.user_id.toString()); const uid = await NTQQUserApi.getUidByUin(payload.user_id.toString());
const isBuddy = await NTQQFriendApi.isBuddy(uid!); const isBuddy = await NTQQFriendApi.isBuddy(uid!);
@ -132,7 +138,7 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
} }
protected async _handle(payload: OB11PostSendMsg): Promise<{ message_id: number }> { protected async _handle(payload: OB11PostSendMsg): Promise<{ message_id: number }> {
const peer = await createContext(payload, this.contextMode); const peer = await createContext(this.CoreContext, payload, this.contextMode);
const messages = normalize( const messages = normalize(
payload.message, payload.message,
@ -156,9 +162,9 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
} }
// log("send msg:", peer, sendElements) // log("send msg:", peer, sendElements)
const { sendElements, deleteAfterSentFiles } = await createSendElements(messages, peer); const { sendElements, deleteAfterSentFiles } = await createSendElements(this.CoreContext, messages, peer);
//console.log(peer, JSON.stringify(sendElements,null,2)); //console.log(peer, JSON.stringify(sendElements,null,2));
const returnMsg = await sendMsg(peer, sendElements, deleteAfterSentFiles); const returnMsg = await sendMsg(this.CoreContext, peer, sendElements, deleteAfterSentFiles);
return { message_id: returnMsg!.id! }; return { message_id: returnMsg!.id! };
} }
} }

View File

@ -1,12 +1,8 @@
import { OB11User } from '../../types'; import { OB11User } from '../../types';
import { OB11Constructor } from '../../constructor'; import { OB11Constructor } from '../../helper/data';
import { friends } from '@/core/data';
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { ActionName } from '../types'; import { ActionName } from '../types';
import { NTQQFriendApi } from '@/core';
import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import { requireMinNTQQBuild } from '@/common/utils/QQBasicInfo';
// no_cache get时传字符串 // no_cache get时传字符串
const SchemaData = { const SchemaData = {
@ -21,20 +17,8 @@ export default class GetFriendList extends BaseAction<Payload, OB11User[]> {
actionName = ActionName.GetFriendList; actionName = ActionName.GetFriendList;
PayloadSchema = SchemaData; PayloadSchema = SchemaData;
protected async _handle(payload: Payload) { protected async _handle(payload: Payload) {
if (requireMinNTQQBuild('26702')) {
//全新逻辑 //全新逻辑
const NTQQFriendApi = this.CoreContext.getApiContext().FriendApi;
return OB11Constructor.friendsV2(await NTQQFriendApi.getBuddyV2(payload?.no_cache === true || payload?.no_cache === 'true')); return OB11Constructor.friendsV2(await NTQQFriendApi.getBuddyV2(payload?.no_cache === true || payload?.no_cache === 'true'));
} }
if (friends.size === 0 || payload?.no_cache === true || payload?.no_cache === 'true') {
const _friends = await NTQQFriendApi.getFriends(true);
// log('强制刷新好友列表,结果: ', _friends)
if (_friends.length > 0) {
friends.clear();
for (const friend of _friends) {
friends.set(friend.uid, friend);
}
}
}
return OB11Constructor.friends(Array.from(friends.values()));
}
} }

View File

@ -2,8 +2,7 @@
import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { ActionName } from '../types'; import { ActionName } from '../types';
import { NTQQMsgApi, NTQQUserApi } from '@/core'; import { OB11Constructor } from '@/onebot/helper/data';
import { OB11Constructor } from '@/onebot11/constructor';
const SchemaData = { const SchemaData = {
type: 'object', type: 'object',
@ -18,12 +17,14 @@ export default class GetRecentContact extends BaseAction<Payload, any> {
actionName = ActionName.GetRecentContact; actionName = ActionName.GetRecentContact;
PayloadSchema = SchemaData; PayloadSchema = SchemaData;
protected async _handle(payload: Payload) { protected async _handle(payload: Payload) {
const NTQQUserApi = this.CoreContext.getApiContext().UserApi;
const NTQQMsgApi = this.CoreContext.getApiContext().MsgApi;
const ret = await NTQQUserApi.getRecentContactListSnapShot(parseInt((payload.count || 10).toString())); const ret = await NTQQUserApi.getRecentContactListSnapShot(parseInt((payload.count || 10).toString()));
const data = await Promise.all(ret.info.changedList.map(async (t) => { const data = await Promise.all(ret.info.changedList.map(async (t) => {
const FastMsg = await NTQQMsgApi.getMsgsByMsgId({ chatType: t.chatType, peerUid: t.peerUid }, [t.msgId]); const FastMsg = await NTQQMsgApi.getMsgsByMsgId({ chatType: t.chatType, peerUid: t.peerUid }, [t.msgId]);
if (FastMsg.msgList.length > 0) { if (FastMsg.msgList.length > 0) {
//扩展ret.info.changedList //扩展ret.info.changedList
const lastestMsg = await OB11Constructor.message(FastMsg.msgList[0]); const lastestMsg = await OB11Constructor.message(this.CoreContext, FastMsg.msgList[0], "array");
return { return {
lastestMsg: lastestMsg, lastestMsg: lastestMsg,
peerUin: t.peerUin, peerUin: t.peerUin,