mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
refactor: msg context
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
import SendMsg from '../msg/SendMsg';
|
import SendMsg, { ContextMode } from '../msg/SendMsg';
|
||||||
import { ActionName, BaseCheckResult } from '../types';
|
import { ActionName, BaseCheckResult } from '../types';
|
||||||
import { OB11PostSendMsg } from '../../types';
|
import { OB11PostSendMsg } from '../../types';
|
||||||
|
|
||||||
// 未检测参数
|
// 未检测参数
|
||||||
class SendGroupMsg extends SendMsg {
|
class SendGroupMsg extends SendMsg {
|
||||||
actionName = ActionName.SendGroupMsg;
|
actionName = ActionName.SendGroupMsg;
|
||||||
|
contextMode: ContextMode = ContextMode.Group;
|
||||||
|
|
||||||
protected async check(payload: OB11PostSendMsg): Promise<BaseCheckResult> {
|
protected async check(payload: OB11PostSendMsg): Promise<BaseCheckResult> {
|
||||||
delete payload.user_id;
|
delete payload.user_id;
|
||||||
|
@@ -19,7 +19,11 @@ import { handleForwardNode } from '@/onebot11/action/msg/SendMsg/handle-forward-
|
|||||||
export interface ReturnDataType {
|
export interface ReturnDataType {
|
||||||
message_id: number;
|
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.
|
// Normalizes a mixed type (CQCode/a single segment/segment array) into a segment array.
|
||||||
export function normalize(message: OB11MessageMixType, autoEscape = false): OB11MessageData[] {
|
export function normalize(message: OB11MessageMixType, autoEscape = false): OB11MessageData[] {
|
||||||
return typeof message === 'string' ? (
|
return typeof message === 'string' ? (
|
||||||
@@ -78,14 +82,14 @@ export async function sendMsg(peer: Peer, sendElements: SendMessageElement[], de
|
|||||||
return returnMsg;
|
return returnMsg;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createContext(payload: OB11PostSendMsg): Promise<{
|
async function createContext(payload: OB11PostSendMsg, contextMode: ContextMode): Promise<{
|
||||||
peer: Peer, group?: Group
|
peer: Peer, group?: Group
|
||||||
}> {
|
}> {
|
||||||
// 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.
|
||||||
|
|
||||||
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
|
const group = (await getGroup(payload.group_id))!; // checked before
|
||||||
return {
|
return {
|
||||||
peer: {
|
peer: {
|
||||||
@@ -94,7 +98,8 @@ async function createContext(payload: OB11PostSendMsg): Promise<{
|
|||||||
},
|
},
|
||||||
group: group,
|
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 Uid = await NTQQUserApi.getUidByUin(payload.user_id.toString());
|
||||||
const isBuddy = await NTQQFriendApi.isBuddy(Uid!);
|
const isBuddy = await NTQQFriendApi.isBuddy(Uid!);
|
||||||
return {
|
return {
|
||||||
@@ -116,6 +121,7 @@ function getSpecialMsgNum(payload: OB11PostSendMsg, msgType: OB11MessageDataType
|
|||||||
|
|
||||||
export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
|
export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
|
||||||
actionName = ActionName.SendMsg;
|
actionName = ActionName.SendMsg;
|
||||||
|
contextMode = ContextMode.Normal;
|
||||||
|
|
||||||
protected async check(payload: OB11PostSendMsg): Promise<BaseCheckResult> {
|
protected async check(payload: OB11PostSendMsg): Promise<BaseCheckResult> {
|
||||||
const messages = normalize(payload.message);
|
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 }> {
|
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(
|
const messages = normalize(
|
||||||
payload.message,
|
payload.message,
|
||||||
|
@@ -1,9 +1,10 @@
|
|||||||
import SendMsg from './SendMsg';
|
import SendMsg, { ContextMode } from './SendMsg';
|
||||||
import { ActionName, BaseCheckResult } from '../types';
|
import { ActionName, BaseCheckResult } from '../types';
|
||||||
import { OB11PostSendMsg } from '../../types';
|
import { OB11PostSendMsg } from '../../types';
|
||||||
// 未检测参数
|
// 未检测参数
|
||||||
class SendPrivateMsg extends SendMsg {
|
class SendPrivateMsg extends SendMsg {
|
||||||
actionName = ActionName.SendPrivateMsg;
|
actionName = ActionName.SendPrivateMsg;
|
||||||
|
contextMode: ContextMode = ContextMode.Private;
|
||||||
|
|
||||||
protected async check(payload: OB11PostSendMsg): Promise<BaseCheckResult> {
|
protected async check(payload: OB11PostSendMsg): Promise<BaseCheckResult> {
|
||||||
payload.message_type = 'private';
|
payload.message_type = 'private';
|
||||||
|
Reference in New Issue
Block a user