<div style="text-align:center">潇<b style="color:#39c5bb">小湘</b></div>
或许不烦的 mirai 机器人
潇小湘 是基于 mirai 的 QQ 机器人。以 mirai 插件的形式开发和发布。
应 mirai 倡议,本机器人采用 AGPLv3 协议开源。
许可证
XiaoXiang, May not be an annoying mirai bot
Copyright (C) 2022 Operacon.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
To contact the author, E-Mail <operacon@outlook.com>
潇小湘 仅以学习和娱乐为目的进行开发。尝试对此类机器人进行一定架构,使得项目更加清晰,便于定制、添加功能和二次开发;同时为像作者一样基础一般的开发者服务。
项目结构
kotlin
|-------component
| |-------项目中用到的全局单例、配置文件以及其他 bean
|
|-------controller
| |-------所有 event listener,listener 中不包含
| 业务逻辑,只会调用其中 service 的 scan()
|
|-------service
| |-------所有 service 的业务逻辑,建议按服务对象分包
|
|-------XiaoXiang.kt
为便于理解,以处理一条群聊消息为例,给出处理流程:
QQ 收到群聊消息,mirai 发出 GroupMessageEvent
在 XiaoXiang.kt 中注册的 listener 类被实例化,monitor() 函数被调用
作为 controller 的 listener 预处理消息文本
listener 将预处理的文本发给其中第一个 service 的 scan() 函数
scan() 发现不调用自己,return false
listener 将预处理的文本发给其中第二个 service 的 scan() 函数
scan() 发现不调用自己,return false
...
listener 将预处理的文本发给其中第 n 个 service 的 scan() 函数
scan() 发现调用自己,进行相应的业务逻辑的处理(处理中可能用到 component),return true
listener 发现本条消息处理完毕或者没有触发任何一个 service,return
monitor() 函数结束
event 处理结束
一条消息在同一插件里触发多个功能可能导致混乱,按如上所示的逻辑,保证了一条消息至多触发一个功能。