fix(api): report sent message

This commit is contained in:
Il Harper 2024-04-06 02:35:58 +08:00
parent 373ebf191f
commit 312b01acff
No known key found for this signature in database
GPG Key ID: 4B71FCA698E7E8EC
5 changed files with 90 additions and 34 deletions

View File

@ -1,9 +1,9 @@
import type { Peer, Element as RedElement } from '@chronocat/red' import type { Peer, Element as RedElement, RedMessage } from '@chronocat/red'
import { AtType, ChatType, FaceType } from '@chronocat/red' import { AtType, ChatType, FaceType } from '@chronocat/red'
import type { import type {
ChronocatContext, ChronocatContext,
ChronocatSatoriServerConfig, ChronocatSatoriServerConfig,
Message, Event,
} from '@chronocat/shell' } from '@chronocat/shell'
import type h from '@satorijs/element' import type h from '@satorijs/element'
import type { O } from 'ts-toolbelt' import type { O } from 'ts-toolbelt'
@ -36,7 +36,7 @@ export class Messager {
peer: Partial<Peer> peer: Partial<Peer>
public errors: Error[] = [] public errors: Error[] = []
public results: Message[] = [] public results: Event[] = []
stack: State[] = [new State('message')] stack: State[] = [new State('message')]
children: O.Partial<RedElement, 'deep'>[] = [] children: O.Partial<RedElement, 'deep'>[] = []
@ -54,24 +54,27 @@ export class Messager {
await this.flush() await this.flush()
if (this.errors.length) throw this.errors if (this.errors.length) throw this.errors
else return this.results.filter(Boolean) // .map(({ id }) => ({ id })) else return this.results.filter(Boolean)
} }
flush = async () => { flush = async () => {
if (!this.children.length) return if (!this.children.length) return
this.normalize() this.normalize()
const result = await this.common.send(this.ctx, this.peer, this.children)
await this.pushResult(
await this.common.send(this.ctx, this.peer, this.children),
)
this.children = []
}
pushResult = async (result: RedMessage) => {
const parsedEvents = await this.ctx.chronocat.api[ const parsedEvents = await this.ctx.chronocat.api[
'chronocat.internal.red.message.parse' 'chronocat.internal.red.message.parse'
](result, this.config) ](result, this.config)
if (parsedEvents) if (parsedEvents) this.results.push(...parsedEvents)
for (const parsedEvent of parsedEvents)
if (parsedEvent.message) this.results.push(parsedEvent.message)
this.children = []
} }
private normalize = () => { private normalize = () => {
@ -294,13 +297,15 @@ export class Messager {
await this.render(children) await this.render(children)
} else if (children.every((x) => 'id' in x)) { } else if (children.every((x) => 'id' in x)) {
// 普通合并转发消息 // 普通合并转发消息
await this.common.sendForward( await this.pushResult(
this.ctx, await this.common.sendForward(
this.peer, this.ctx,
children.map((x) => ({ this.peer,
msgId: x.attrs['id'] as string, children.map((x) => ({
senderShowName: 'QQ用户', msgId: x.attrs['id'] as string,
})), senderShowName: 'QQ用户',
})),
),
) )
} else { } else {
//伪造合并转发消息。本引擎不支持,使用兜底行为 //伪造合并转发消息。本引擎不支持,使用兜底行为

View File

@ -49,7 +49,7 @@ const defaultSrcContact: Peer = {
export let sendForwardMsgBuffer = Buffer.alloc(0) export let sendForwardMsgBuffer = Buffer.alloc(0)
export let sendForwardCover = defaultSendForwardCover export let sendForwardCover = defaultSendForwardCover
let task = Promise.resolve<unknown>(undefined) let task = Promise.resolve<RedMessage>(undefined as unknown as RedMessage)
export const commonSendForward = async ( export const commonSendForward = async (
ctx: ChronocatContext, ctx: ChronocatContext,
@ -78,10 +78,10 @@ export const commonSendForward = async (
: (peer as Peer) : (peer as Peer)
task = task task = task
.then(() => ctx.chronocat.sleep(80)) .then(() => ctx.chronocat.sleep(80) as unknown as RedMessage)
.then( .then(
() => () =>
new Promise((resolve, reject) => { new Promise<RedMessage>((resolve, reject) => {
sendForwardMsgBuffer = Buffer.alloc(0) sendForwardMsgBuffer = Buffer.alloc(0)
sendForwardCover = defaultSendForwardCover sendForwardCover = defaultSendForwardCover
@ -103,7 +103,7 @@ export const commonSendForward = async (
}, ctx.chronocat.timeout) }, ctx.chronocat.timeout)
}), }),
) )
.catch((e) => console.log(e)) .catch((e) => console.log(e) as unknown as RedMessage)
return task return task
} }

View File

@ -58,8 +58,11 @@ export const apply = async (ctx: ChronocatContext) => {
register('guild.member.list', buildGuildMemberList(ctx)) register('guild.member.list', buildGuildMemberList(ctx))
register('guild.member.kick', buildGuildMemberKick(ctx)) register('guild.member.kick', buildGuildMemberKick(ctx))
register('guild.member.mute', buildGuildMemberMute(ctx)) register('guild.member.mute', buildGuildMemberMute(ctx))
register('message.create', buildMessageCreate(ctx)) register('chronocat.internal.message.create2.normal', buildMessageCreate(ctx))
register('chronocat.internal.message.create.forward', buildMessageCreate(ctx)) register(
'chronocat.internal.message.create2.forward',
buildMessageCreate(ctx),
)
register('message.get', buildMessageGet(ctx)) register('message.get', buildMessageGet(ctx))
register('message.delete', buildMessageDelete(ctx)) register('message.delete', buildMessageDelete(ctx))
register('message.list', buildMessageList(ctx)) register('message.list', buildMessageList(ctx))

View File

@ -1,4 +1,13 @@
import type { MessageCreatePayload as MessageCreatePayloadRich } from '../../types' import type { O } from 'ts-toolbelt'
import type {
ChronocatLogCurrentConfig,
ChronocatSatoriEventsConfig,
} from '../../../services/config/configEntity'
import type { ChronocatContext, SatoriDispatchMessage } from '../../../types'
import type {
Event,
MessageCreatePayload as MessageCreatePayloadRich,
} from '../../types'
import type { MessageCreatePayload as MessageCreatePayloadEntity } from '../../types/satoriPayloadEntity' import type { MessageCreatePayload as MessageCreatePayloadEntity } from '../../types/satoriPayloadEntity'
import type { RouteContext } from '../types' import type { RouteContext } from '../types'
@ -61,11 +70,12 @@ async function messageCreateUsingJson({
} }
let method: let method:
| 'message.create' | 'chronocat.internal.message.create2.normal'
| 'chronocat.internal.message.create.forward' | 'chronocat.internal.message.create2.forward'
| 'chronocat.internal.message.create.forward.fake' | 'chronocat.internal.message.create2.forward.fake'
| 'chronocat.internal.message.create.poke' | 'chronocat.internal.message.create2.poke'
| 'chronocat.internal.message.create.markdown' = 'message.create' | 'chronocat.internal.message.create2.markdown' =
'chronocat.internal.message.create2.normal'
const forwards = cctx.chronocat.h const forwards = cctx.chronocat.h
.select(payloadRich.content, 'message') .select(payloadRich.content, 'message')
@ -103,8 +113,8 @@ async function messageCreateUsingJson({
return return
if (forward!.children.every((x) => x.attrs['id'])) if (forward!.children.every((x) => x.attrs['id']))
method = 'chronocat.internal.message.create.forward' method = 'chronocat.internal.message.create2.forward'
else method = 'chronocat.internal.message.create.forward.fake' else method = 'chronocat.internal.message.create2.forward.fake'
} }
const pokes = cctx.chronocat.h.select( const pokes = cctx.chronocat.h.select(
@ -113,7 +123,7 @@ async function messageCreateUsingJson({
) )
if (pokes.length) { if (pokes.length) {
// TODO: 如果单条消息内除了 poke 还有其他元素,打印警告 // TODO: 如果单条消息内除了 poke 还有其他元素,打印警告
method = 'chronocat.internal.message.create.poke' method = 'chronocat.internal.message.create2.poke'
} }
const markdowns = cctx.chronocat.h.select( const markdowns = cctx.chronocat.h.select(
@ -122,8 +132,25 @@ async function messageCreateUsingJson({
) )
if (markdowns.length) { if (markdowns.length) {
// TODO: 如果单条消息内除了 markdown 还有其他元素,打印警告 // TODO: 如果单条消息内除了 markdown 还有其他元素,打印警告
method = 'chronocat.internal.message.create.markdown' method = 'chronocat.internal.message.create2.markdown'
} }
return await cctx.chronocat.api[method](payloadRich, config) const result = await cctx.chronocat.api[method](payloadRich, config)
cctx.chronocat.emit(new MessageCreatedDispatchMessage(result))
return result.map((x) => x.message).filter(Boolean)
}
export class MessageCreatedDispatchMessage implements SatoriDispatchMessage {
constructor(private events: Event[]) {}
type = 'satori' as const
toSatori = async (
_ctx: ChronocatContext,
_config: O.Intersect<
ChronocatLogCurrentConfig,
ChronocatSatoriEventsConfig
>,
) => this.events
} }

View File

@ -153,6 +153,27 @@ export interface CCInternalMethods {
Message[], Message[],
] ]
'chronocat.internal.message.create2.normal': [
[MessageCreatePayload, ChronocatSatoriServerConfig],
Event[],
]
'chronocat.internal.message.create2.forward': [
[MessageCreatePayload, ChronocatSatoriServerConfig],
Event[],
]
'chronocat.internal.message.create2.forward.fake': [
[MessageCreatePayload, ChronocatSatoriServerConfig],
Event[],
]
'chronocat.internal.message.create2.poke': [
[MessageCreatePayload, ChronocatSatoriServerConfig],
Event[],
]
'chronocat.internal.message.create2.markdown': [
[MessageCreatePayload, ChronocatSatoriServerConfig],
Event[],
]
'chronocat.internal.red.message.parse': [ 'chronocat.internal.red.message.parse': [
[RedMessage, ChronocatSatoriServerConfig], [RedMessage, ChronocatSatoriServerConfig],
Event[] | undefined, Event[] | undefined,