From cc8255d1fe0626f2e6bd116d28e5acd03694e47e Mon Sep 17 00:00:00 2001 From: mcoo <2435932516@qq.com> Date: Sun, 25 Jul 2021 17:41:24 +0800 Subject: [PATCH] =?UTF-8?q?fix=20wordCloud=E5=8A=9F=E8=83=BD=20=E8=AF=8D?= =?UTF-8?q?=E4=BA=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BanWord/main.go | 9 + Config/config.go | 76 ++-- GroupManager/main.go | 934 ++++++++++++++++++++++--------------------- dictionary.txt | 2 + go.mod | 5 +- main.go | 13 +- wordCloud/main.go | 63 ++- 7 files changed, 582 insertions(+), 520 deletions(-) diff --git a/BanWord/main.go b/BanWord/main.go index fc2cc16..0053dc6 100644 --- a/BanWord/main.go +++ b/BanWord/main.go @@ -20,7 +20,16 @@ func Hook(b *Core.Bot) error { } else { c = Config.CoreConfig.DefaultGroupConfig } + banQQ := Config.CoreConfig.BanQQ Config.Lock.RUnlock() + + for _, v := range banQQ { + if packet.FromUserID == v { + packet.Ban = true + return + } + } + if !c.Enable { return } diff --git a/Config/config.go b/Config/config.go index 421bb3f..fbc98b4 100644 --- a/Config/config.go +++ b/Config/config.go @@ -7,7 +7,6 @@ import ( "os" "sync" - "github.com/fsnotify/fsnotify" "gopkg.in/yaml.v3" ) @@ -36,6 +35,7 @@ type CoreConfigStruct struct { PixivRefreshToken string PixivProxy string } + BanQQ []int64 Debug bool BiliLive bool YiQing bool @@ -113,10 +113,10 @@ func Save() error { } func init() { - watcher, err := fsnotify.NewWatcher() - if err != nil { - log.Fatal(err) - } + //watcher, err := fsnotify.NewWatcher() + //if err != nil { + // log.Fatal(err) + //} if len(os.Args) == 2 && os.Args[1] == "first" { b, err := yaml.Marshal(CoreConfig) @@ -140,38 +140,38 @@ func init() { panic(err) } Save() - go func() { - for { - select { - case event, ok := <-watcher.Events: - if !ok { - return - } - if event.Op&fsnotify.Write == fsnotify.Write { - b, err := ioutil.ReadFile("./config.yaml") - if err != nil { - log.Println("读取配置文件失败") - break - } - Lock.Lock() - err = yaml.Unmarshal(b, &CoreConfig) - Lock.Unlock() - if err != nil { - log.Println("读取配置文件失败") - break - } - } - case err, ok := <-watcher.Errors: - if !ok { - return - } - log.Println("error:", err) - } - } - }() - err = watcher.Add("./config.yaml") - if err != nil { - log.Fatal(err) - } + //go func() { + // for { + // select { + // case event, ok := <-watcher.Events: + // if !ok { + // return + // } + // if event.Op&fsnotify.Write == fsnotify.Write { + // b, err := ioutil.ReadFile("./config.yaml") + // if err != nil { + // log.Println("读取配置文件失败") + // break + // } + // Lock.Lock() + // err = yaml.Unmarshal(b, &CoreConfig) + // Lock.Unlock() + // if err != nil { + // log.Println("读取配置文件失败") + // break + // } + // } + // case err, ok := <-watcher.Errors: + // if !ok { + // return + // } + // log.Println("error:", err) + // } + // } + //}() + //err = watcher.Add("./config.yaml") + //if err != nil { + // log.Fatal(err) + //} dbInit() } diff --git a/GroupManager/main.go b/GroupManager/main.go index 9953d55..81857a3 100644 --- a/GroupManager/main.go +++ b/GroupManager/main.go @@ -323,332 +323,297 @@ func (m *Module) ModuleInit(b *Core.Bot, l *logrus.Entry) error { if Config.CoreConfig.OPQWebConfig.Enable { log.Println("启动Web 😊") - go func() { - Config.Lock.Lock() - sess = sessions.New(sessions.Config{Cookie: "OPQWebSession"}) - if Config.CoreConfig.OPQWebConfig.CSRF == "" { - Config.CoreConfig.OPQWebConfig.CSRF = utils.RandomString(32) - err := Config.Save() - if err != nil { - log.Println(err) - } + + Config.Lock.Lock() + sess = sessions.New(sessions.Config{Cookie: "OPQWebSession"}) + if Config.CoreConfig.OPQWebConfig.CSRF == "" { + Config.CoreConfig.OPQWebConfig.CSRF = utils.RandomString(32) + err := Config.Save() + if err != nil { + log.Println(err) } - fads, _ := fs.Sub(staticFs, "Web/dist/spa") - if Config.CoreConfig.ReverseProxy != "" { - // target, err := url.Parse(Config.CoreConfig.ReverseProxy) - if err != nil { - panic(err) - } - App.Get("{root:path}", func(ctx iris.Context) { - director := func(r *http.Request) { - r.Host = Config.CoreConfig.ReverseProxy - r.URL, _ = url.Parse(r.Host + "/" + ctx.Path()) - } - p := &httputil.ReverseProxy{Director: director} - p.ServeHTTP(ctx.ResponseWriter(), ctx.Request()) - }) - } else { - App.HandleDir("/", http.FS(fads)) + } + fads, _ := fs.Sub(staticFs, "Web/dist/spa") + if Config.CoreConfig.ReverseProxy != "" { + // target, err := url.Parse(Config.CoreConfig.ReverseProxy) + if err != nil { + panic(err) } + App.Get("{root:path}", func(ctx iris.Context) { + director := func(r *http.Request) { + r.Host = Config.CoreConfig.ReverseProxy + r.URL, _ = url.Parse(r.Host + "/" + ctx.Path()) + } + p := &httputil.ReverseProxy{Director: director} + p.ServeHTTP(ctx.ResponseWriter(), ctx.Request()) + }) + } else { + App.HandleDir("/", http.FS(fads)) + } - // app.HandleDir("/", iris.Dir("./Web/dist/spa")) - Config.Lock.Unlock() + // app.HandleDir("/", iris.Dir("./Web/dist/spa")) + Config.Lock.Unlock() - App.Use(beforeCsrf) - App.Use(sess.Handler()) - App.WrapRouter(func(w http.ResponseWriter, r *http.Request, router http.HandlerFunc) { - w.Header().Add("Access-Control-Allow-Origin", r.Header.Get("Origin")) - w.Header().Add("Access-Control-Allow-Credentials", "true") - path := r.URL.Path - if r.Method == "OPTIONS" { - w.Header().Add("Access-Control-Allow-Headers", "content-type") - w.WriteHeader(200) - return + App.Use(beforeCsrf) + App.Use(sess.Handler()) + App.WrapRouter(func(w http.ResponseWriter, r *http.Request, router http.HandlerFunc) { + w.Header().Add("Access-Control-Allow-Origin", r.Header.Get("Origin")) + w.Header().Add("Access-Control-Allow-Credentials", "true") + path := r.URL.Path + if r.Method == "OPTIONS" { + w.Header().Add("Access-Control-Allow-Headers", "content-type") + w.WriteHeader(200) + return + } + if len(path) < 4 { + if !pathIsFile(path) { + r.URL.Path = "/" } - if len(path) < 4 { + } else { + if r.URL.Path[0:4] != "/api" && r.URL.Path[0:4] != "/git" { if !pathIsFile(path) { r.URL.Path = "/" } - } else { - if r.URL.Path[0:4] != "/api" && r.URL.Path[0:4] != "/git" { - if !pathIsFile(path) { - r.URL.Path = "/" - } - } } - // log.Println(r.URL.Path) - router.ServeHTTP(w, r) - }) - App.Get("/api/csrf", func(ctx iris.Context) { - s := sess.Start(ctx) - salt := int(time.Now().Unix()) - keyTmp := utils.Md5V(strconv.Itoa(salt + rand.Intn(100))) - s.Set("OPQWebCSRF", keyTmp) - ctx.SetCookieKV("OPQWebCSRF", keyTmp, iris.CookieHTTPOnly(false)) - _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: s.Get("username")}) - }) - App.Get("/api/status", func(ctx iris.Context) { + } + // log.Println(r.URL.Path) + router.ServeHTTP(w, r) + }) + App.Get("/api/csrf", func(ctx iris.Context) { + s := sess.Start(ctx) + salt := int(time.Now().Unix()) + keyTmp := utils.Md5V(strconv.Itoa(salt + rand.Intn(100))) + s.Set("OPQWebCSRF", keyTmp) + ctx.SetCookieKV("OPQWebCSRF", keyTmp, iris.CookieHTTPOnly(false)) + _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: s.Get("username")}) + }) + App.Get("/api/status", func(ctx iris.Context) { + s := sess.Start(ctx) + salt := int(time.Now().Unix()) + keyTmp := utils.Md5V(strconv.Itoa(salt + rand.Intn(100))) + s.Set("OPQWebCSRF", keyTmp) + ctx.SetCookieKV("OPQWebCSRF", keyTmp, iris.CookieHTTPOnly(false)) + if s.GetBooleanDefault("auth", false) { + _, _ = ctx.JSON(WebResult{Code: 1, Info: "已登录!", Data: s.Get("username")}) + return + } else { + _, _ = ctx.JSON(WebResult{Code: 0, Info: "未登录!", Data: nil}) + return + } + }) + App.Post("/api/login", func(ctx iris.Context) { + username := ctx.FormValue("username") + password := ctx.FormValue("password") + Config.Lock.RLock() + defer Config.Lock.RUnlock() + if username == Config.CoreConfig.OPQWebConfig.Username && password == utils.Md5V(Config.CoreConfig.OPQWebConfig.Password) { s := sess.Start(ctx) - salt := int(time.Now().Unix()) - keyTmp := utils.Md5V(strconv.Itoa(salt + rand.Intn(100))) - s.Set("OPQWebCSRF", keyTmp) - ctx.SetCookieKV("OPQWebCSRF", keyTmp, iris.CookieHTTPOnly(false)) - if s.GetBooleanDefault("auth", false) { - _, _ = ctx.JSON(WebResult{Code: 1, Info: "已登录!", Data: s.Get("username")}) - return - } else { - _, _ = ctx.JSON(WebResult{Code: 0, Info: "未登录!", Data: nil}) - return - } - }) - App.Post("/api/login", func(ctx iris.Context) { - username := ctx.FormValue("username") - password := ctx.FormValue("password") - Config.Lock.RLock() - defer Config.Lock.RUnlock() - if username == Config.CoreConfig.OPQWebConfig.Username && password == utils.Md5V(Config.CoreConfig.OPQWebConfig.Password) { - s := sess.Start(ctx) - s.Set("auth", true) - _, _ = ctx.JSON(WebResult{Code: 1, Info: "登录成功", Data: nil}) - return - } else { - _, _ = ctx.JSON(WebResult{Code: 0, Info: "用户名密码错误!", Data: nil}) - return - } + s.Set("auth", true) + _, _ = ctx.JSON(WebResult{Code: 1, Info: "登录成功", Data: nil}) + return + } else { + _, _ = ctx.JSON(WebResult{Code: 0, Info: "用户名密码错误!", Data: nil}) + return + } - }) - // job周期任务读取 - Config.Lock.RLock() - for k, v := range Config.CoreConfig.GroupConfig { - for k1, v2 := range v.Job { - switch v2.Type { + }) + // job周期任务读取 + Config.Lock.RLock() + for k, v := range Config.CoreConfig.GroupConfig { + for k1, v2 := range v.Job { + switch v2.Type { + case 1: + err = b.BotCronManager.AddJob(k, k1, v2.Cron, func() { + log.Print("执行任务" + k1) + if b.Announce(v2.Title, v2.Content, 0, 10, k) != nil { + log.Print(err) + } + }) + if err != nil { + log.Print("添加任务" + k1 + "出现错误" + err.Error()) + } + case 2: + err = b.BotCronManager.AddJob(k, k1, v2.Cron, func() { + log.Print("执行任务" + k1) + if b.SetForbidden(0, 1, k, 0) != nil { + log.Print(err) + } + }) + if err != nil { + log.Print("添加任务" + k1 + "出现错误" + err.Error()) + } + case 3: + err = b.BotCronManager.AddJob(k, k1, v2.Cron, func() { + log.Print("执行任务" + k1) + if b.SetForbidden(0, 0, k, 0) != nil { + log.Print(err) + } + }) + if err != nil { + log.Print("添加任务" + k1 + "出现错误" + err.Error()) + } + case 4: + err = b.BotCronManager.AddJob(k, k1, v2.Cron, func() { + log.Print("执行任务" + k1) + b.Send(OPQBot.SendMsgPack{ + SendToType: OPQBot.SendToTypeGroup, + ToUserUid: k, + Content: OPQBot.SendTypeTextMsgContent{ + Content: v2.Content, + }, + }) + }) + if err != nil { + log.Print("添加任务" + k1 + "出现错误" + err.Error()) + } + } + } + } + Config.Lock.RUnlock() + needAuth := App.Party("/api/admin", requireAuth) + { + rJob := needAuth.Party("/job") + { + rJob.Post("/add", func(ctx iris.Context) { + ids := ctx.FormValue("id") + id, err := strconv.ParseInt(ids, 10, 64) + if err != nil { + _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) + return + } + if id == -1 { + _, _ = ctx.JSON(WebResult{Code: 0, Info: "默认群禁止添加周期任务", Data: nil}) + return + } + span := ctx.FormValue("span") + jobName := ctx.FormValue("jobName") + if jobName == "" { + _, _ = ctx.JSON(WebResult{Code: 0, Info: "jobName为空", Data: nil}) + return + } + cronType, _ := strconv.Atoi(ctx.FormValue("type")) + switch cronType { + // 公告 case 1: - err = b.BotCronManager.AddJob(k, k1, v2.Cron, func() { - log.Print("执行任务" + k1) - if b.Announce(v2.Title, v2.Content, 0, 10, k) != nil { + title := ctx.FormValue("title") + content := ctx.FormValue("content") + err = b.BotCronManager.AddJob(id, jobName, span, func() { + log.Print("执行任务" + jobName) + if b.Announce(title, content, 0, 10, id) != nil { log.Print(err) } }) if err != nil { - log.Print("添加任务" + k1 + "出现错误" + err.Error()) + _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) + return + } else { + job := Config.Job{Type: cronType, Cron: span, Title: title, Content: content} + Config.Lock.Lock() + defer Config.Lock.Unlock() + if v, ok := Config.CoreConfig.GroupConfig[id]; ok { + v.Job[jobName] = job + Config.CoreConfig.GroupConfig[id] = v + } else { + v = Config.CoreConfig.DefaultGroupConfig + v.Job[jobName] = job + Config.CoreConfig.GroupConfig[id] = v + } + Config.Save() + _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: nil}) + return } + // 全局禁言 case 2: - err = b.BotCronManager.AddJob(k, k1, v2.Cron, func() { - log.Print("执行任务" + k1) - if b.SetForbidden(0, 1, k, 0) != nil { + err = b.BotCronManager.AddJob(id, jobName, span, func() { + log.Print("执行任务" + jobName) + if b.SetForbidden(0, 1, id, 0) != nil { log.Print(err) } }) if err != nil { - log.Print("添加任务" + k1 + "出现错误" + err.Error()) + _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) + return + } else { + job := Config.Job{Type: cronType, Cron: span} + Config.Lock.Lock() + defer Config.Lock.Unlock() + if v, ok := Config.CoreConfig.GroupConfig[id]; ok { + v.Job[jobName] = job + Config.CoreConfig.GroupConfig[id] = v + } else { + v = Config.CoreConfig.DefaultGroupConfig + v.Job[jobName] = job + Config.CoreConfig.GroupConfig[id] = v + } + Config.Save() + _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: nil}) + return } + // 解除全局禁言 case 3: - err = b.BotCronManager.AddJob(k, k1, v2.Cron, func() { - log.Print("执行任务" + k1) - if b.SetForbidden(0, 0, k, 0) != nil { + err = b.BotCronManager.AddJob(id, jobName, span, func() { + log.Print("执行任务" + jobName) + if b.SetForbidden(0, 0, id, 0) != nil { log.Print(err) } }) if err != nil { - log.Print("添加任务" + k1 + "出现错误" + err.Error()) + _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) + return + } else { + job := Config.Job{Type: cronType, Cron: span} + Config.Lock.Lock() + defer Config.Lock.Unlock() + if v, ok := Config.CoreConfig.GroupConfig[id]; ok { + v.Job[jobName] = job + Config.CoreConfig.GroupConfig[id] = v + } else { + v = Config.CoreConfig.DefaultGroupConfig + v.Job[jobName] = job + Config.CoreConfig.GroupConfig[id] = v + } + Config.Save() + _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: nil}) + return } case 4: - err = b.BotCronManager.AddJob(k, k1, v2.Cron, func() { - log.Print("执行任务" + k1) + // title := ctx.FormValue("title") + content := ctx.FormValue("content") + err = b.BotCronManager.AddJob(id, jobName, span, func() { + log.Print("执行任务" + jobName) b.Send(OPQBot.SendMsgPack{ SendToType: OPQBot.SendToTypeGroup, - ToUserUid: k, + ToUserUid: id, Content: OPQBot.SendTypeTextMsgContent{ - Content: v2.Content, + Content: content, }, }) }) - if err != nil { - log.Print("添加任务" + k1 + "出现错误" + err.Error()) - } - } - } - } - Config.Lock.RUnlock() - needAuth := App.Party("/api/admin", requireAuth) - { - rJob := needAuth.Party("/job") - { - rJob.Post("/add", func(ctx iris.Context) { - ids := ctx.FormValue("id") - id, err := strconv.ParseInt(ids, 10, 64) if err != nil { _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) return - } - if id == -1 { - _, _ = ctx.JSON(WebResult{Code: 0, Info: "默认群禁止添加周期任务", Data: nil}) - return - } - span := ctx.FormValue("span") - jobName := ctx.FormValue("jobName") - if jobName == "" { - _, _ = ctx.JSON(WebResult{Code: 0, Info: "jobName为空", Data: nil}) - return - } - cronType, _ := strconv.Atoi(ctx.FormValue("type")) - switch cronType { - // 公告 - case 1: - title := ctx.FormValue("title") - content := ctx.FormValue("content") - err = b.BotCronManager.AddJob(id, jobName, span, func() { - log.Print("执行任务" + jobName) - if b.Announce(title, content, 0, 10, id) != nil { - log.Print(err) - } - }) - if err != nil { - _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) - return - } else { - job := Config.Job{Type: cronType, Cron: span, Title: title, Content: content} - Config.Lock.Lock() - defer Config.Lock.Unlock() - if v, ok := Config.CoreConfig.GroupConfig[id]; ok { - v.Job[jobName] = job - Config.CoreConfig.GroupConfig[id] = v - } else { - v = Config.CoreConfig.DefaultGroupConfig - v.Job[jobName] = job - Config.CoreConfig.GroupConfig[id] = v - } - Config.Save() - _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: nil}) - return - } - // 全局禁言 - case 2: - err = b.BotCronManager.AddJob(id, jobName, span, func() { - log.Print("执行任务" + jobName) - if b.SetForbidden(0, 1, id, 0) != nil { - log.Print(err) - } - }) - if err != nil { - _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) - return - } else { - job := Config.Job{Type: cronType, Cron: span} - Config.Lock.Lock() - defer Config.Lock.Unlock() - if v, ok := Config.CoreConfig.GroupConfig[id]; ok { - v.Job[jobName] = job - Config.CoreConfig.GroupConfig[id] = v - } else { - v = Config.CoreConfig.DefaultGroupConfig - v.Job[jobName] = job - Config.CoreConfig.GroupConfig[id] = v - } - Config.Save() - _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: nil}) - return - } - // 解除全局禁言 - case 3: - err = b.BotCronManager.AddJob(id, jobName, span, func() { - log.Print("执行任务" + jobName) - if b.SetForbidden(0, 0, id, 0) != nil { - log.Print(err) - } - }) - if err != nil { - _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) - return - } else { - job := Config.Job{Type: cronType, Cron: span} - Config.Lock.Lock() - defer Config.Lock.Unlock() - if v, ok := Config.CoreConfig.GroupConfig[id]; ok { - v.Job[jobName] = job - Config.CoreConfig.GroupConfig[id] = v - } else { - v = Config.CoreConfig.DefaultGroupConfig - v.Job[jobName] = job - Config.CoreConfig.GroupConfig[id] = v - } - Config.Save() - _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: nil}) - return - } - case 4: - // title := ctx.FormValue("title") - content := ctx.FormValue("content") - err = b.BotCronManager.AddJob(id, jobName, span, func() { - log.Print("执行任务" + jobName) - b.Send(OPQBot.SendMsgPack{ - SendToType: OPQBot.SendToTypeGroup, - ToUserUid: id, - Content: OPQBot.SendTypeTextMsgContent{ - Content: content, - }, - }) - }) - if err != nil { - _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) - return + } else { + job := Config.Job{Type: cronType, Cron: span, Content: content} + Config.Lock.Lock() + defer Config.Lock.Unlock() + if v, ok := Config.CoreConfig.GroupConfig[id]; ok { + v.Job[jobName] = job + Config.CoreConfig.GroupConfig[id] = v } else { - job := Config.Job{Type: cronType, Cron: span, Content: content} - Config.Lock.Lock() - defer Config.Lock.Unlock() - if v, ok := Config.CoreConfig.GroupConfig[id]; ok { - v.Job[jobName] = job - Config.CoreConfig.GroupConfig[id] = v - } else { - v = Config.CoreConfig.DefaultGroupConfig - v.Job[jobName] = job - Config.CoreConfig.GroupConfig[id] = v - } - Config.Save() - _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: nil}) - return + v = Config.CoreConfig.DefaultGroupConfig + v.Job[jobName] = job + Config.CoreConfig.GroupConfig[id] = v } - default: - _, _ = ctx.JSON(WebResult{Code: 0, Info: "类型不存在", Data: nil}) + Config.Save() + _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: nil}) return } - }) - rJob.Post("/del", func(ctx iris.Context) { - ids := ctx.FormValue("id") - id, err := strconv.ParseInt(ids, 10, 64) - if err != nil { - _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) - return - } - if id == -1 { - _, _ = ctx.JSON(WebResult{Code: 0, Info: "默认群禁止删除周期任务", Data: nil}) - return - } - jobName := ctx.FormValue("jobName") - if jobName == "" { - _, _ = ctx.JSON(WebResult{Code: 0, Info: "jobName为空", Data: nil}) - return - } - err = b.BotCronManager.Remove(id, jobName) - - if err != nil { - _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) - return - } - Config.Lock.Lock() - defer Config.Lock.Unlock() - if v, ok := Config.CoreConfig.GroupConfig[id]; ok { - delete(v.Job, jobName) - Config.CoreConfig.GroupConfig[id] = v - } else { - _, _ = ctx.JSON(WebResult{Code: 0, Info: "Group在配置文件中不存在!", Data: nil}) - return - } - Config.Save() - _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: nil}) - }) - } - needAuth.Post("/getGroupMember", func(ctx iris.Context) { + default: + _, _ = ctx.JSON(WebResult{Code: 0, Info: "类型不存在", Data: nil}) + return + } + }) + rJob.Post("/del", func(ctx iris.Context) { ids := ctx.FormValue("id") id, err := strconv.ParseInt(ids, 10, 64) if err != nil { @@ -656,68 +621,84 @@ func (m *Module) ModuleInit(b *Core.Bot, l *logrus.Entry) error { return } if id == -1 { - _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: []int{}}) + _, _ = ctx.JSON(WebResult{Code: 0, Info: "默认群禁止删除周期任务", Data: nil}) + return + } + jobName := ctx.FormValue("jobName") + if jobName == "" { + _, _ = ctx.JSON(WebResult{Code: 0, Info: "jobName为空", Data: nil}) return } + err = b.BotCronManager.Remove(id, jobName) - glist, err := b.GetGroupMemberList(id, 0) if err != nil { _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) return } - result := glist - for { - if glist.LastUin == 0 { - break - } - glist, err = b.GetGroupMemberList(id, glist.LastUin) - if err != nil { - _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) - return - } - result.MemberList = append(result.MemberList, glist.MemberList...) - result.Count += glist.Count - result.LastUin = glist.LastUin + Config.Lock.Lock() + defer Config.Lock.Unlock() + if v, ok := Config.CoreConfig.GroupConfig[id]; ok { + delete(v.Job, jobName) + Config.CoreConfig.GroupConfig[id] = v + } else { + _, _ = ctx.JSON(WebResult{Code: 0, Info: "Group在配置文件中不存在!", Data: nil}) + return } - _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: result}) - return + Config.Save() + _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: nil}) }) - needAuth.Post("/setGroupConfig", func(ctx iris.Context) { - ids := ctx.FormValue("id") - enable := ctx.FormValue("enable") - id, err := strconv.ParseInt(ids, 10, 64) + } + needAuth.Post("/getGroupMember", func(ctx iris.Context) { + ids := ctx.FormValue("id") + id, err := strconv.ParseInt(ids, 10, 64) + if err != nil { + _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) + return + } + if id == -1 { + _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: []int{}}) + return + } + + glist, err := b.GetGroupMemberList(id, 0) + if err != nil { + _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) + return + } + result := glist + for { + if glist.LastUin == 0 { + break + } + glist, err = b.GetGroupMemberList(id, glist.LastUin) if err != nil { _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) return } - if enable != "" { - Config.Lock.Lock() - defer Config.Lock.Unlock() - Enable := ctx.FormValue("enable") == "true" - if id == -1 { - Config.CoreConfig.DefaultGroupConfig.Enable = Enable - _, _ = ctx.JSON(WebResult{ - Code: 1, - Info: "默认配置保存成功!", - Data: Config.CoreConfig.GroupConfig[id].Enable, - }) - err := Config.Save() - if err != nil { - log.Println(err) - } - return - } - if v, ok := Config.CoreConfig.GroupConfig[id]; ok { - v.Enable = Enable - Config.CoreConfig.GroupConfig[id] = v - } else { - v = Config.CoreConfig.DefaultGroupConfig - v.Enable = Enable - Config.CoreConfig.GroupConfig[id] = v - } + result.MemberList = append(result.MemberList, glist.MemberList...) + result.Count += glist.Count + result.LastUin = glist.LastUin + } + _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: result}) + return + }) + needAuth.Post("/setGroupConfig", func(ctx iris.Context) { + ids := ctx.FormValue("id") + enable := ctx.FormValue("enable") + id, err := strconv.ParseInt(ids, 10, 64) + if err != nil { + _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) + return + } + if enable != "" { + Config.Lock.Lock() + defer Config.Lock.Unlock() + Enable := ctx.FormValue("enable") == "true" + if id == -1 { + Config.CoreConfig.DefaultGroupConfig.Enable = Enable _, _ = ctx.JSON(WebResult{ Code: 1, - Info: "保存成功!", + Info: "默认配置保存成功!", Data: Config.CoreConfig.GroupConfig[id].Enable, }) err := Config.Save() @@ -726,160 +707,180 @@ func (m *Module) ModuleInit(b *Core.Bot, l *logrus.Entry) error { } return } - menuData := ctx.FormValue("data[Menu]") - menuKeyWordData := ctx.FormValue("data[MenuKeyWord]") - Enable := ctx.FormValue("data[Enable]") == "true" - ShutUpWord := ctx.FormValue("data[ShutUpWord]") - Welcome := ctx.FormValue("data[Welcome]") - AdminUin, _ := strconv.ParseInt(ctx.FormValue("data[AdminUin]"), 10, 64) - JoinVerifyTime, _ := strconv.Atoi(ctx.FormValue("data[JoinVerifyTime]")) - JoinAutoShutUpTime, _ := strconv.Atoi(ctx.FormValue("data[JoinAutoShutUpTime]")) - ShutUpTime, _ := strconv.Atoi(ctx.FormValue("data[ShutUpTime]")) - JoinVerifyType, _ := strconv.Atoi(ctx.FormValue("data[JoinVerifyType]")) - Zan := ctx.FormValue("data[Zan]") == "true" - Bili := ctx.FormValue("data[Bili]") == "true" - SignIn := ctx.FormValue("data[SignIn]") == "true" - Job := map[string]Config.Job{} - for k, v := range ctx.FormValues() { - //log.Println(k,strings.HasPrefix(k,"data[Job]["),strings.Split(strings.TrimPrefix(k,"data[Job]["),"]")) - if strings.HasPrefix(k, "data[Job][") { - if v1 := strings.Split(strings.TrimPrefix(k, "data[Job]["), "]"); len(v1) >= 2 && len(v) >= 1 { - switch v1[1] { - case "[Cron": - v2, _ := Job[v1[0]] - v2.Cron = v[0] - Job[v1[0]] = v2 - case "[JobType": - v2, _ := Job[v1[0]] - v2.Type, _ = strconv.Atoi(v[0]) - Job[v1[0]] = v2 - case "[Content": - v2, _ := Job[v1[0]] - v2.Content = v[0] - Job[v1[0]] = v2 - } - - } - } - } - Config.Lock.Lock() - defer Config.Lock.Unlock() - - if id == -1 { - Config.CoreConfig.DefaultGroupConfig = Config.GroupConfig{BiliUps: map[int64]Config.Up{}, Bili: Bili, Job: Job, JoinVerifyType: JoinVerifyType, Welcome: Welcome, SignIn: SignIn, Zan: Zan, JoinVerifyTime: JoinVerifyTime, JoinAutoShutUpTime: JoinAutoShutUpTime, AdminUin: AdminUin, Menu: menuData, MenuKeyWord: menuKeyWordData, Enable: Enable, ShutUpWord: ShutUpWord, ShutUpTime: ShutUpTime} - Config.Save() - _, _ = ctx.JSON(WebResult{ - Code: 1, - Info: "默认配置,保存成功!", - Data: nil, - }) - return + if v, ok := Config.CoreConfig.GroupConfig[id]; ok { + v.Enable = Enable + Config.CoreConfig.GroupConfig[id] = v + } else { + v = Config.CoreConfig.DefaultGroupConfig + v.Enable = Enable + Config.CoreConfig.GroupConfig[id] = v } - Config.CoreConfig.GroupConfig[id] = Config.GroupConfig{BiliUps: Config.CoreConfig.GroupConfig[id].BiliUps, Bili: Bili, Job: Job, JoinVerifyType: JoinVerifyType, Welcome: Welcome, SignIn: SignIn, Zan: Zan, JoinVerifyTime: JoinVerifyTime, JoinAutoShutUpTime: JoinAutoShutUpTime, AdminUin: AdminUin, Menu: menuData, MenuKeyWord: menuKeyWordData, Enable: Enable, ShutUpWord: ShutUpWord, ShutUpTime: ShutUpTime} - Config.Save() _, _ = ctx.JSON(WebResult{ Code: 1, Info: "保存成功!", - Data: nil, + Data: Config.CoreConfig.GroupConfig[id].Enable, }) - return - }) - needAuth.Post("/groupStatus", func(ctx iris.Context) { - ids := ctx.FormValue("id") - id, err := strconv.ParseInt(ids, 10, 64) + err := Config.Save() if err != nil { - _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) - return - } - Config.Lock.RLock() - defer Config.Lock.RUnlock() - if id == -1 { - _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: Config.CoreConfig.DefaultGroupConfig}) - return - } - if v, ok := Config.CoreConfig.GroupConfig[id]; ok { - _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: v}) - return - } else { - _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: Config.CoreConfig.DefaultGroupConfig}) - return + log.Println(err) } - }) - needAuth.Get("/groups", func(ctx iris.Context) { - g, err := b.GetGroupList("") - if err != nil { - _, _ = ctx.JSON(WebResult{ - Code: 0, - Info: err.Error(), - Data: nil, - }) - return + return + } + menuData := ctx.FormValue("data[Menu]") + menuKeyWordData := ctx.FormValue("data[MenuKeyWord]") + Enable := ctx.FormValue("data[Enable]") == "true" + ShutUpWord := ctx.FormValue("data[ShutUpWord]") + Welcome := ctx.FormValue("data[Welcome]") + AdminUin, _ := strconv.ParseInt(ctx.FormValue("data[AdminUin]"), 10, 64) + JoinVerifyTime, _ := strconv.Atoi(ctx.FormValue("data[JoinVerifyTime]")) + JoinAutoShutUpTime, _ := strconv.Atoi(ctx.FormValue("data[JoinAutoShutUpTime]")) + ShutUpTime, _ := strconv.Atoi(ctx.FormValue("data[ShutUpTime]")) + JoinVerifyType, _ := strconv.Atoi(ctx.FormValue("data[JoinVerifyType]")) + Zan := ctx.FormValue("data[Zan]") == "true" + Bili := ctx.FormValue("data[Bili]") == "true" + SignIn := ctx.FormValue("data[SignIn]") == "true" + Job := map[string]Config.Job{} + for k, v := range ctx.FormValues() { + //log.Println(k,strings.HasPrefix(k,"data[Job]["),strings.Split(strings.TrimPrefix(k,"data[Job]["),"]")) + if strings.HasPrefix(k, "data[Job][") { + if v1 := strings.Split(strings.TrimPrefix(k, "data[Job]["), "]"); len(v1) >= 2 && len(v) >= 1 { + switch v1[1] { + case "[Cron": + v2, _ := Job[v1[0]] + v2.Cron = v[0] + Job[v1[0]] = v2 + case "[JobType": + v2, _ := Job[v1[0]] + v2.Type, _ = strconv.Atoi(v[0]) + Job[v1[0]] = v2 + case "[Content": + v2, _ := Job[v1[0]] + v2.Content = v[0] + Job[v1[0]] = v2 + } + + } } + } + Config.Lock.Lock() + defer Config.Lock.Unlock() + + if id == -1 { + Config.CoreConfig.DefaultGroupConfig = Config.GroupConfig{BiliUps: map[int64]Config.Up{}, Bili: Bili, Job: Job, JoinVerifyType: JoinVerifyType, Welcome: Welcome, SignIn: SignIn, Zan: Zan, JoinVerifyTime: JoinVerifyTime, JoinAutoShutUpTime: JoinAutoShutUpTime, AdminUin: AdminUin, Menu: menuData, MenuKeyWord: menuKeyWordData, Enable: Enable, ShutUpWord: ShutUpWord, ShutUpTime: ShutUpTime} + Config.Save() _, _ = ctx.JSON(WebResult{ Code: 1, - Info: "success", - Data: g, + Info: "默认配置,保存成功!", + Data: nil, }) - }) - needAuth.Post("/shutUp", func(ctx iris.Context) { - ids := ctx.FormValue("id") - uins := ctx.FormValue("uin") - times := ctx.FormValue("time") - id, err := strconv.ParseInt(ids, 10, 64) - if err != nil { - _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) - return - } - uin, err := strconv.ParseInt(uins, 10, 64) - if err != nil { - _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) - return - } - time1, err := strconv.Atoi(times) - if err != nil { - _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) - return - } - err = b.SetForbidden(1, time1, id, uin) - if err != nil { - _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) - return - } - _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: nil}) return + } + Config.CoreConfig.GroupConfig[id] = Config.GroupConfig{BiliUps: Config.CoreConfig.GroupConfig[id].BiliUps, Bili: Bili, Job: Job, JoinVerifyType: JoinVerifyType, Welcome: Welcome, SignIn: SignIn, Zan: Zan, JoinVerifyTime: JoinVerifyTime, JoinAutoShutUpTime: JoinAutoShutUpTime, AdminUin: AdminUin, Menu: menuData, MenuKeyWord: menuKeyWordData, Enable: Enable, ShutUpWord: ShutUpWord, ShutUpTime: ShutUpTime} + Config.Save() + _, _ = ctx.JSON(WebResult{ + Code: 1, + Info: "保存成功!", + Data: nil, }) - needAuth.Post("/kick", func(ctx iris.Context) { - ids := ctx.FormValue("id") - uins := ctx.FormValue("uin") - id, err := strconv.ParseInt(ids, 10, 64) - if err != nil { - _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) - return - } - uin, err := strconv.ParseInt(uins, 10, 64) - if err != nil { - _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) - return - } - err = b.KickGroupMember(id, uin) - if err != nil { - _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) - return - } - _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: nil}) + return + }) + needAuth.Post("/groupStatus", func(ctx iris.Context) { + ids := ctx.FormValue("id") + id, err := strconv.ParseInt(ids, 10, 64) + if err != nil { + _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) return - }) - needAuth.Get("/logout", func(ctx iris.Context) { - s := sess.Start(ctx) - s.Set("auth", false) - s.Clear() + } + Config.Lock.RLock() + defer Config.Lock.RUnlock() + if id == -1 { + _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: Config.CoreConfig.DefaultGroupConfig}) + return + } + if v, ok := Config.CoreConfig.GroupConfig[id]; ok { + _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: v}) + return + } else { + _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: Config.CoreConfig.DefaultGroupConfig}) + return + } + }) + needAuth.Get("/groups", func(ctx iris.Context) { + g, err := b.GetGroupList("") + if err != nil { _, _ = ctx.JSON(WebResult{ - Code: 1, - Info: "Success", + Code: 0, + Info: err.Error(), Data: nil, }) + return + } + _, _ = ctx.JSON(WebResult{ + Code: 1, + Info: "success", + Data: g, }) - } + }) + needAuth.Post("/shutUp", func(ctx iris.Context) { + ids := ctx.FormValue("id") + uins := ctx.FormValue("uin") + times := ctx.FormValue("time") + id, err := strconv.ParseInt(ids, 10, 64) + if err != nil { + _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) + return + } + uin, err := strconv.ParseInt(uins, 10, 64) + if err != nil { + _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) + return + } + time1, err := strconv.Atoi(times) + if err != nil { + _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) + return + } + err = b.SetForbidden(1, time1, id, uin) + if err != nil { + _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) + return + } + _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: nil}) + return + }) + needAuth.Post("/kick", func(ctx iris.Context) { + ids := ctx.FormValue("id") + uins := ctx.FormValue("uin") + id, err := strconv.ParseInt(ids, 10, 64) + if err != nil { + _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) + return + } + uin, err := strconv.ParseInt(uins, 10, 64) + if err != nil { + _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) + return + } + err = b.KickGroupMember(id, uin) + if err != nil { + _, _ = ctx.JSON(WebResult{Code: 0, Info: err.Error(), Data: nil}) + return + } + _, _ = ctx.JSON(WebResult{Code: 1, Info: "success", Data: nil}) + return + }) + needAuth.Get("/logout", func(ctx iris.Context) { + s := sess.Start(ctx) + s.Set("auth", false) + s.Clear() + _, _ = ctx.JSON(WebResult{ + Code: 1, + Info: "Success", + Data: nil, + }) + }) + } + go func() { App.Logger().Prefix = "[Web]" err := App.Run(iris.Addr(Config.CoreConfig.OPQWebConfig.Host+":"+strconv.Itoa(Config.CoreConfig.OPQWebConfig.Port)), iris.WithoutStartupLog) if err != nil { @@ -887,6 +888,7 @@ func (m *Module) ModuleInit(b *Core.Bot, l *logrus.Entry) error { return } }() + } return nil } diff --git a/dictionary.txt b/dictionary.txt index fb72b02..d4b6d43 100644 --- a/dictionary.txt +++ b/dictionary.txt @@ -438794,6 +438794,7 @@ c++ 3 nz 依那普利 2 ns 保利大厦 2 ns 保利大廈 2 ns +地王大厦 2 ns 克尔曼省 2 ns 克爾曼省 2 ns 六枝特区 2 ns @@ -567854,6 +567855,7 @@ c++ 3 nz 天津大学 56 nt 天津大學 56 nt 北斗七星 56 nz +山达尔星 56 nz 山梨酸鉀 56 nz 山梨酸钾 56 nz 湘鄂川黔 56 nz diff --git a/go.mod b/go.mod index d74ad88..85a66cd 100644 --- a/go.mod +++ b/go.mod @@ -4,14 +4,11 @@ go 1.16 require ( github.com/Joker/hpp v1.0.0 // indirect - github.com/adamzy/cedar-go v0.0.0-20170805034717-80a9c64b256d // indirect github.com/antonfisher/nested-logrus-formatter v1.3.1 github.com/fogleman/gg v1.3.0 - github.com/fsnotify/fsnotify v1.4.9 github.com/gaoyanpao/biliLiveHelper v0.0.0-20210205164551-dca3842ab39b + github.com/go-ego/gse v0.67.0 github.com/go-playground/webhooks/v6 v6.0.0-beta.3 - github.com/huichen/sego v0.0.0-20180617034105-3f3c8a8cfacc - github.com/issue9/assert v1.4.1 // indirect github.com/kataras/iris/v12 v12.2.0-alpha2.0.20210427211137-fa175eb84754 github.com/mcoo/OPQBot v0.1.3-0.20210721042534-b725701428cd github.com/mcoo/requests v0.0.2 diff --git a/main.go b/main.go index 641c71c..fa05e0c 100644 --- a/main.go +++ b/main.go @@ -11,8 +11,10 @@ import ( _ "OPQBot-QQGroupManager/githubManager" _ "OPQBot-QQGroupManager/setu" _ "OPQBot-QQGroupManager/wordCloud" + "fmt" "github.com/sirupsen/logrus" - + "net/http" + _ "net/http/pprof" //_ "OPQBot-QQGroupManager/steam" "OPQBot-QQGroupManager/utils" @@ -40,6 +42,15 @@ func main() { log.SetLevel(logrus.ErrorLevel) } + } + if Config.CoreConfig.Debug { + go func() { + ip := ":25569" + if err := http.ListenAndServe(ip, nil); err != nil { + fmt.Printf("start pprof failed on %s\n", ip) + } + }() + } log.Println("QQ Group Manager -️" + version + " 编译时间 " + date) androidDns.SetDns() diff --git a/wordCloud/main.go b/wordCloud/main.go index 6d2f64d..7a897c9 100644 --- a/wordCloud/main.go +++ b/wordCloud/main.go @@ -3,14 +3,13 @@ package wordCloud import ( "OPQBot-QQGroupManager/Core" "bytes" - "github.com/huichen/sego" + "github.com/go-ego/gse" "github.com/mcoo/OPQBot" "github.com/psykhi/wordclouds" "github.com/sirupsen/logrus" "gorm.io/gorm" "image/color" "image/png" - "strings" "time" ) @@ -20,6 +19,10 @@ var DefaultColors = []color.RGBA{ {0x59, 0x3a, 0xee, 0xff}, {0x65, 0xCD, 0xFA, 0xff}, {0x70, 0xD6, 0xBF, 0xff}, + {153, 50, 204, 255}, + {100, 149, 237, 255}, + {0, 255, 127, 255}, + {255, 0, 0, 255}, } type Module struct { @@ -49,20 +52,24 @@ type HotWord struct { } func (m *Module) DoHotWord() { - var segmented sego.Segmenter - segmented.LoadDictionary("./dictionary.txt") + var segmenter gse.Segmenter + err := segmenter.LoadDict("./dictionary.txt") + if err != nil { + log.Error(err) + } + //var segmented sego.Segmenter + //segmented.LoadDictionary("./dictionary.txt") for { msg := <-m.MsgChannel - split := strings.Split(sego.SegmentsToString(segmented.Segment([]byte(msg.Content)), false), " ") - for _, v := range split { - if s := strings.Split(v, "/"); len(s) == 2 && (len(s[0]) > 1) { - err := m.AddHotWord(s[0], msg.FromGroupID) + tmp := segmenter.Segment([]byte(msg.Content)) + for _, v := range gse.ToSlice(tmp, false) { + if len([]rune(v)) > 1 { + err := m.AddHotWord(v, msg.FromGroupID) if err != nil { log.Error(err) } } } - } } @@ -86,9 +93,12 @@ func (m *Module) ModuleInit(b *Core.Bot, l *logrus.Entry) error { log.Error(err) return } - sendMsg := "今日本群词云\n" + sendMsg := "今日本群词云" hotMap := map[string]int{} for i := 0; i < len(hotWords); i++ { + if len([]rune(hotWords[i].Word)) <= 1 { + continue + } hotMap[hotWords[i].Word] = hotWords[i].Count } log.Info(hotMap) @@ -97,7 +107,7 @@ func (m *Module) ModuleInit(b *Core.Bot, l *logrus.Entry) error { colors = append(colors, c) } - img := wordclouds.NewWordcloud(hotMap, wordclouds.FontMaxSize(128), wordclouds.FontMinSize(60), wordclouds.FontFile("./font.ttf"), + img := wordclouds.NewWordcloud(hotMap, wordclouds.FontMaxSize(150), wordclouds.FontMinSize(20), wordclouds.FontFile("./font.ttf"), wordclouds.Height(1024), wordclouds.Width(2048), wordclouds.Colors(colors)).Draw() @@ -109,6 +119,37 @@ func (m *Module) ModuleInit(b *Core.Bot, l *logrus.Entry) error { } b.SendGroupPicMsg(packet.FromGroupID, sendMsg, buf.Bytes()) } + if packet.Content == "本周词云" { + hotWords, err := m.GetWeeklyWord(packet.FromGroupID) + if err != nil { + log.Error(err) + return + } + sendMsg := "本周词云" + hotMap := map[string]int{} + for i := 0; i < len(hotWords); i++ { + if len([]rune(hotWords[i].Word)) <= 1 { + continue + } + hotMap[hotWords[i].Word] = hotWords[i].Count + } + log.Info(hotMap) + colors := make([]color.Color, 0) + for _, c := range DefaultColors { + colors = append(colors, c) + } + + img := wordclouds.NewWordcloud(hotMap, wordclouds.FontMaxSize(150), wordclouds.FontMinSize(20), wordclouds.FontFile("./font.ttf"), + wordclouds.Height(1024), + wordclouds.Width(2048), wordclouds.Colors(colors)).Draw() + buf := new(bytes.Buffer) + err = png.Encode(buf, img) + if err != nil { + log.Error(err) + return + } + b.SendGroupPicMsg(packet.FromGroupID, sendMsg, buf.Bytes()) + } } }) if err != nil {