koishi
koishi copied to clipboard
Bug: `session.execute("", true)` 错误地将消息组件序列化成字符串
Describe the bug
session.execute(___, true)
时,无法用字符串正确地表示的消息元素被错误地序列化成了字符串。
Steps to reproduce
ctx.command("foo").action(() => (
<html>
<div style={{ backgroundColor: green }}>Hello, world!</div>
</html>
))
ctx.command("bar").action(async ({ session }) => {
return (
<>
<p>foo 指令的执行结果:</p>
{h.parse(await session.execute("foo", true))} // <-- <html><div style="[object Object]">Hello, world!</div></html>
</>
)
})
Expected behavior
session.execute(___, true)
应提供一种方法以允许取得含有消息组件的指令结果的元素形式,或始终将消息组件展开成基本元素后以字符串形式返回,而不是直接将可能含有消息组件的内容序列化成字符串。
Screenshots
No response
Versions
- Node version: 20.11.1
- Koishi version: 4.17.6
Additional context
https://github.com/koishijs/koishi/blob/5f1610e861bb4dd02200adccd5a7058eed2212e0/packages/core/src/session.ts#L395-L398
一种可能的解决方案:对指令结果执行 session.transform()
展开成基本元素后再以字符串形式返回。
或许可以让 session.execute()
提供一个新的 transform?: boolean
参数,默认为 true
;设为 false
时,直接将指令结果原样返回(或 h.parse
后返回)。这样即可在不改变 session.execute(___, true)
返回值类型,且保证其有一个合理的返回值的前提下,允许从外部获取指令所返回的未经解析的消息组件结构。