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

支持 多个主指令名相同的CompositeCommand注册的SubCommand均生效 #1804

Open
hundun000 opened this issue Aug 10, 2021 · 2 comments · May be fixed by #2125
Open

支持 多个主指令名相同的CompositeCommand注册的SubCommand均生效 #1804

hundun000 opened this issue Aug 10, 2021 · 2 comments · May be fixed by #2125
Labels
N 优先级: 一般 s:console 子系统: mirai-console t:enhancement 类型: 现有功能上的优化
Milestone

Comments

@hundun000
Copy link
Contributor

目前当多个主指令名相同的CompositeCommand注册时,不论override真假,最终只有唯一的CompositeCommand(中的SubCommand)生效。

设想需求是一个XX助手插件需要如下指令集:

  • /XX助手 刷新b站订阅数据
  • /XX助手 新增b站订阅
  • /XX助手(b站的其他子指令)……
  • /XX助手 刷新微博订阅数据
  • /XX助手 新增微博订阅
  • /XX助手(微博的其他子指令)……
  • ……
  • /XX助手(其他模块的数个子指令)
  • ……

按照目前的情况,只能把以上所有SubCommand放在同一个CompositeCommand,其用XX助手作为主指令名。

理想的情况是编写BilibiliCommand、WeiboCommand……,多个CompositeCommand分别拥有各自的SubCommand,均用XX助手作为主指令名。注册后,这些CompositeCommand的SubCommand均生效。

@Him188 Him188 transferred this issue from mamoe/mirai-console Jan 4, 2022
@Him188 Him188 added N 优先级: 一般 s:console 子系统: mirai-console t:enhancement 类型: 现有功能上的优化 labels Jan 4, 2022
@Him188 Him188 added this to the Backlog milestone Jan 4, 2022
@Him188
Copy link
Member

Him188 commented Jan 4, 2022

在内部实现上是先匹配主指令名再由 CompositeCommand 实例去处理子指令,不容易实现你所提议的

@hundun000
Copy link
Contributor Author

我现在通过如下方式间接实现了。不过ParentCommand里需要人工编写数个方法(人工编写委托关系),不知是否可以进一步优化成自动化。

MyPlugin.onEnable() {
    // 只注册ParentCommand
    CommandManager.INSTANCE.registerCommand(new ParentCommand(), false);
}

public class ParentCommand extends CompositeCommand {
    
    BilibiliCommand bilibiliCommand = new BilibiliCommand();
    WeiboiCommand weiboiCommand = new WeiboiCommand();

    public ParentCommand() {
        super("XX助手")
    }

    @SubCommand("查询b站动态")
    public void bilibiliCommandWork(CommandSender sender) {
        bilibiliCommand.work(sender);
    }

    @SubCommand("查询微博动态")
    public void weiboiCommandWork(CommandSender sender) {
        weiboiCommand.work(sender);
    }
}

// 其实可以不extends CompositeCommand, 因为不会注册到CommandManager
public class BilibiliCommand extends CompositeCommand {

    public BilibiliCommand() {
        // 无用的PrimaryName, 因为不会注册到CommandManager
        super("uselessName0")
    }

    // 其实可以不@SubCommand, 因为不会注册到CommandManager
    @SubCommand("查询b站动态")
    public void work(CommandSender sender) {
        // do something
    }
}

public class WeiboCommand extends CompositeCommand {

    public WeiboCommand() {
        super("uselessName1")
    }

    @SubCommand("查询微博动态")
    public void work(CommandSender sender) {
        // do something
    }
}

@hundun000 hundun000 linked a pull request Jun 30, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
N 优先级: 一般 s:console 子系统: mirai-console t:enhancement 类型: 现有功能上的优化
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants