mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
refactor
This commit is contained in:
7
.gitattributes
vendored
Normal file
7
.gitattributes
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
* text eol=lf
|
||||||
|
|
||||||
|
*.png -text
|
||||||
|
*.jpg -text
|
||||||
|
*.ico -text
|
||||||
|
*.gif -text
|
||||||
|
*.webp -text
|
@@ -41,7 +41,7 @@
|
|||||||
"electron-vite": "^2.3.0",
|
"electron-vite": "^2.3.0",
|
||||||
"protobufjs-cli": "^1.1.3",
|
"protobufjs-cli": "^1.1.3",
|
||||||
"typescript": "^5.6.2",
|
"typescript": "^5.6.2",
|
||||||
"vite": "^5.4.5",
|
"vite": "^5.4.6",
|
||||||
"vite-plugin-cp": "^4.0.8"
|
"vite-plugin-cp": "^4.0.8"
|
||||||
},
|
},
|
||||||
"packageManager": "yarn@4.4.1"
|
"packageManager": "yarn@4.4.1"
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { BaseAction } from '../BaseAction'
|
import { BaseAction, Schema } from '../BaseAction'
|
||||||
import { OB11ForwardMessage } from '../../types'
|
import { OB11ForwardMessage } from '../../types'
|
||||||
import { OB11Entities } from '../../entities'
|
import { OB11Entities } from '../../entities'
|
||||||
import { ActionName } from '../types'
|
import { ActionName } from '../types'
|
||||||
@@ -16,6 +16,11 @@ interface Response {
|
|||||||
|
|
||||||
export class GetForwardMsg extends BaseAction<Payload, Response> {
|
export class GetForwardMsg extends BaseAction<Payload, Response> {
|
||||||
actionName = ActionName.GoCQHTTP_GetForwardMsg
|
actionName = ActionName.GoCQHTTP_GetForwardMsg
|
||||||
|
payloadSchema = Schema.object({
|
||||||
|
message_id: String,
|
||||||
|
id: String
|
||||||
|
})
|
||||||
|
|
||||||
protected async _handle(payload: Payload) {
|
protected async _handle(payload: Payload) {
|
||||||
const msgId = payload.id || payload.message_id
|
const msgId = payload.id || payload.message_id
|
||||||
if (!msgId) {
|
if (!msgId) {
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { BaseAction } from '../BaseAction'
|
import { BaseAction, Schema } from '../BaseAction'
|
||||||
import { ActionName } from '../types'
|
import { ActionName } from '../types'
|
||||||
|
|
||||||
interface Payload {
|
interface Payload {
|
||||||
@@ -13,6 +13,9 @@ interface Response {
|
|||||||
|
|
||||||
export class GetGroupAtAllRemain extends BaseAction<Payload, Response> {
|
export class GetGroupAtAllRemain extends BaseAction<Payload, Response> {
|
||||||
actionName = ActionName.GoCQHTTP_GetGroupAtAllRemain
|
actionName = ActionName.GoCQHTTP_GetGroupAtAllRemain
|
||||||
|
payloadSchema = Schema.object({
|
||||||
|
group_id: Schema.union([Number, String]).required()
|
||||||
|
})
|
||||||
|
|
||||||
async _handle(payload: Payload) {
|
async _handle(payload: Payload) {
|
||||||
const data = await this.ctx.ntGroupApi.getGroupRemainAtTimes(payload.group_id.toString())
|
const data = await this.ctx.ntGroupApi.getGroupRemainAtTimes(payload.group_id.toString())
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { BaseAction } from '../BaseAction'
|
import { BaseAction, Schema } from '../BaseAction'
|
||||||
import { OB11Message } from '../../types'
|
import { OB11Message } from '../../types'
|
||||||
import { ActionName } from '../types'
|
import { ActionName } from '../types'
|
||||||
import { ChatType } from '@/ntqqapi/types'
|
import { ChatType } from '@/ntqqapi/types'
|
||||||
@@ -10,8 +10,8 @@ import { filterNullable } from '@/common/utils/misc'
|
|||||||
interface Payload {
|
interface Payload {
|
||||||
group_id: number | string
|
group_id: number | string
|
||||||
message_seq?: number | string
|
message_seq?: number | string
|
||||||
count?: number | string
|
count: number | string
|
||||||
reverseOrder?: boolean
|
reverseOrder: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Response {
|
interface Response {
|
||||||
@@ -20,10 +20,15 @@ interface Response {
|
|||||||
|
|
||||||
export class GetGroupMsgHistory extends BaseAction<Payload, Response> {
|
export class GetGroupMsgHistory extends BaseAction<Payload, Response> {
|
||||||
actionName = ActionName.GoCQHTTP_GetGroupMsgHistory
|
actionName = ActionName.GoCQHTTP_GetGroupMsgHistory
|
||||||
|
payloadSchema = Schema.object({
|
||||||
|
group_id: Schema.union([Number, String]).required(),
|
||||||
|
message_seq: Schema.union([Number, String]),
|
||||||
|
count: Schema.union([Number, String]).default(20),
|
||||||
|
reverseOrder: Schema.boolean().default(false),
|
||||||
|
})
|
||||||
|
|
||||||
protected async _handle(payload: Payload): Promise<Response> {
|
protected async _handle(payload: Payload): Promise<Response> {
|
||||||
const count = payload.count || 20
|
const { count, reverseOrder } = payload
|
||||||
const isReverseOrder = payload.reverseOrder || true
|
|
||||||
const peer = { chatType: ChatType.group, peerUid: payload.group_id.toString() }
|
const peer = { chatType: ChatType.group, peerUid: payload.group_id.toString() }
|
||||||
let msgList: RawMessage[] | undefined
|
let msgList: RawMessage[] | undefined
|
||||||
// 包含 message_seq 0
|
// 包含 message_seq 0
|
||||||
@@ -35,7 +40,7 @@ export class GetGroupMsgHistory extends BaseAction<Payload, Response> {
|
|||||||
msgList = (await this.ctx.ntMsgApi.getMsgHistory(peer, startMsgId, +count)).msgList
|
msgList = (await this.ctx.ntMsgApi.getMsgHistory(peer, startMsgId, +count)).msgList
|
||||||
}
|
}
|
||||||
if (!msgList?.length) throw new Error('未找到消息')
|
if (!msgList?.length) throw new Error('未找到消息')
|
||||||
if (isReverseOrder) msgList.reverse()
|
if (reverseOrder) msgList.reverse()
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
msgList.map(async msg => {
|
msgList.map(async msg => {
|
||||||
msg.msgShortId = MessageUnique.createMsg({ chatType: msg.chatType, peerUid: msg.peerUid }, msg.msgId)
|
msg.msgShortId = MessageUnique.createMsg({ chatType: msg.chatType, peerUid: msg.peerUid }, msg.msgId)
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { BaseAction } from '../BaseAction'
|
import { BaseAction, Schema } from '../BaseAction'
|
||||||
import { ActionName } from '../types'
|
import { ActionName } from '../types'
|
||||||
import { OB11GroupFile, OB11GroupFileFolder } from '../../types'
|
import { OB11GroupFile, OB11GroupFileFolder } from '../../types'
|
||||||
|
|
||||||
@@ -14,18 +14,19 @@ interface Response {
|
|||||||
|
|
||||||
export class GetGroupRootFiles extends BaseAction<Payload, Response> {
|
export class GetGroupRootFiles extends BaseAction<Payload, Response> {
|
||||||
actionName = ActionName.GoCQHTTP_GetGroupRootFiles
|
actionName = ActionName.GoCQHTTP_GetGroupRootFiles
|
||||||
|
payloadSchema = Schema.object({
|
||||||
|
group_id: Schema.union([Number, String]).required(),
|
||||||
|
file_count: Schema.union([Number, String]).default(50),
|
||||||
|
})
|
||||||
|
|
||||||
async _handle(payload: Payload) {
|
async _handle(payload: Payload) {
|
||||||
const data = await this.ctx.ntGroupApi.getGroupFileList(payload.group_id.toString(), {
|
const data = await this.ctx.ntGroupApi.getGroupFileList(payload.group_id.toString(), {
|
||||||
sortType: 1,
|
sortType: 1,
|
||||||
fileCount: +(payload.file_count ?? 50),
|
fileCount: +payload.file_count,
|
||||||
startIndex: 0,
|
startIndex: 0,
|
||||||
sortOrder: 2,
|
sortOrder: 2,
|
||||||
showOnlinedocFolder: 0,
|
showOnlinedocFolder: 0,
|
||||||
})
|
})
|
||||||
|
|
||||||
this.ctx.logger.info(data)
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
files: data.filter(item => item.fileInfo)
|
files: data.filter(item => item.fileInfo)
|
||||||
.map(item => {
|
.map(item => {
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { BaseAction } from '../BaseAction'
|
import { BaseAction, Schema } from '../BaseAction'
|
||||||
import { OB11User } from '../../types'
|
import { OB11User } from '../../types'
|
||||||
import { OB11Entities } from '../../entities'
|
import { OB11Entities } from '../../entities'
|
||||||
import { ActionName } from '../types'
|
import { ActionName } from '../types'
|
||||||
@@ -12,6 +12,9 @@ interface Payload {
|
|||||||
|
|
||||||
export class GetStrangerInfo extends BaseAction<Payload, OB11User> {
|
export class GetStrangerInfo extends BaseAction<Payload, OB11User> {
|
||||||
actionName = ActionName.GoCQHTTP_GetStrangerInfo
|
actionName = ActionName.GoCQHTTP_GetStrangerInfo
|
||||||
|
payloadSchema = Schema.object({
|
||||||
|
user_id: Schema.union([Number, String]).required()
|
||||||
|
})
|
||||||
|
|
||||||
protected async _handle(payload: Payload): Promise<OB11User> {
|
protected async _handle(payload: Payload): Promise<OB11User> {
|
||||||
if (!(getBuildVersion() >= 26702)) {
|
if (!(getBuildVersion() >= 26702)) {
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { BaseAction } from '../BaseAction'
|
import { BaseAction, Schema } from '../BaseAction'
|
||||||
import { ActionName } from '../types'
|
import { ActionName } from '../types'
|
||||||
import { MessageUnique } from '@/common/utils/messageUnique'
|
import { MessageUnique } from '@/common/utils/messageUnique'
|
||||||
|
|
||||||
@@ -8,11 +8,11 @@ interface Payload {
|
|||||||
|
|
||||||
export class MarkMsgAsRead extends BaseAction<Payload, null> {
|
export class MarkMsgAsRead extends BaseAction<Payload, null> {
|
||||||
actionName = ActionName.GoCQHTTP_MarkMsgAsRead
|
actionName = ActionName.GoCQHTTP_MarkMsgAsRead
|
||||||
|
payloadSchema = Schema.object({
|
||||||
|
message_id: Schema.union([Number, String]).required()
|
||||||
|
})
|
||||||
|
|
||||||
protected async _handle(payload: Payload) {
|
protected async _handle(payload: Payload) {
|
||||||
if (!payload.message_id) {
|
|
||||||
throw new Error('参数 message_id 不能为空')
|
|
||||||
}
|
|
||||||
const msg = await MessageUnique.getMsgIdAndPeerByShortId(+payload.message_id)
|
const msg = await MessageUnique.getMsgIdAndPeerByShortId(+payload.message_id)
|
||||||
if (!msg) {
|
if (!msg) {
|
||||||
throw new Error('msg not found')
|
throw new Error('msg not found')
|
||||||
|
@@ -9,6 +9,7 @@ interface Payload {
|
|||||||
|
|
||||||
export class HandleQuickOperation extends BaseAction<Payload, null> {
|
export class HandleQuickOperation extends BaseAction<Payload, null> {
|
||||||
actionName = ActionName.GoCQHTTP_HandleQuickOperation
|
actionName = ActionName.GoCQHTTP_HandleQuickOperation
|
||||||
|
|
||||||
protected async _handle(payload: Payload): Promise<null> {
|
protected async _handle(payload: Payload): Promise<null> {
|
||||||
handleQuickOperation(this.ctx, payload.context, payload.operation).catch(e => this.ctx.logger.error(e))
|
handleQuickOperation(this.ctx, payload.context, payload.operation).catch(e => this.ctx.logger.error(e))
|
||||||
return null
|
return null
|
||||||
|
@@ -1,20 +1,180 @@
|
|||||||
import SendMsg from '../msg/SendMsg'
|
import { unlink } from 'node:fs/promises'
|
||||||
import { OB11PostSendMsg } from '../../types'
|
import { OB11MessageNode } from '../../types'
|
||||||
import { ActionName } from '../types'
|
import { ActionName } from '../types'
|
||||||
|
import { BaseAction, Schema } from '../BaseAction'
|
||||||
|
import { Peer } from '@/ntqqapi/types/msg'
|
||||||
|
import { ChatType, ElementType, RawMessage, SendMessageElement } from '@/ntqqapi/types'
|
||||||
|
import { MessageUnique } from '@/common/utils/messageUnique'
|
||||||
|
import { selfInfo } from '@/common/globalVars'
|
||||||
|
import { convertMessage2List, createSendElements, sendMsg, createPeer, CreatePeerMode } from '../../helper/createMessage'
|
||||||
|
|
||||||
export class SendForwardMsg extends SendMsg {
|
interface Payload {
|
||||||
|
user_id?: string | number
|
||||||
|
group_id?: string | number
|
||||||
|
messages: OB11MessageNode[]
|
||||||
|
message_type?: 'group' | 'private'
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Response {
|
||||||
|
message_id: number
|
||||||
|
forward_id?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export class SendForwardMsg extends BaseAction<Payload, Response> {
|
||||||
actionName = ActionName.GoCQHTTP_SendForwardMsg
|
actionName = ActionName.GoCQHTTP_SendForwardMsg
|
||||||
|
payloadSchema = Schema.object({
|
||||||
|
user_id: Schema.union([Number, String]),
|
||||||
|
group_id: Schema.union([Number, String]),
|
||||||
|
messages: Schema.array(Schema.any()).required(),
|
||||||
|
message_type: Schema.union(['group', 'private'])
|
||||||
|
})
|
||||||
|
|
||||||
protected async _handle(payload: OB11PostSendMsg) {
|
protected async _handle(payload: Payload) {
|
||||||
if (payload.messages) payload.message = payload.messages
|
let contextMode = CreatePeerMode.Normal
|
||||||
return super._handle(payload)
|
if (payload.message_type === 'group') {
|
||||||
|
contextMode = CreatePeerMode.Group
|
||||||
|
} else if (payload.message_type === 'private') {
|
||||||
|
contextMode = CreatePeerMode.Private
|
||||||
|
}
|
||||||
|
const peer = await createPeer(this.ctx, payload, contextMode)
|
||||||
|
const returnMsg = await this.handleForwardNode(peer, payload.messages)
|
||||||
|
return { message_id: returnMsg.msgShortId! }
|
||||||
|
}
|
||||||
|
|
||||||
|
private async cloneMsg(msg: RawMessage): Promise<RawMessage | undefined> {
|
||||||
|
this.ctx.logger.info('克隆的目标消息', msg)
|
||||||
|
const sendElements: SendMessageElement[] = []
|
||||||
|
for (const ele of msg.elements) {
|
||||||
|
sendElements.push(ele as SendMessageElement)
|
||||||
|
}
|
||||||
|
if (sendElements.length === 0) {
|
||||||
|
this.ctx.logger.warn('需要clone的消息无法解析,将会忽略掉', msg)
|
||||||
|
}
|
||||||
|
this.ctx.logger.info('克隆消息', sendElements)
|
||||||
|
try {
|
||||||
|
const peer = {
|
||||||
|
chatType: ChatType.friend,
|
||||||
|
peerUid: selfInfo.uid
|
||||||
|
}
|
||||||
|
const nodeMsg = await this.ctx.ntMsgApi.sendMsg(peer, sendElements)
|
||||||
|
await this.ctx.sleep(400)
|
||||||
|
return nodeMsg
|
||||||
|
} catch (e) {
|
||||||
|
this.ctx.logger.warn(e, '克隆转发消息失败,将忽略本条消息', msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 返回一个合并转发的消息id
|
||||||
|
private async handleForwardNode(destPeer: Peer, messageNodes: OB11MessageNode[]) {
|
||||||
|
const selfPeer = {
|
||||||
|
chatType: ChatType.friend,
|
||||||
|
peerUid: selfInfo.uid,
|
||||||
|
}
|
||||||
|
let nodeMsgIds: string[] = []
|
||||||
|
// 先判断一遍是不是id和自定义混用
|
||||||
|
for (const messageNode of messageNodes) {
|
||||||
|
// 一个node表示一个人的消息
|
||||||
|
const nodeId = messageNode.data.id
|
||||||
|
// 有nodeId表示一个子转发消息卡片
|
||||||
|
if (nodeId) {
|
||||||
|
const nodeMsg = await MessageUnique.getMsgIdAndPeerByShortId(+nodeId) || await MessageUnique.getPeerByMsgId(nodeId)
|
||||||
|
if (!nodeMsg) {
|
||||||
|
this.ctx.logger.warn('转发消息失败,未找到消息', nodeId)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
nodeMsgIds.push(nodeMsg.MsgId)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// 自定义的消息
|
||||||
|
// 提取消息段,发给自己生成消息id
|
||||||
|
try {
|
||||||
|
const { sendElements, deleteAfterSentFiles } = await createSendElements(
|
||||||
|
this.ctx,
|
||||||
|
convertMessage2List(messageNode.data.content),
|
||||||
|
destPeer
|
||||||
|
)
|
||||||
|
this.ctx.logger.info('开始生成转发节点', sendElements)
|
||||||
|
const sendElementsSplit: SendMessageElement[][] = []
|
||||||
|
let splitIndex = 0
|
||||||
|
for (const ele of sendElements) {
|
||||||
|
if (!sendElementsSplit[splitIndex]) {
|
||||||
|
sendElementsSplit[splitIndex] = []
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ele.elementType === ElementType.FILE || ele.elementType === ElementType.VIDEO) {
|
||||||
|
if (sendElementsSplit[splitIndex].length > 0) {
|
||||||
|
splitIndex++
|
||||||
|
}
|
||||||
|
sendElementsSplit[splitIndex] = [ele]
|
||||||
|
splitIndex++
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sendElementsSplit[splitIndex].push(ele)
|
||||||
|
}
|
||||||
|
this.ctx.logger.info(sendElementsSplit)
|
||||||
|
}
|
||||||
|
// log("分割后的转发节点", sendElementsSplit)
|
||||||
|
for (const eles of sendElementsSplit) {
|
||||||
|
const nodeMsg = await sendMsg(this.ctx, selfPeer, eles, [])
|
||||||
|
if (!nodeMsg) {
|
||||||
|
this.ctx.logger.warn('转发节点生成失败', eles)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
nodeMsgIds.push(nodeMsg.msgId)
|
||||||
|
await this.ctx.sleep(400)
|
||||||
|
}
|
||||||
|
deleteAfterSentFiles.map(path => unlink(path))
|
||||||
|
} catch (e) {
|
||||||
|
this.ctx.logger.error('生成转发消息节点失败', e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查srcPeer是否一致,不一致则需要克隆成自己的消息, 让所有srcPeer都变成自己的,使其保持一致才能够转发
|
||||||
|
const nodeMsgArray: RawMessage[] = []
|
||||||
|
let srcPeer: Peer | null = null
|
||||||
|
let needSendSelf = false
|
||||||
|
for (const msgId of nodeMsgIds) {
|
||||||
|
const nodeMsgPeer = await MessageUnique.getPeerByMsgId(msgId)
|
||||||
|
if (nodeMsgPeer) {
|
||||||
|
const nodeMsg = (await this.ctx.ntMsgApi.getMsgsByMsgId(nodeMsgPeer.Peer, [msgId])).msgList[0]
|
||||||
|
srcPeer = srcPeer ?? { chatType: nodeMsg.chatType, peerUid: nodeMsg.peerUid }
|
||||||
|
if (srcPeer.peerUid !== nodeMsg.peerUid) {
|
||||||
|
needSendSelf = true
|
||||||
|
}
|
||||||
|
nodeMsgArray.push(nodeMsg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nodeMsgIds = nodeMsgArray.map((msg) => msg.msgId)
|
||||||
|
if (needSendSelf) {
|
||||||
|
for (const msg of nodeMsgArray) {
|
||||||
|
if (msg.peerUid === selfPeer.peerUid) continue
|
||||||
|
await this.cloneMsg(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (nodeMsgIds.length === 0) {
|
||||||
|
throw Error('转发消息失败,节点为空')
|
||||||
|
}
|
||||||
|
const returnMsg = await this.ctx.ntMsgApi.multiForwardMsg(srcPeer!, destPeer, nodeMsgIds)
|
||||||
|
returnMsg.msgShortId = MessageUnique.createMsg(destPeer, returnMsg.msgId)
|
||||||
|
return returnMsg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SendPrivateForwardMsg extends SendForwardMsg {
|
export class SendPrivateForwardMsg extends SendForwardMsg {
|
||||||
actionName = ActionName.GoCQHTTP_SendPrivateForwardMsg
|
actionName = ActionName.GoCQHTTP_SendPrivateForwardMsg
|
||||||
|
|
||||||
|
protected _handle(payload: Payload) {
|
||||||
|
payload.message_type = 'private'
|
||||||
|
return super._handle(payload)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SendGroupForwardMsg extends SendForwardMsg {
|
export class SendGroupForwardMsg extends SendForwardMsg {
|
||||||
actionName = ActionName.GoCQHTTP_SendGroupForwardMsg
|
actionName = ActionName.GoCQHTTP_SendGroupForwardMsg
|
||||||
|
|
||||||
|
protected _handle(payload: Payload) {
|
||||||
|
payload.message_type = 'group'
|
||||||
|
return super._handle(payload)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { BaseAction } from '../BaseAction'
|
import { BaseAction, Schema } from '../BaseAction'
|
||||||
import { ActionName } from '../types'
|
import { ActionName } from '../types'
|
||||||
import { unlink } from 'fs/promises'
|
import { unlink } from 'fs/promises'
|
||||||
import { checkFileReceived, uri2local } from '@/common/utils/file'
|
import { checkFileReceived, uri2local } from '@/common/utils/file'
|
||||||
@@ -7,20 +7,24 @@ interface Payload {
|
|||||||
group_id: number | string
|
group_id: number | string
|
||||||
content: string
|
content: string
|
||||||
image?: string
|
image?: string
|
||||||
pinned?: number | string //扩展
|
pinned: number | string //扩展
|
||||||
confirm_required?: number | string //扩展
|
confirm_required: number | string //扩展
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SendGroupNotice extends BaseAction<Payload, null> {
|
export class SendGroupNotice extends BaseAction<Payload, null> {
|
||||||
actionName = ActionName.GoCQHTTP_SendGroupNotice
|
actionName = ActionName.GoCQHTTP_SendGroupNotice
|
||||||
|
payloadSchema = Schema.object({
|
||||||
|
group_id: Schema.union([Number, String]).required(),
|
||||||
|
content: Schema.string().required(),
|
||||||
|
image: Schema.string(),
|
||||||
|
pinned: Schema.union([Number, String]).default(0),
|
||||||
|
confirm_required: Schema.union([Number, String]).default(1)
|
||||||
|
})
|
||||||
|
|
||||||
async _handle(payload: Payload) {
|
async _handle(payload: Payload) {
|
||||||
if (!payload.content) {
|
|
||||||
throw new Error('参数 content 不能为空')
|
|
||||||
}
|
|
||||||
const groupCode = payload.group_id.toString()
|
const groupCode = payload.group_id.toString()
|
||||||
const pinned = Number(payload.pinned ?? 0)
|
const pinned = +payload.pinned
|
||||||
const confirmRequired = Number(payload.confirm_required ?? 1)
|
const confirmRequired = +payload.confirm_required
|
||||||
|
|
||||||
let picInfo: { id: string, width: number, height: number } | undefined
|
let picInfo: { id: string, width: number, height: number } | undefined
|
||||||
if (payload.image) {
|
if (payload.image) {
|
||||||
|
@@ -6,7 +6,6 @@ class SendGroupMsg extends SendMsg {
|
|||||||
actionName = ActionName.SendGroupMsg
|
actionName = ActionName.SendGroupMsg
|
||||||
|
|
||||||
protected _handle(payload: OB11PostSendMsg) {
|
protected _handle(payload: OB11PostSendMsg) {
|
||||||
delete (payload as Partial<OB11PostSendMsg>).user_id
|
|
||||||
payload.message_type = 'group'
|
payload.message_type = 'group'
|
||||||
return super._handle(payload)
|
return super._handle(payload)
|
||||||
}
|
}
|
||||||
|
@@ -1,25 +1,14 @@
|
|||||||
import {
|
|
||||||
ChatType,
|
|
||||||
ElementType,
|
|
||||||
RawMessage,
|
|
||||||
SendMessageElement,
|
|
||||||
} from '@/ntqqapi/types'
|
|
||||||
import {
|
import {
|
||||||
OB11MessageCustomMusic,
|
OB11MessageCustomMusic,
|
||||||
OB11MessageData,
|
OB11MessageData,
|
||||||
OB11MessageDataType,
|
OB11MessageDataType,
|
||||||
OB11MessageJson,
|
OB11MessageJson,
|
||||||
OB11MessageMusic,
|
OB11MessageMusic,
|
||||||
OB11MessageNode,
|
|
||||||
OB11PostSendMsg,
|
OB11PostSendMsg,
|
||||||
} from '../../types'
|
} from '../../types'
|
||||||
import fs from 'node:fs'
|
|
||||||
import { BaseAction } from '../BaseAction'
|
import { BaseAction } from '../BaseAction'
|
||||||
import { ActionName } from '../types'
|
import { ActionName } from '../types'
|
||||||
import { CustomMusicSignPostData, IdMusicSignPostData, MusicSign, MusicSignPostData } from '@/common/utils/sign'
|
import { CustomMusicSignPostData, IdMusicSignPostData, MusicSign, MusicSignPostData } from '@/common/utils/sign'
|
||||||
import { Peer } from '@/ntqqapi/types/msg'
|
|
||||||
import { MessageUnique } from '@/common/utils/messageUnique'
|
|
||||||
import { selfInfo } from '@/common/globalVars'
|
|
||||||
import { convertMessage2List, createSendElements, sendMsg, createPeer, CreatePeerMode } from '../../helper/createMessage'
|
import { convertMessage2List, createSendElements, sendMsg, createPeer, CreatePeerMode } from '../../helper/createMessage'
|
||||||
|
|
||||||
interface ReturnData {
|
interface ReturnData {
|
||||||
@@ -42,12 +31,7 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnData> {
|
|||||||
payload.auto_escape === true || payload.auto_escape === 'true',
|
payload.auto_escape === true || payload.auto_escape === 'true',
|
||||||
)
|
)
|
||||||
if (this.getSpecialMsgNum(messages, OB11MessageDataType.node)) {
|
if (this.getSpecialMsgNum(messages, OB11MessageDataType.node)) {
|
||||||
try {
|
throw new Error('请使用 /send_group_forward_msg 或 /send_private_forward_msg 发送合并转发')
|
||||||
const returnMsg = await this.handleForwardNode(peer, messages as OB11MessageNode[])
|
|
||||||
return { message_id: returnMsg.msgShortId! }
|
|
||||||
} catch (e) {
|
|
||||||
throw '发送转发消息失败 ' + e
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if (this.getSpecialMsgNum(messages, OB11MessageDataType.music)) {
|
else if (this.getSpecialMsgNum(messages, OB11MessageDataType.music)) {
|
||||||
const music = messages[0] as OB11MessageMusic
|
const music = messages[0] as OB11MessageMusic
|
||||||
@@ -114,140 +98,10 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnData> {
|
|||||||
|
|
||||||
private getSpecialMsgNum(message: OB11MessageData[], msgType: OB11MessageDataType): number {
|
private getSpecialMsgNum(message: OB11MessageData[], msgType: OB11MessageDataType): number {
|
||||||
if (Array.isArray(message)) {
|
if (Array.isArray(message)) {
|
||||||
return message.filter((msg) => msg.type == msgType).length
|
return message.filter((msg) => msg.type === msgType).length
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
private async cloneMsg(msg: RawMessage): Promise<RawMessage | undefined> {
|
|
||||||
this.ctx.logger.info('克隆的目标消息', msg)
|
|
||||||
const sendElements: SendMessageElement[] = []
|
|
||||||
for (const ele of msg.elements) {
|
|
||||||
sendElements.push(ele as SendMessageElement)
|
|
||||||
}
|
|
||||||
if (sendElements.length === 0) {
|
|
||||||
this.ctx.logger.warn('需要clone的消息无法解析,将会忽略掉', msg)
|
|
||||||
}
|
|
||||||
this.ctx.logger.info('克隆消息', sendElements)
|
|
||||||
try {
|
|
||||||
const peer = {
|
|
||||||
chatType: ChatType.friend,
|
|
||||||
peerUid: selfInfo.uid
|
|
||||||
}
|
|
||||||
const nodeMsg = await this.ctx.ntMsgApi.sendMsg(peer, sendElements)
|
|
||||||
await this.ctx.sleep(400)
|
|
||||||
return nodeMsg
|
|
||||||
} catch (e) {
|
|
||||||
this.ctx.logger.warn(e, '克隆转发消息失败,将忽略本条消息', msg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 返回一个合并转发的消息id
|
|
||||||
private async handleForwardNode(destPeer: Peer, messageNodes: OB11MessageNode[]) {
|
|
||||||
const selfPeer = {
|
|
||||||
chatType: ChatType.friend,
|
|
||||||
peerUid: selfInfo.uid,
|
|
||||||
}
|
|
||||||
let nodeMsgIds: string[] = []
|
|
||||||
// 先判断一遍是不是id和自定义混用
|
|
||||||
for (const messageNode of messageNodes) {
|
|
||||||
// 一个node表示一个人的消息
|
|
||||||
const nodeId = messageNode.data.id
|
|
||||||
// 有nodeId表示一个子转发消息卡片
|
|
||||||
if (nodeId) {
|
|
||||||
const nodeMsg = await MessageUnique.getMsgIdAndPeerByShortId(+nodeId) || await MessageUnique.getPeerByMsgId(nodeId)
|
|
||||||
if (!nodeMsg) {
|
|
||||||
this.ctx.logger.warn('转发消息失败,未找到消息', nodeId)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
nodeMsgIds.push(nodeMsg.MsgId)
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// 自定义的消息
|
|
||||||
// 提取消息段,发给自己生成消息id
|
|
||||||
try {
|
|
||||||
const { sendElements, deleteAfterSentFiles } = await createSendElements(
|
|
||||||
this.ctx,
|
|
||||||
convertMessage2List(messageNode.data.content),
|
|
||||||
destPeer
|
|
||||||
)
|
|
||||||
this.ctx.logger.info('开始生成转发节点', sendElements)
|
|
||||||
const sendElementsSplit: SendMessageElement[][] = []
|
|
||||||
let splitIndex = 0
|
|
||||||
for (const ele of sendElements) {
|
|
||||||
if (!sendElementsSplit[splitIndex]) {
|
|
||||||
sendElementsSplit[splitIndex] = []
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ele.elementType === ElementType.FILE || ele.elementType === ElementType.VIDEO) {
|
|
||||||
if (sendElementsSplit[splitIndex].length > 0) {
|
|
||||||
splitIndex++
|
|
||||||
}
|
|
||||||
sendElementsSplit[splitIndex] = [ele]
|
|
||||||
splitIndex++
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
sendElementsSplit[splitIndex].push(ele)
|
|
||||||
}
|
|
||||||
this.ctx.logger.info(sendElementsSplit)
|
|
||||||
}
|
|
||||||
// log("分割后的转发节点", sendElementsSplit)
|
|
||||||
for (const eles of sendElementsSplit) {
|
|
||||||
const nodeMsg = await sendMsg(this.ctx, selfPeer, eles, [])
|
|
||||||
if (!nodeMsg) {
|
|
||||||
this.ctx.logger.warn('转发节点生成失败', eles)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
nodeMsgIds.push(nodeMsg.msgId)
|
|
||||||
await this.ctx.sleep(400)
|
|
||||||
}
|
|
||||||
deleteAfterSentFiles.map((f) => fs.unlink(f, () => {
|
|
||||||
}))
|
|
||||||
} catch (e) {
|
|
||||||
this.ctx.logger.error('生成转发消息节点失败', e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查srcPeer是否一致,不一致则需要克隆成自己的消息, 让所有srcPeer都变成自己的,使其保持一致才能够转发
|
|
||||||
const nodeMsgArray: RawMessage[] = []
|
|
||||||
let srcPeer: Peer | null = null
|
|
||||||
let needSendSelf = false
|
|
||||||
for (const msgId of nodeMsgIds) {
|
|
||||||
const nodeMsgPeer = await MessageUnique.getPeerByMsgId(msgId)
|
|
||||||
if (nodeMsgPeer) {
|
|
||||||
const nodeMsg = (await this.ctx.ntMsgApi.getMsgsByMsgId(nodeMsgPeer.Peer, [msgId])).msgList[0]
|
|
||||||
srcPeer = srcPeer ?? { chatType: nodeMsg.chatType, peerUid: nodeMsg.peerUid }
|
|
||||||
if (srcPeer.peerUid !== nodeMsg.peerUid) {
|
|
||||||
needSendSelf = true
|
|
||||||
}
|
|
||||||
nodeMsgArray.push(nodeMsg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
nodeMsgIds = nodeMsgArray.map((msg) => msg.msgId)
|
|
||||||
if (needSendSelf) {
|
|
||||||
for (const msg of nodeMsgArray) {
|
|
||||||
if (msg.peerUid === selfPeer.peerUid) continue
|
|
||||||
await this.cloneMsg(msg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// elements之间用换行符分隔
|
|
||||||
// let _sendForwardElements: SendMessageElement[] = []
|
|
||||||
// for(let i = 0; i < sendForwardElements.length; i++){
|
|
||||||
// _sendForwardElements.push(sendForwardElements[i])
|
|
||||||
// _sendForwardElements.push(SendMsgElementConstructor.text("\n\n"))
|
|
||||||
// }
|
|
||||||
// const nodeMsg = await NTQQApi.sendMsg(selfPeer, _sendForwardElements, true);
|
|
||||||
// nodeIds.push(nodeMsg.msgId)
|
|
||||||
// await sleep(500);
|
|
||||||
// 开发转发
|
|
||||||
if (nodeMsgIds.length === 0) {
|
|
||||||
throw Error('转发消息失败,节点为空')
|
|
||||||
}
|
|
||||||
const returnMsg = await this.ctx.ntMsgApi.multiForwardMsg(srcPeer!, destPeer, nodeMsgIds)
|
|
||||||
returnMsg.msgShortId = MessageUnique.createMsg(destPeer, returnMsg.msgId)
|
|
||||||
return returnMsg
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default SendMsg
|
export default SendMsg
|
||||||
|
@@ -308,8 +308,8 @@ export type OB11MessageData =
|
|||||||
|
|
||||||
export interface OB11PostSendMsg {
|
export interface OB11PostSendMsg {
|
||||||
message_type?: 'private' | 'group'
|
message_type?: 'private' | 'group'
|
||||||
user_id: string
|
user_id?: string | number
|
||||||
group_id?: string
|
group_id?: string | number
|
||||||
message: OB11MessageMixType
|
message: OB11MessageMixType
|
||||||
messages?: OB11MessageMixType // 兼容 go-cqhttp
|
messages?: OB11MessageMixType // 兼容 go-cqhttp
|
||||||
auto_escape?: boolean | string
|
auto_escape?: boolean | string
|
||||||
|
Reference in New Issue
Block a user