diff --git a/Bili/main.go b/Bili/main.go index b634e13..6b28ef6 100644 --- a/Bili/main.go +++ b/Bili/main.go @@ -99,7 +99,7 @@ func (m *Module) ModuleInit(b *Core.Bot, l *logrus.Entry) error { } } }) - err := b.AddEvent(OPQBot.EventNameOnGroupMessage, func(botQQ int64, packet *OPQBot.GroupMsgPack) { + _, err := b.AddEvent(OPQBot.EventNameOnGroupMessage, func(botQQ int64, packet *OPQBot.GroupMsgPack) { if packet.FromUserID == botQQ { return } diff --git a/Config/config.go b/Config/config.go index 4679308..075c223 100644 --- a/Config/config.go +++ b/Config/config.go @@ -42,6 +42,10 @@ type CoreConfigStruct struct { Key string Secret string } + Moli struct { + Key string + Secret string + } } HtmlToImgUrl string DisableModule []string diff --git a/GroupManager/Chat/Local/main.go b/GroupManager/Chat/Local/main.go index 2a8dc47..4549c5f 100644 --- a/GroupManager/Chat/Local/main.go +++ b/GroupManager/Chat/Local/main.go @@ -19,6 +19,7 @@ type LocalChat struct { GroupId int64 Question string Answer string + From int64 } func (c *Core) Init(l *logrus.Entry) error { @@ -49,6 +50,7 @@ func (c *Core) AddAnswer(question, answer string, GroupId, userId int64) error { GroupId: GroupId, Question: question, Answer: answer, + From: userId, } if err := Config.DB.Create(&tmp).Error; err != nil { return err diff --git a/GroupManager/Chat/Moli/main.go b/GroupManager/Chat/Moli/main.go new file mode 100644 index 0000000..2f6a407 --- /dev/null +++ b/GroupManager/Chat/Moli/main.go @@ -0,0 +1,53 @@ +package Moli + +import ( + "OPQBot-QQGroupManager/Config" + "OPQBot-QQGroupManager/GroupManager/Chat" + "errors" + "github.com/mcoo/requests" + "github.com/sirupsen/logrus" +) + +var log *logrus.Entry + +type Core struct { + replace map[string]string + key string + secret string +} + +func (c *Core) GetAnswer(question string, GroupId, userId int64) string { + res, err := requests.Get("http://i.itpk.cn/api.php?question=" + question + "&limit=8&api_key=" + c.key + "&api_secret=" + c.secret) + if err != nil { + log.Error(err) + return "" + } + return res.Text() +} + +func (c *Core) AddAnswer(question, answer string, GroupId, userId int64) error { + return nil +} + +func (c *Core) SetReplace(regexp string, target string) error { + return nil +} + +func (c *Core) Init(l *logrus.Entry) error { + log = l + Config.Lock.RLock() + c.key = Config.CoreConfig.ChatKey.Moli.Key + c.secret = Config.CoreConfig.ChatKey.Moli.Secret + Config.Lock.RUnlock() + c.replace = map[string]string{"[cqname]": "[YOU]", "[name]": "我"} + if c.key == "" || c.secret == "" { + return errors.New("key和密匙没有填写") + } + return nil +} +func init() { + err := Chat.Register("茉莉", &Core{}) + if err != nil { + panic(err) + } +} diff --git a/GroupManager/Chat/main.go b/GroupManager/Chat/main.go index a2305cc..bc776b2 100644 --- a/GroupManager/Chat/main.go +++ b/GroupManager/Chat/main.go @@ -33,6 +33,7 @@ func StartChatCore(l *logrus.Entry) Manager { err := v.Init(tmp) if err != nil { tmp.Error(err) + delete(Providers, k) } tmp.Info("载入成功") } @@ -46,6 +47,13 @@ func StartChatCore(l *logrus.Entry) Manager { func init() { Providers = make(map[string]ChatCore) } +func (m *Manager) Learn(Question, Answer string, GroupId, From int64) error { + if v, ok := Providers["local"]; ok { + return v.AddAnswer(Question, Answer, GroupId, From) + } else { + return errors.New("本地聊天系统出现故障") + } +} func (m *Manager) SetChatDB(db string) error { if _, ok := Providers[db]; ok { m.SelectCore = db @@ -64,12 +72,21 @@ func (m *Manager) GetAnswer(question string, groupId, userId int64) (string, err return answer, nil } } - // 联网对话数据库 + // 联网查询默认对话数据库 if v, ok := Providers[m.SelectCore]; ok { if answer := v.GetAnswer(question, groupId, userId); answer != "" { return answer, nil } } + // 遍历其他数据库 + for k, v := range Providers { + if k == "local" || k == m.SelectCore { + continue + } + if answer := v.GetAnswer(question, groupId, userId); answer != "" { + return answer, nil + } + } return "", errors.New("没有找到对话记录,无法回答") } func Register(name string, core ChatCore) error { diff --git a/GroupManager/main.go b/GroupManager/main.go index 0430706..4dc5946 100644 --- a/GroupManager/main.go +++ b/GroupManager/main.go @@ -5,7 +5,8 @@ import ( "OPQBot-QQGroupManager/Core" "OPQBot-QQGroupManager/GroupManager/Chat" _ "OPQBot-QQGroupManager/GroupManager/Chat/Local" - _ "OPQBot-QQGroupManager/GroupManager/Chat/XiaoI" + //_ "OPQBot-QQGroupManager/GroupManager/Chat/XiaoI" + _ "OPQBot-QQGroupManager/GroupManager/Chat/Moli" _ "OPQBot-QQGroupManager/GroupManager/Chat/Zhai" "OPQBot-QQGroupManager/GroupManager/QunInfo" "OPQBot-QQGroupManager/draw" @@ -66,7 +67,7 @@ func (m *Module) ModuleInit(b *Core.Bot, l *logrus.Entry) error { }{} VerifyLock := sync.Mutex{} qun := QunInfo.NewQun(b) - err := b.AddEvent(OPQBot.EventNameOnGroupJoin, func(botQQ int64, packet *OPQBot.GroupJoinPack) { + _, err := b.AddEvent(OPQBot.EventNameOnGroupJoin, func(botQQ int64, packet *OPQBot.GroupJoinPack) { Config.Lock.RLock() var c Config.GroupConfig if v, ok := Config.CoreConfig.GroupConfig[packet.EventMsg.FromUin]; ok { @@ -137,7 +138,7 @@ func (m *Module) ModuleInit(b *Core.Bot, l *logrus.Entry) error { } }) // 接受处理解禁功能 - err = b.AddEvent(OPQBot.EventNameOnFriendMessage, func(qq int64, packet *OPQBot.FriendMsgPack) { + _, err = b.AddEvent(OPQBot.EventNameOnFriendMessage, func(qq int64, packet *OPQBot.FriendMsgPack) { if packet.FromUin != b.QQ { //log.Println(packet.Content) content := map[string]interface{}{} @@ -243,7 +244,7 @@ func (m *Module) ModuleInit(b *Core.Bot, l *logrus.Entry) error { log.Error(err) } chat := Chat.StartChatCore(log.WithField("Func", "Chat")) - err = b.AddEvent(OPQBot.EventNameOnGroupMessage, func(botQQ int64, packet *OPQBot.GroupMsgPack) { + _, err = b.AddEvent(OPQBot.EventNameOnGroupMessage, func(botQQ int64, packet *OPQBot.GroupMsgPack) { if packet.FromUserID == botQQ { return } @@ -259,7 +260,7 @@ func (m *Module) ModuleInit(b *Core.Bot, l *logrus.Entry) error { return } - if v, _ := regexp.MatchString(`[0-9]{6}`, packet.Content); v { + if v, _ := regexp.MatchString(`^[0-9]{6}$`, packet.Content); v { VerifyLock.Lock() if v1, ok := VerifyNum[strconv.FormatInt(packet.FromUserID, 10)+"|"+strconv.FormatInt(packet.FromGroupID, 10)]; ok { if v1.Code == packet.Content { @@ -486,10 +487,74 @@ func (m *Module) ModuleInit(b *Core.Bot, l *logrus.Entry) error { return } if packet.Content == "当前聊天数据库" { + if chat.SelectCore == "" { + b.SendGroupTextMsg(packet.FromGroupID, "当前没有设置") + return + } b.SendGroupTextMsg(packet.FromGroupID, "设置聊天数据库为"+chat.SelectCore) return } + log.Println(packet.Content) cm := strings.Split(packet.Content, " ") + if len(cm) == 2 && cm[0] == "拉黑" { + Config.Lock.Lock() + log.Println(cm) + if Config.CoreConfig.SuperAdminUin == packet.FromUserID { + if uin, err := strconv.ParseInt(cm[1], 10, 64); err == nil { + Config.CoreConfig.BanQQ = append(Config.CoreConfig.BanQQ, uin) + b.SendGroupTextMsg(packet.FromGroupID, "已经拉黑了") + } else { + log.Error(err) + b.SendGroupTextMsg(packet.FromGroupID, "命令有问题") + } + + } else { + b.SendGroupTextMsg(packet.FromGroupID, "权限不足") + } + Config.Save() + Config.Lock.Unlock() + return + } + if len(cm) == 2 && cm[0] == "取消拉黑" { + Config.Lock.Lock() + if Config.CoreConfig.SuperAdminUin == packet.FromUserID { + if uin, err := strconv.ParseInt(cm[1], 10, 64); err != nil { + for i, v := range Config.CoreConfig.BanQQ { + if v == uin { + Config.CoreConfig.BanQQ = append(Config.CoreConfig.BanQQ[:i], Config.CoreConfig.BanQQ[i+1:]...) + } + } + b.SendGroupTextMsg(packet.FromGroupID, "已经拉黑了") + } else { + b.SendGroupTextMsg(packet.FromGroupID, "命令有问题") + } + } else { + b.SendGroupTextMsg(packet.FromGroupID, "权限不足") + } + Config.Save() + Config.Lock.Unlock() + return + } + if packet.Content == "黑名单" { + Config.Lock.RLock() + s := "黑名单列表" + for _, v := range Config.CoreConfig.BanQQ { + s += "\n" + strconv.FormatInt(v, 10) + } + b.SendGroupTextMsg(packet.FromGroupID, s) + Config.Lock.RUnlock() + return + } + if len(cm) >= 3 && cm[0] == "教你" { + tmp := strings.SplitN(packet.Content, " ", 3) + err := chat.Learn(tmp[1], tmp[2], packet.FromGroupID, packet.FromUserID) + if err != nil { + b.SendGroupTextMsg(packet.FromGroupID, "学习出现问题") + return + } + b.SendGroupTextMsg(packet.FromGroupID, "已经学会了") + return + } if len(cm) == 2 && cm[0] == "设置聊天数据库" { err = chat.SetChatDB(cm[1]) if err != nil { @@ -499,23 +564,24 @@ func (m *Module) ModuleInit(b *Core.Bot, l *logrus.Entry) error { b.SendGroupTextMsg(packet.FromGroupID, "设置聊天数据库为"+cm[1]) return } - if packet.Content == "关闭聊天" { + if a, _ := regexp.MatchString("关闭|无路赛|吵|别说话|闭嘴", packet.Content); a { Config.Lock.Lock() - c.EnableChat = false - Config.CoreConfig.GroupConfig[packet.FromGroupID] = c - Config.Save() + if c.Enable == true { + c.EnableChat = false + b.SendGroupTextMsg(packet.FromGroupID, "关闭了聊天功能") + Config.CoreConfig.GroupConfig[packet.FromGroupID] = c + Config.Save() + } Config.Lock.Unlock() - b.SendGroupTextMsg(packet.FromGroupID, "关闭了聊天功能") return } if c.EnableChat { - answer, err := chat.GetAnswer(packet.Content, packet.FromGroupID, packet.FromUserID) + answer, err := chat.GetAnswer(OPQBot.DecodeFaceFromSentences(packet.Content, "%s"), packet.FromGroupID, packet.FromUserID) if err != nil { log.Warn(err) return } - - b.SendGroupTextMsg(packet.FromGroupID, OPQBot.MacroAt([]int64{packet.FromUserID})+answer) + b.SendGroupTextMsg(packet.FromGroupID, strings.ReplaceAll(answer, "[YOU]", packet.FromNickName)) } }) if err != nil { diff --git a/genAndYiqin/main.go b/genAndYiqin/main.go index 43a74a4..4193764 100644 --- a/genAndYiqin/main.go +++ b/genAndYiqin/main.go @@ -127,7 +127,7 @@ func (m *Module) ModuleInit(b *Core.Bot, l *logrus.Entry) error { fmt.Println(ups) }) - err := b.AddEvent(OPQBot.EventNameOnGroupMessage, func(botQQ int64, packet *OPQBot.GroupMsgPack) { + _, err := b.AddEvent(OPQBot.EventNameOnGroupMessage, func(botQQ int64, packet *OPQBot.GroupMsgPack) { if packet.FromUserID == botQQ { return } diff --git a/githubManager/main.go b/githubManager/main.go index b189e23..5e92aa3 100644 --- a/githubManager/main.go +++ b/githubManager/main.go @@ -28,7 +28,7 @@ func (m *Module) ModuleInfo() Core.ModuleInfo { func (m *Module) ModuleInit(b *Core.Bot, l *logrus.Entry) error { log = l g := NewManager(GroupManager.App, b) - err := b.AddEvent(OPQBot.EventNameOnGroupMessage, func(botQQ int64, packet *OPQBot.GroupMsgPack) { + _, err := b.AddEvent(OPQBot.EventNameOnGroupMessage, func(botQQ int64, packet *OPQBot.GroupMsgPack) { if packet.FromUserID == botQQ { return } @@ -86,7 +86,7 @@ func (m *Module) ModuleInit(b *Core.Bot, l *logrus.Entry) error { return err } - err = b.AddEvent(OPQBot.EventNameOnFriendMessage, func(qq int64, packet *OPQBot.FriendMsgPack) { + _, err = b.AddEvent(OPQBot.EventNameOnFriendMessage, func(qq int64, packet *OPQBot.FriendMsgPack) { s := b.Session.SessionStart(packet.FromUin) if v, err := s.GetString("github"); err == nil { groupidI, err := s.Get("github_groupId") diff --git a/go.mod b/go.mod index 5c104da..cca639a 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/go-ego/gse v0.67.0 github.com/go-playground/webhooks/v6 v6.0.0-beta.3 github.com/kataras/iris/v12 v12.2.0-alpha2.0.20210427211137-fa175eb84754 - github.com/mcoo/OPQBot v0.2.0 + github.com/mcoo/OPQBot v0.1.3-0.20210805075247-38da2137714b github.com/mcoo/gg v1.3.0-edit github.com/mcoo/requests v0.0.2 github.com/mcoo/sqlite v1.1.4-fix-1 diff --git a/main.go b/main.go index ee790ff..c78a605 100644 --- a/main.go +++ b/main.go @@ -57,7 +57,7 @@ func main() { androidDns.SetDns() go CheckUpdate() b := Core.Bot{BotManager: OPQBot.NewBotManager(Config.CoreConfig.OPQBotConfig.QQ, Config.CoreConfig.OPQBotConfig.Url)} - err := b.AddEvent(OPQBot.EventNameOnDisconnected, func() { + _, err := b.AddEvent(OPQBot.EventNameOnDisconnected, func() { log.Println("断开服务器") }) if err != nil { @@ -66,7 +66,7 @@ func main() { b.BotCronManager = utils.NewBotCronManager() b.BotCronManager.Start() b.DB = Config.DB - err = b.AddEvent(OPQBot.EventNameOnConnected, func() { + _, err = b.AddEvent(OPQBot.EventNameOnConnected, func() { log.Println("连接服务器成功") }) if err != nil { diff --git a/setu/main.go b/setu/main.go index b412fd3..db49abf 100644 --- a/setu/main.go +++ b/setu/main.go @@ -35,7 +35,7 @@ func (m *Module) ModuleInit(b *Core.Bot, l *logrus.Entry) error { log = l px := &pixiv.Provider{} RegisterProvider(px, b, b.DB) - err := b.AddEvent(OPQBot.EventNameOnGroupMessage, func(qq int64, packet *OPQBot.GroupMsgPack) { + _, err := b.AddEvent(OPQBot.EventNameOnGroupMessage, func(qq int64, packet *OPQBot.GroupMsgPack) { if packet.FromUserID != b.QQ { //cm := strings.Split(packet.Content, " ") cm := strings.SplitN(packet.Content, " ", 2) diff --git a/setu/pixiv/pixiv.go b/setu/pixiv/pixiv.go index e51dcdc..b33991e 100644 --- a/setu/pixiv/pixiv.go +++ b/setu/pixiv/pixiv.go @@ -102,7 +102,7 @@ func (p *Provider) InitProvider(l *logrus.Entry, b *Core.Bot, db *gorm.DB) { log.Error(err) } } - err := b.AddEvent(OPQBot.EventNameOnFriendMessage, func(qq int64, packet *OPQBot.FriendMsgPack) { + _, err := b.AddEvent(OPQBot.EventNameOnFriendMessage, func(qq int64, packet *OPQBot.FriendMsgPack) { if packet.FromUin != b.QQ { if strings.HasPrefix(packet.Content, "code=") && !p.c.Login { code := strings.TrimPrefix(packet.Content, "code=") diff --git a/taobao/main.go b/taobao/main.go index 463c33e..4c5f6fe 100644 --- a/taobao/main.go +++ b/taobao/main.go @@ -37,7 +37,7 @@ func (m *Module) ModuleInfo() Core.ModuleInfo { func (m *Module) ModuleInit(b *Core.Bot, l *logrus.Entry) error { log = l - err := b.AddEvent(OPQBot.EventNameOnGroupMessage, func(botQQ int64, packet *OPQBot.GroupMsgPack) { + _, err := b.AddEvent(OPQBot.EventNameOnGroupMessage, func(botQQ int64, packet *OPQBot.GroupMsgPack) { if packet.FromUserID == botQQ { return } diff --git a/wordCloud/main.go b/wordCloud/main.go index ba3fa00..99f89a4 100644 --- a/wordCloud/main.go +++ b/wordCloud/main.go @@ -141,7 +141,7 @@ func (m *Module) ModuleInit(b *Core.Bot, l *logrus.Entry) error { m.ImgServer = Config.CoreConfig.HtmlToImgUrl Config.Lock.RUnlock() - err = b.AddEvent(OPQBot.EventNameOnGroupMessage, func(qq int64, packet *OPQBot.GroupMsgPack) { + _, err = b.AddEvent(OPQBot.EventNameOnGroupMessage, func(qq int64, packet *OPQBot.GroupMsgPack) { if packet.FromUserID != b.QQ { if packet.MsgType == "TextMsg" { m.MsgChannel <- *packet