Skip to content
/ Shiro Public
forked from MisakaTAT/Shiro

基于OneBot协议的QQ机器人快速开发框架

License

Notifications You must be signed in to change notification settings

alexskim/Shiro

 
 

Repository files navigation

Shiro

Shiro

✨ 基于 OneBot 协议的 QQ机器人 快速开发框架 ✨

maven issues license jdk-version qq-group

文档 · 下载 · 快速开始 · 参与贡献

Repobeats analytics image

Migration Guide

v2 版本开始仅支持 JDK 17+SpringBoot 3.0.0+

详见项目文档 v2迁移指南

QuickStart

依赖引入

引入依赖时请替换版本 latestMaven Central 实际的最新版本

Maven

<dependency>
    <groupId>com.mikuac</groupId>
    <artifactId>shiro</artifactId>
    <version>latest</version>
</dependency>

Gradle Kotlin DSL

implementation("com.mikuac:shiro:latest")

Gradle Groovy DSL

implementation 'com.mikuac:shiro:latest'

示例插件

注解调用

编写 application.yaml 配置文件 或参考 进阶配置文件

server:
  port: 5000
@Shiro
@Component
public class ExamplePlugin {
    // 更多用法详见 @MessageHandlerFilter 注解源码

    // 当机器人收到的私聊消息消息符合 cmd 值 "hi" 时,这个方法会被调用。
    @PrivateMessageHandler
    @MessageHandlerFilter(cmd = "hi")
    public void fun1(Bot bot, PrivateMessageEvent event, Matcher matcher) {
        // 构建消息
        String sendMsg = MsgUtils.builder().face(66).text("Hello, this is shiro demo.").build();
        // 发送私聊消息
        bot.sendPrivateMsg(event.getUserId(), sendMsg, false);
    }

    // 如果 at 参数设定为 AtEnum.NEED 则只有 at 了机器人的消息会被响应
    @GroupMessageHandler
    @MessageHandlerFilter(at = AtEnum.NEED)
    public void fun2(GroupMessageEvent event) {
        // 以注解方式调用可以根据自己的需要来为方法设定参数
        // 例如群组消息可以传递 GroupMessageEvent, Bot, Matcher 多余的参数会被设定为 null
        System.out.println(event.getMessage());
    }

    // 同时监听群组及私聊消息 并根据消息类型(私聊,群聊)回复
    @AnyMessageHandler
    @MessageHandlerFilter(cmd = "say hello")
    public void fun3(Bot bot, AnyMessageEvent event) {
        bot.sendMsg(event, "hello", false);
    }
}

重写父类方法

  • 注解方式编写的插件无需在插件列表 plugin-list定义
  • 服务端配置文件 resources/application.yaml 追加如下内容
  • 插件列表为顺序执行,如果前一个插件返回了 MESSAGE_BLOCK 将不会执行后续插件

编写 application.yaml 配置文件 或参考 进阶配置文件

server:
  port: 5000
shiro:
  plugin-list:
    - com.example.bot.plugins.ExamplePlugin
@Component
public class ExamplePlugin extends BotPlugin {

    @Override
    public int onPrivateMessage(Bot bot, PrivateMessageEvent event) {
        if ("hi".equals(event.getMessage())) {
            // 构建消息
            String sendMsg = MsgUtils.builder()
                    .face(66)
                    .text("hello, this is shiro example plugin.")
                    .build();
            // 发送私聊消息
            bot.sendPrivateMsg(event.getUserId(), sendMsg, false);
        }
        // 返回 MESSAGE_IGNORE 执行 plugin-list 下一个插件,返回 MESSAGE_BLOCK 则不执行下一个插件
        return MESSAGE_IGNORE;
    }

    @Override
    public int onGroupMessage(Bot bot, GroupMessageEvent event) {
        if ("hi".equals(event.getMessage())) {
            // 构建消息
            String sendMsg = MsgUtils.builder()
                    .at(event.getUserId())
                    .face(66)
                    .text("hello, this is shiro example plugin.")
                    .build();
            // 发送群消息
            bot.sendGroupMsg(event.getGroupId(), sendMsg, false);
        }
        // 返回 MESSAGE_IGNORE 执行 plugin-list 下一个插件,返回 MESSAGE_BLOCK 则不执行下一个插件
        return MESSAGE_IGNORE;
    }

}

Client

Shiro 以 OneBot-v11 标准协议进行开发,兼容所有支持反向WebSocket的OneBot协议客户端

项目地址 平台 核心作者 备注
Yiwen-Chan/OneBot-YaYa 先驱 kanri
richardchien/coolq-http-api CKYU richardchien 可在 Mirai 平台使用 mirai-native 加载
Mrs4s/go-cqhttp MiraiGo Mrs4s
yyuueexxiinngg/OneBot-Mirai Mirai yyuueexxiinngg
takayama-lily/onebot OICQ takayama

Contributors

contributors

Credits

License

This product is licensed under the GNU General Public License version 3. The license is as published by the Free Software Foundation published at https://www.gnu.org/licenses/gpl-3.0.html.

Alternatively, this product is licensed under the GNU Lesser General Public License version 3 for non-commercial use. The license is as published by the Free Software Foundation published at https://www.gnu.org/licenses/lgpl-3.0.html.

Feel free to contact us if you have any questions about licensing or want to use the library in a commercial closed source product.

Thanks

JetBrains offers free licenses to support open source projects.

Stargazers over time

Stargazers over time

About

基于OneBot协议的QQ机器人快速开发框架

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%