🎨 调整代码结构

This commit is contained in:
Yiwen-Chan 2021-04-01 14:25:54 +08:00
parent 14d07bf963
commit 9a47bc93b0
10 changed files with 75 additions and 105 deletions

View File

@ -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...
### 使用方法

2
go.mod
View File

@ -1,4 +1,4 @@
module bot
module github.com/Yiwen-Chan/ZeroBot-Plugin
go 1.15

View File

@ -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() {

View File

@ -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)
})
}

View File

@ -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

View File

@ -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
}

View File

@ -1,10 +0,0 @@
package utils
type CQMusic struct {
Type string
Url string
Audio string
Title string
Content string
Image string
}

View File

@ -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)
})
}

View File

@ -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"
)

View File

@ -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"
)