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: alias替换shortcut出现问题 #1440

Open
ChengTu-Lazy opened this issue Aug 8, 2024 · 4 comments
Open

Bug: alias替换shortcut出现问题 #1440

ChengTu-Lazy opened this issue Aug 8, 2024 · 4 comments

Comments

@ChengTu-Lazy
Copy link

Describe the bug

我写的插件是:https://github.com/ChengTu-Lazy/koishi-plugin-dst-search
其中一个指令是 [.] 或者 [。]
用alias注册指令时就报错了

Steps to reproduce

ctx.command("test [number]")
  // .shortcut(/^\.(\d+)$/, { args: ['$1'] })
  // .shortcut(/^\。(\d+)$/, { args: ['$1'] })
  .alias(".", { args: ['$1'] })
  .action((Session , numberStr) =>{
    return "test"+numberStr
  })

Expected behavior

按例子来说,不用alias直接用我注释掉的shrtcut的话,输入.1应当回复test1
但实际上这样的写法会直接报错

Screenshots

2024-08-08 16:44:41 [I] hmr reload plugin at external\dst-test\src\index.ts
2024-08-08 16:44:41 [E] app TypeError: Cannot read properties of null (reading 'name')
                            at Command._registerAlias (E:\Github\Koishi\koishi-app\node_modules\@koishijs\core\src\command\command.ts:103:50)
                            at Command.alias (E:\Github\Koishi\koishi-app\node_modules\@koishijs\core\src\command\command.ts:142:12)     
                            at Object.apply (e:\Github\Koishi\koishi-app\external\dst-test\src\index.ts:14:4)
                            at MainScope.apply (E:\Github\Koishi\koishi-app\node_modules\@cordisjs\core\src\scope.ts:356:26)
                            at <anonymous> (E:\Github\Koishi\koishi-app\node_modules\@cordisjs\core\src\scope.ts:383:49)
                            at MainScope.ensure (E:\Github\Koishi\koishi-app\node_modules\@cordisjs\core\src\scope.ts:150:18)
                            at MainScope.start (E:\Github\Koishi\koishi-app\node_modules\@cordisjs\core\src\scope.ts:383:12)
                            at MainScope.init (E:\Github\Koishi\koishi-app\node_modules\@cordisjs\core\src\scope.ts:189:12)
                            at MainScope (E:\Github\Koishi\koishi-app\node_modules\@cordisjs\core\src\scope.ts:321:12)
                            at Proxy.plugin (E:\Github\Koishi\koishi-app\node_modules\@cordisjs\core\src\registry.ts:188:15)

Versions

  • OS: Windows 11 10.0.22631
  • Platform: 沙盒
  • Node version: 18.20.0
  • Koishi version: 4.17.10

System:
OS: Windows 11 10.0.22631
CPU: (16) x64 13th Gen Intel(R) Core(TM) i5-1340P

Binaries:
Node: 18.20.0
Yarn: 4.1.1

Koishi:
Core: 4.17.10
Console: 5.29.3

Additional context

我有看到文档说如果用户加载的多个插件都注册了同一个指令别名,那么后一个加载的插件将直接加载失败。
实际上shortcut的正则匹配可以实现我的需求,但是它要被alias替换了,以后某个版本可能就用不了了

@ChengTu-Lazy ChengTu-Lazy added the bug BUG label Aug 8, 2024
@DGCK81LNN
Copy link
Contributor

“.”在指令名中表示点或空格分隔的子指令。

不是所有情况都适合用 alias,请自行编写中间件或使用 dialogue 插件代替。

@ChengTu-Lazy
Copy link
Author

“.”在指令名中表示点或空格分隔的子指令。

不是所有情况都适合用 alias,请自行编写中间件或使用 dialogue 插件代替。

我懂你的意思了,把正则匹配这一段放到中间件中进行对吗,不过我还是希望能把正则匹配放到alias里,这样就能无痛更换了

@DGCK81LNN
Copy link
Contributor

DGCK81LNN commented Aug 12, 2024

不管怎样,句点在指令名中属于特殊字符。而且就算可以注册名称为一个句点的指令,调用时也必须在后面加一个空格才能传入参数,同时即使句点后面没有数字,也会被认为是在调用该指令,这可能影响 Koishi 的其他操作逻辑,如回复句点来接受拼写纠正。

@shigma
Copy link
Member

shigma commented Aug 13, 2024

希望能把正则匹配放到 alias 里

我们之所以不推荐使用 shortcut 就是因为有正则。正则表达式的平台支持比较糟糕,并且不好的正则也会有一系列性能问题。

如果非常需要使用正则的话还是继续用 shortcut 吧,未来也可能有其他方案提供。

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

No branches or pull requests

3 participants