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

View File

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

View File

@ -58,8 +58,11 @@ export const apply = async (ctx: ChronocatContext) => {
register('guild.member.list', buildGuildMemberList(ctx))
register('guild.member.kick', buildGuildMemberKick(ctx))
register('guild.member.mute', buildGuildMemberMute(ctx))
register('message.create', buildMessageCreate(ctx))
register('chronocat.internal.message.create.forward', buildMessageCreate(ctx))
register('chronocat.internal.message.create2.normal', buildMessageCreate(ctx))
register(
'chronocat.internal.message.create2.forward',
buildMessageCreate(ctx),
)
register('message.get', buildMessageGet(ctx))
register('message.delete', buildMessageDelete(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 { RouteContext } from '../types'
@ -61,11 +70,12 @@ async function messageCreateUsingJson({
}
let method:
| 'message.create'
| 'chronocat.internal.message.create.forward'
| 'chronocat.internal.message.create.forward.fake'
| 'chronocat.internal.message.create.poke'
| 'chronocat.internal.message.create.markdown' = 'message.create'
| 'chronocat.internal.message.create2.normal'
| 'chronocat.internal.message.create2.forward'
| 'chronocat.internal.message.create2.forward.fake'
| 'chronocat.internal.message.create2.poke'
| 'chronocat.internal.message.create2.markdown' =
'chronocat.internal.message.create2.normal'
const forwards = cctx.chronocat.h
.select(payloadRich.content, 'message')
@ -103,8 +113,8 @@ async function messageCreateUsingJson({
return
if (forward!.children.every((x) => x.attrs['id']))
method = 'chronocat.internal.message.create.forward'
else method = 'chronocat.internal.message.create.forward.fake'
method = 'chronocat.internal.message.create2.forward'
else method = 'chronocat.internal.message.create2.forward.fake'
}
const pokes = cctx.chronocat.h.select(
@ -113,7 +123,7 @@ async function messageCreateUsingJson({
)
if (pokes.length) {
// TODO: 如果单条消息内除了 poke 还有其他元素,打印警告
method = 'chronocat.internal.message.create.poke'
method = 'chronocat.internal.message.create2.poke'
}
const markdowns = cctx.chronocat.h.select(
@ -122,8 +132,25 @@ async function messageCreateUsingJson({
)
if (markdowns.length) {
// 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[],
]
'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': [
[RedMessage, ChronocatSatoriServerConfig],
Event[] | undefined,