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

🐛 fix route path of command system, add missed command event. #50

Merged
merged 3 commits into from
Jul 7, 2022

Conversation

NFWSA
Copy link
Contributor

@NFWSA NFWSA commented Jul 6, 2022

想要尝试一下command相关的接口,发现现在的支持有问题
根据源文件 mirai-http-api paths.kt ,mirai-api-http 的 comand 的接口路由现在是 cmd 开始,下面有 cmd/registercmd/execute ,修改于15个月前
而 mirai-ts 的实现中,相关接口仍然写的是 command/registercommand/send ,所以修复了路由,同时把 Command.send 接口名字也改成了 Command.execute 保证一致
另外 EventMap 中缺乏 CommandExecute 事件,导致 mirai-api-http 转发的接口调用事件(参考mirai-http-api api event)没法直接监听,所以补上了这部分。

src/types/event-type.ts Outdated Show resolved Hide resolved
@NFWSA NFWSA requested a review from YunYouJun July 7, 2022 03:47
@YunYouJun YunYouJun merged commit 704346f into YunYouJun:master Jul 7, 2022
@YunYouJun
Copy link
Owner

Released in v2.4.0.

Could you test it?

@NFWSA
Copy link
Contributor Author

NFWSA commented Jul 8, 2022

// file command-test.ts
// ...

async function app() {
    await mirai.link(qq);
    mirai.on('message', (msg) => {
        // eslint-disable-next-line no-console
        // console.log(msg)
    });

    mirai.api.command.register("test", ['command', 'alias'], 'test command interface', 'test <args>...');
    mirai.on("CommandExecutedEvent", (data) => {
        if (data.name == 'test' || data.name == 'command' || data.name == 'alias') {
            console.log(data);
            if (data.friend != null) {
                mirai.api.sendFriendMessage(data.args, data.friend.id);
            }
            else if (data.member != null) {
                mirai.api.sendGroupMessage(data.args, data.member.group.id);
            }
            else {
                mirai.logger.debug(data.args);
            }
        }
    });
    mirai.listen();
}

app();

yeah, here is my test code and runtime result is below:

# ./mcl --boot-only
## after run ts-node command-test.ts
> help
# ...test <args>...
## result for send friend message:
2022-07-08 09:59:13 V/Bot.*********: Event: net.mamoe.mirai.api.http.command.CommandExecutedEvent@46b5fda1
2022-07-08 09:59:13 V/Bot.*********: 杯弓蛇影(*********) -> #test qq friend cmd test
2022-07-08 09:59:13 V/Bot.*********: Friend(*********) <- qqfriendcmdtest
## result for send group member message:
2022-07-08 09:59:37 V/Bot.*********: [Test(*********)] 测试名(*********) -> #test qq member cmd test
2022-07-08 09:59:37 V/Bot.*********: Event: net.mamoe.mirai.api.http.command.CommandExecutedEvent@c7e430e
2022-07-08 09:59:38 V/Bot.*********: Group(*********) <- qqmembercmdtest
## result for console command:
> test 123 456 test
2022-07-08 09:59:50 V/Bot.*********: Event: net.mamoe.mirai.api.http.command.CommandExecutedEvent@66a92375
# yarn upgrade
yarn upgrade v1.22.19
warning package.json: No license field
warning No license field
warning No license field
warning No license field
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Rebuilding all packages...
success Saved lockfile.
warning No license field
success Saved 62 new dependencies.
info Direct dependencies
├─ @types/js-yaml@4.0.5
├─ @types/jsdom@16.2.14
├─ @types/node@18.0.3
├─ fs@0.0.1-security
├─ js-yaml@4.1.0
├─ jsdom@20.0.0
└─ mirai-ts@2.4.0
...

# ts-node command-test.ts 
[mirai-api-http] [info] [http] Address: http://localhost:8080
[mirai-api-http] [info] [http] Session: itmOgxPM
[mirai-api-http] [success] 验证成功
[mirai-api-http] [info] [websocket] [all](消息与事件) ws://localhost:8080
[mirai-api-http] [info] [ws] Session: NnnVOlAR
## result for send friend message:
{
  type: 'CommandExecutedEvent',
  name: 'test',
  friend: { id: 000000000, nickname: '杯弓蛇影', remark: '****' },
  member: null,
  args: [
    { type: 'Plain', text: 'qq' },
    { type: 'Plain', text: 'friend' },
    { type: 'Plain', text: 'cmd' },
    { type: 'Plain', text: 'test' }
  ],
  reply: [AsyncFunction (anonymous)]
}
## result for send group member message:
{
  type: 'CommandExecutedEvent',
  name: 'test',
  friend: null,
  member: {
    id: 000000000,
    memberName: '测试名',
    specialTitle: '',
    permission: 'OWNER',
    joinTimestamp: 1529044925,
    lastSpeakTimestamp: 1657245577,
    muteTimeRemaining: 0,
    group: { id: 000000000, name: 'Test', permission: 'MEMBER' }
  },
  args: [
    { type: 'Plain', text: 'qq' },
    { type: 'Plain', text: 'member' },
    { type: 'Plain', text: 'cmd' },
    { type: 'Plain', text: 'test' }
  ],
  reply: [AsyncFunction (anonymous)]
}
## result for console command:
{
  type: 'CommandExecutedEvent',
  name: 'test',
  friend: null,
  member: null,
  args: [
    { type: 'Plain', text: '123' },
    { type: 'Plain', text: '456' },
    { type: 'Plain', text: 'test' }
  ],
  reply: [AsyncFunction (anonymous)]
}
[mirai-api-http] [debug] [
  { type: 'Plain', text: '123' },
  { type: 'Plain', text: '456' },
  { type: 'Plain', text: 'test' }
]

so I think it works fine, if you needed, i can send runtime picture (I used text rather than image, beacuse there isn't suitable gallery website.

@YunYouJun
Copy link
Owner

That's ok. Have fun!

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

Successfully merging this pull request may close these issues.

2 participants