mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-19 05:30:07 +08:00
✨ Add music selector
This commit is contained in:
parent
52cb435fa4
commit
52d9bd0a80
50
music/music.go
Normal file
50
music/music.go
Normal file
@ -0,0 +1,50 @@
|
||||
package setutime
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"bot/music/utils"
|
||||
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
)
|
||||
|
||||
func init() {
|
||||
zero.RegisterPlugin(musicSelector{}) // 注册插件
|
||||
}
|
||||
|
||||
type musicSelector struct{} // musicSelector 点歌
|
||||
|
||||
func (_ musicSelector) GetPluginInfo() zero.PluginInfo { // 返回插件信息
|
||||
return zero.PluginInfo{
|
||||
Author: "kanri",
|
||||
PluginName: "MusicSelector",
|
||||
Version: "0.0.1",
|
||||
Details: "点歌",
|
||||
}
|
||||
}
|
||||
|
||||
func (_ musicSelector) Start() { // 插件主体
|
||||
// TODO 根据PID搜图
|
||||
zero.OnRegex(`点歌(.*)`).SetBlock(true).SetPriority(50).
|
||||
Handle(func(matcher *zero.Matcher, event zero.Event, state zero.State) zero.Response {
|
||||
music, err := utils.CloudMusic(state["regex_matched"].([]string)[1])
|
||||
if err != nil {
|
||||
utils.SendError(event, err)
|
||||
return zero.FinishResponse
|
||||
}
|
||||
// TODO 发送搜索结果
|
||||
zero.Send(
|
||||
event,
|
||||
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,
|
||||
),
|
||||
)
|
||||
return zero.FinishResponse
|
||||
})
|
||||
}
|
||||
56
music/utils/cloud_music.go
Normal file
56
music/utils/cloud_music.go
Normal file
@ -0,0 +1,56 @@
|
||||
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{}
|
||||
|
||||
// TODO 包装请求参数
|
||||
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())
|
||||
|
||||
// TODO 网络请求
|
||||
req, err := http.NewRequest("POST", api, fromData)
|
||||
if err != nil {
|
||||
return music, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
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
|
||||
}
|
||||
20
music/utils/utils.go
Normal file
20
music/utils/utils.go
Normal file
@ -0,0 +1,20 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
)
|
||||
|
||||
type CQMusic struct {
|
||||
Type string
|
||||
Url string
|
||||
Audio string
|
||||
Title string
|
||||
Content string
|
||||
Image string
|
||||
}
|
||||
|
||||
func SendError(event zero.Event, err error) {
|
||||
zero.Send(event, fmt.Sprintf("ERROR: %v", err))
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user