diff --git a/README.md b/README.md index 1261407e..1c275756 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ - [ ] 同意好友请求 - [ ] 撤回[@xxx] [xxx] - [ ] 警告[@xxx] + - [x] run[xxx] - 涩图 - [x] 来份[涩图/二次元/风景] - [x] 添加[涩图/二次元/风景][P站图片ID] @@ -38,8 +39,6 @@ - [x] setu -p - 点歌 - [x] 点歌[xxx] -- run - - [x] run[xxx] - TODO... ### 使用方法 diff --git a/go.mod b/go.mod index f201d407..1d9dde97 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module bot +module github.com/Yiwen-Chan/ZeroBot-Plugin go 1.15 diff --git a/main.go b/main.go index 8b495090..30da56f0 100644 --- a/main.go +++ b/main.go @@ -8,11 +8,10 @@ import ( zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/driver" - _ "bot/github" - _ "bot/manager" - _ "bot/music" - _ "bot/run" - setutime "bot/setutime" + _ "github.com/Yiwen-Chan/ZeroBot-Plugin/github" + _ "github.com/Yiwen-Chan/ZeroBot-Plugin/manager" + _ "github.com/Yiwen-Chan/ZeroBot-Plugin/music" + setutime "github.com/Yiwen-Chan/ZeroBot-Plugin/setutime" ) func init() { diff --git a/manager/manager.go b/manager/manager.go index 6e92f8b5..43a9c642 100644 --- a/manager/manager.go +++ b/manager/manager.go @@ -1,10 +1,10 @@ package manager import ( - "bot/manager/utils" "strings" "time" + "github.com/Yiwen-Chan/ZeroBot-Plugin/manager/utils" zero "github.com/wdvxdr1123/ZeroBot" ) @@ -23,6 +23,8 @@ func init() { // 插件主体 - 修改名片@QQ XXX - 修改头衔@QQ XXX - 申请头衔 XXX +- 踢出群聊@QQ +- 退出群聊 1234 - 群聊转发 1234 XXX - 私聊转发 0000 XXX`) return @@ -251,4 +253,12 @@ func init() { // 插件主体 } return }) + // 运行 CQ 码 + zero.OnRegex(`^run(.*)$`, zero.SuperUserPermission).SetBlock(true).SetPriority(0). + Handle(func(ctx *zero.Ctx) { + var cmd = ctx.State["regex_matched"].([]string)[1] + cmd = strings.ReplaceAll(cmd, "[", "[") + cmd = strings.ReplaceAll(cmd, "]", "]") + ctx.Send(cmd) + }) } diff --git a/music/music_selecter.go b/music/music_selecter.go index b4179fc1..61a4a9e5 100644 --- a/music/music_selecter.go +++ b/music/music_selecter.go @@ -2,30 +2,77 @@ package setutime import ( "fmt" + "io/ioutil" + "net/http" + "net/url" + "strings" + "time" - "bot/music/utils" - + "github.com/tidwall/gjson" zero "github.com/wdvxdr1123/ZeroBot" + "github.com/wdvxdr1123/ZeroBot/extension/rate" ) +var limit = rate.NewManager(time.Minute*1, 3) + func init() { // 插件主体 zero.OnRegex(`^点歌(.*)$`).SetBlock(true).SetPriority(50). Handle(func(ctx *zero.Ctx) { - music, err := utils.CloudMusic(ctx.State["regex_matched"].([]string)[1]) + if limit.Load(ctx.Event.UserID).Acquire() == false { + ctx.Send("请稍后重试0x0...") + return + } + // 调用网易云 API + var api = "http://music.163.com/api/search/pc" + + client := &http.Client{} + + // 包装请求参数 + data := url.Values{} + data.Set("offset", "0") + data.Set("total", "true") + data.Set("limit", "9") + data.Set("type", "1") + data.Set("s", ctx.State["regex_matched"].([]string)[1]) + fromData := strings.NewReader(data.Encode()) + + // 网络请求 + req, err := http.NewRequest("POST", api, fromData) if err != nil { ctx.Send(fmt.Sprintf("ERROR: %v", err)) return } + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36") + resp, err := client.Do(req) + if err != nil { + ctx.Send(fmt.Sprintf("ERROR: %v", err)) + return + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + ctx.Send(fmt.Sprintf("ERROR: %v", err)) + return + } + + if code := resp.StatusCode; code != 200 { + // 如果返回不是200则立刻抛出错误 + ctx.Send(fmt.Sprintf("ERROR: code %d", code)) + return + } + content := gjson.ParseBytes(body).Get("result.songs.0") + // 发送搜索结果 ctx.Send( fmt.Sprintf( "[CQ:music,type=%s,url=%s,audio=%s,title=%s,content=%s,image=%s]", - music.Type, - music.Url, - music.Audio, - music.Title, - music.Content, - music.Image, + "custom", + fmt.Sprintf("http://y.music.163.com/m/song?id=%d", content.Get("id").Int()), + fmt.Sprintf("http://music.163.com/song/media/outer/url?id=%d.mp3", content.Get("id").Int()), + content.Get("name").Str, + content.Get("artists.0.name").Str, + content.Get("album.blurPicUrl").Str, ), ) return diff --git a/music/utils/cloud_music.go b/music/utils/cloud_music.go deleted file mode 100644 index 49ac0482..00000000 --- a/music/utils/cloud_music.go +++ /dev/null @@ -1,57 +0,0 @@ -package utils - -import ( - "errors" - "fmt" - "io/ioutil" - "net/http" - "net/url" - "strings" - - "github.com/tidwall/gjson" -) - -func CloudMusic(name string) (music CQMusic, err error) { - var api = "http://music.163.com/api/search/pc" - - client := &http.Client{} - - // 包装请求参数 - data := url.Values{} - data.Set("offset", "0") - data.Set("total", "true") - data.Set("limit", "9") - data.Set("type", "1") - data.Set("s", name) - fromData := strings.NewReader(data.Encode()) - - // 网络请求 - req, err := http.NewRequest("POST", api, fromData) - if err != nil { - return music, err - } - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36") - resp, err := client.Do(req) - if err != nil { - return music, err - } - defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return music, err - } - - if code := resp.StatusCode; code != 200 { - // 如果返回不是200则立刻抛出错误 - return music, errors.New(fmt.Sprintf("CloudMusic not found, code %d", code)) - } - content := gjson.ParseBytes(body).Get("result.songs.0") - music.Type = "custom" - music.Url = fmt.Sprintf("http://y.music.163.com/m/song?id=%d", content.Get("id").Int()) - music.Audio = fmt.Sprintf("http://music.163.com/song/media/outer/url?id=%d.mp3", content.Get("id").Int()) - music.Title = content.Get("name").Str - music.Content = content.Get("artists.0.name").Str - music.Image = content.Get("album.blurPicUrl").Str - return music, nil -} diff --git a/music/utils/utils.go b/music/utils/utils.go deleted file mode 100644 index fe642ca6..00000000 --- a/music/utils/utils.go +++ /dev/null @@ -1,10 +0,0 @@ -package utils - -type CQMusic struct { - Type string - Url string - Audio string - Title string - Content string - Image string -} diff --git a/run/runner.go b/run/runner.go deleted file mode 100644 index da28e33b..00000000 --- a/run/runner.go +++ /dev/null @@ -1,17 +0,0 @@ -package runner - -import ( - "strings" - - zero "github.com/wdvxdr1123/ZeroBot" -) - -func init() { // 插件主体 - zero.OnRegex(`^run(.*)$`, zero.SuperUserPermission).SetBlock(true).SetPriority(0). - Handle(func(ctx *zero.Ctx) { - var cmd = ctx.State["regex_matched"].([]string)[1] - cmd = strings.ReplaceAll(cmd, "[", "[") - cmd = strings.ReplaceAll(cmd, "]", "]") - ctx.Send(cmd) - }) -} diff --git a/setutime/pic_searcher.go b/setutime/pic_searcher.go index 5a3558ef..259aca44 100644 --- a/setutime/pic_searcher.go +++ b/setutime/pic_searcher.go @@ -1,9 +1,9 @@ package setutime import ( - utils "bot/setutime/utils" "fmt" + utils "github.com/Yiwen-Chan/ZeroBot-Plugin/setutime/utils" zero "github.com/wdvxdr1123/ZeroBot" ) diff --git a/setutime/setu_geter.go b/setutime/setu_geter.go index 00a81e74..fb94e254 100644 --- a/setutime/setu_geter.go +++ b/setutime/setu_geter.go @@ -6,8 +6,7 @@ import ( "strings" "time" - utils "bot/setutime/utils" - + "github.com/Yiwen-Chan/ZeroBot-Plugin/setutime/utils" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/extension/rate" )