2023-12-11 10:44:23 +08:00
|
|
|
package initialization
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go-wechat/common/current"
|
|
|
|
"go-wechat/model"
|
|
|
|
plugin "go-wechat/plugin"
|
|
|
|
"go-wechat/plugin/plugins"
|
2023-12-22 09:58:27 +08:00
|
|
|
"go-wechat/service"
|
2023-12-11 10:44:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Plugin
|
|
|
|
// @description: 初始化插件
|
|
|
|
func Plugin() {
|
|
|
|
// 定义一个处理器
|
|
|
|
dispatcher := plugin.NewMessageMatchDispatcher()
|
|
|
|
// 设置为异步处理
|
|
|
|
dispatcher.SetAsync(true)
|
|
|
|
|
|
|
|
// 注册插件
|
|
|
|
|
|
|
|
// 保存消息进数据库
|
|
|
|
dispatcher.RegisterHandler(func(*model.Message) bool {
|
|
|
|
return true
|
|
|
|
}, plugins.SaveToDb)
|
|
|
|
|
2023-12-20 15:42:50 +08:00
|
|
|
// 私聊指令消息
|
|
|
|
dispatcher.RegisterHandler(func(m *model.Message) bool {
|
2023-12-22 09:58:27 +08:00
|
|
|
// 私聊消息 或 群聊艾特机器人并且以/开头的消息
|
2024-01-12 21:50:56 +08:00
|
|
|
isGroupAt := m.IsAt() && !m.IsAtAll()
|
|
|
|
return (m.IsPrivateText() || isGroupAt) && m.CleanContentStartWith("/") && service.CheckIsEnableCommand(m.FromUser)
|
2023-12-20 15:42:50 +08:00
|
|
|
}, plugins.Command)
|
|
|
|
|
2023-12-11 10:44:23 +08:00
|
|
|
// AI消息插件
|
|
|
|
dispatcher.RegisterHandler(func(m *model.Message) bool {
|
|
|
|
// 群内@或者私聊文字消息
|
2024-01-12 21:50:56 +08:00
|
|
|
return (m.IsAt() && !m.IsAtAll()) || m.IsPrivateText()
|
2023-12-11 10:44:23 +08:00
|
|
|
}, plugins.AI)
|
|
|
|
|
|
|
|
// 欢迎新成员
|
|
|
|
dispatcher.RegisterHandler(func(m *model.Message) bool {
|
|
|
|
return m.IsNewUserJoin()
|
|
|
|
}, plugins.WelcomeNew)
|
|
|
|
|
|
|
|
// 注册消息处理器
|
|
|
|
current.SetRobotMessageHandler(plugin.DispatchMessage(dispatcher))
|
|
|
|
}
|