Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: session.execute("", true) 错误地将消息组件序列化成字符串 #1409

Closed
DGCK81LNN opened this issue Jun 4, 2024 · 1 comment
Labels
bug BUG

Comments

@DGCK81LNN
Copy link
Contributor

DGCK81LNN commented Jun 4, 2024

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

if (!shouldEmit) {
if (typeof result === 'string') return result
return h(null, result).toString()
}

一种可能的解决方案:对指令结果执行 session.transform() 展开成基本元素后再以字符串形式返回。

或许可以让 session.execute() 提供一个新的 transform?: boolean 参数,默认为 true;设为 false 时,直接将指令结果原样返回(或 h.parse 后返回)。这样即可在不改变 session.execute(___, true) 返回值类型,且保证其有一个合理的返回值的前提下,允许从外部获取指令所返回的未经解析的消息组件结构。

@DGCK81LNN DGCK81LNN added the bug BUG label Jun 4, 2024
@DGCK81LNN DGCK81LNN changed the title Bug: session.execute("", true) 错误地将消息元素序列化成字符串 Bug: session.execute("", true) 错误地将消息组件序列化成字符串 Jun 4, 2024
@shigma
Copy link
Member

shigma commented Jun 4, 2024

cc @MaikoTan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug BUG
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants