refactor: msg context

This commit is contained in:
手瓜一十雪
2024-06-28 13:38:03 +08:00
parent 861f105bea
commit a643fac073
3 changed files with 15 additions and 7 deletions

View File

@@ -1,10 +1,11 @@
import SendMsg from '../msg/SendMsg';
import SendMsg, { ContextMode } from '../msg/SendMsg';
import { ActionName, BaseCheckResult } from '../types';
import { OB11PostSendMsg } from '../../types';
// 未检测参数
class SendGroupMsg extends SendMsg {
actionName = ActionName.SendGroupMsg;
contextMode: ContextMode = ContextMode.Group;
protected async check(payload: OB11PostSendMsg): Promise<BaseCheckResult> {
delete payload.user_id;

View File

@@ -19,7 +19,11 @@ import { handleForwardNode } from '@/onebot11/action/msg/SendMsg/handle-forward-
export interface ReturnDataType {
message_id: number;
}
export enum ContextMode {
Normal = 0,
Private = 1,
Group = 2
}
// Normalizes a mixed type (CQCode/a single segment/segment array) into a segment array.
export function normalize(message: OB11MessageMixType, autoEscape = false): OB11MessageData[] {
return typeof message === 'string' ? (
@@ -78,14 +82,14 @@ export async function sendMsg(peer: Peer, sendElements: SendMessageElement[], de
return returnMsg;
}
async function createContext(payload: OB11PostSendMsg): Promise<{
async function createContext(payload: OB11PostSendMsg, contextMode: ContextMode): Promise<{
peer: Peer, group?: Group
}> {
// This function determines the type of message by the existence of user_id / group_id,
// not message_type.
// This redundant design of Ob11 here should be blamed.
if (payload.group_id) { // take this as a group message
if ((contextMode === ContextMode.Group || contextMode === ContextMode.Normal) && payload.group_id) {
const group = (await getGroup(payload.group_id))!; // checked before
return {
peer: {
@@ -94,7 +98,8 @@ async function createContext(payload: OB11PostSendMsg): Promise<{
},
group: group,
};
} else if (payload.user_id) { // take this as a private message
}
if ((contextMode === ContextMode.Private || contextMode === ContextMode.Normal) && payload.user_id) {
const Uid = await NTQQUserApi.getUidByUin(payload.user_id.toString());
const isBuddy = await NTQQFriendApi.isBuddy(Uid!);
return {
@@ -116,6 +121,7 @@ function getSpecialMsgNum(payload: OB11PostSendMsg, msgType: OB11MessageDataType
export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
actionName = ActionName.SendMsg;
contextMode = ContextMode.Normal;
protected async check(payload: OB11PostSendMsg): Promise<BaseCheckResult> {
const messages = normalize(payload.message);
@@ -138,7 +144,7 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
}
protected async _handle(payload: OB11PostSendMsg): Promise<{ message_id: number }> {
const { peer, group } = await createContext(payload);
let { peer, group } = await createContext(payload, this.contextMode);
const messages = normalize(
payload.message,

View File

@@ -1,9 +1,10 @@
import SendMsg from './SendMsg';
import SendMsg, { ContextMode } from './SendMsg';
import { ActionName, BaseCheckResult } from '../types';
import { OB11PostSendMsg } from '../../types';
// 未检测参数
class SendPrivateMsg extends SendMsg {
actionName = ActionName.SendPrivateMsg;
contextMode: ContextMode = ContextMode.Private;
protected async check(payload: OB11PostSendMsg): Promise<BaseCheckResult> {
payload.message_type = 'private';