Skip to content

Commit

Permalink
优化猜歌流程
Browse files Browse the repository at this point in the history
猜歌结束后不发送完整歌曲
  • Loading branch information
vatebur committed Dec 8, 2023
1 parent 71de9f1 commit 7b934e7
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 9 deletions.
49 changes: 46 additions & 3 deletions plugin/guessmusic/apiservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package guessmusic
import (
"encoding/json"
"math/rand"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"

"github.com/FloatTech/floatbox/process"

wyy "github.com/FloatTech/AnimeAPI/neteasemusic"
"github.com/FloatTech/floatbox/file"
Expand Down Expand Up @@ -303,6 +306,46 @@ func init() {
})
}

// DownloadMusic 下载网易云音乐(歌曲ID,歌曲名称,下载路径)
func DownloadMusic(musicID int, musicName, pathOfMusic string) error {

APIURL := cfg.APIURL + "song/url?id=" + strconv.Itoa(musicID)
data, err := web.GetData(APIURL)
if err != nil {
return err
}
var parsed musicDownloadURL
err = json.Unmarshal(data, &parsed)
if err != nil {
return err
}

if parsed.Code != 200 {
err = errors.Errorf("requset code : %d", parsed.Code)
return err
}

musicURL := parsed.Data.URL
downMusic := pathOfMusic + "/" + musicName + ".mp3"

if file.IsNotExist(downMusic) {
// 检查歌曲是否存在
response, err := http.Head(musicURL)
if err != nil {
return err
}
_ = response.Body.Close()
if response.StatusCode != 200 {
return errors.Errorf("Status Code: %d", response.StatusCode)
}
// 下载歌曲
err = file.DownloadTo(musicURL, downMusic)
process.SleepAbout1sTo2s()
return err
}
return nil
}

// 随机从歌单下载歌曲(歌单ID, 音乐保存路径)
func drawByAPI(playlistID int64, musicPath string) (musicName string, err error) {
APIURL := cfg.APIURL + "playlist/track/all?id=" + strconv.FormatInt(playlistID, 10) + "&limit=1000"
Expand Down Expand Up @@ -346,7 +389,7 @@ func drawByAPI(playlistID int64, musicPath string) (musicName string, err error)
name += " - " + artistName
}
// 下载歌曲
err = wyy.DownloadMusic(musicID, name, musicPath)
err = DownloadMusic(musicID, name, musicPath)
if err == nil {
musicName = name + ".mp3"
if cfg.Local {
Expand Down Expand Up @@ -401,7 +444,7 @@ func downloadlist(playlistID int64, musicPath string) error {
musicName += " - " + artistName
}
// 下载歌曲
err = wyy.DownloadMusic(musicID, musicName, musicPath)
err = DownloadMusic(musicID, musicName, musicPath)
if err == nil {
if cfg.Local {
// 下载歌词
Expand Down Expand Up @@ -440,7 +483,7 @@ func downloadByOvooa(playlistID int64, musicPath string) (musicName string, err
}
name := musicList[0]
// 下载歌曲
err = wyy.DownloadMusic(mid, name, musicPath)
err = DownloadMusic(mid, name, musicPath)
if err == nil {
musicName = name + ".mp3"
if cfg.Local {
Expand Down
13 changes: 7 additions & 6 deletions plugin/guessmusic/guessmusic.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ func init() {
tick.Stop()
after.Stop()
ctx.SendChain(message.Reply(c.Event.MessageID), messageStr)
ctx.SendChain(message.Record("file:///" + pathOfMusic + musicName))
//歌曲结束后不发送完整歌曲
//ctx.SendChain(message.Record("file:///" + pathOfMusic + musicName))
} else {
wait.Reset(40 * time.Second)
tick.Reset(105 * time.Second)
Expand Down Expand Up @@ -286,7 +287,7 @@ func gameMatch(c *zero.Ctx, beginner int64, musicInfo []string, answerTimes, tic
switch {
case answer == "取消":
if c.Event.UserID == beginner {
return message.Text("游戏已取消,猜歌答案是\n", musicInfo[len(musicInfo)-1], "\n\n\n下面欣赏猜歌的歌曲"), answerTimes, tickTimes, true
return message.Text("游戏已取消,猜歌答案是\n", musicInfo[len(musicInfo)-1]), answerTimes, tickTimes, true
}
return message.Text("你无权限取消"), answerTimes, tickTimes, false
case answer == "提示":
Expand All @@ -296,19 +297,19 @@ func gameMatch(c *zero.Ctx, beginner int64, musicInfo []string, answerTimes, tic
}
return message.Text("再听这段音频,要仔细听哦"), answerTimes, tickTimes, false
case strings.Contains(musicInfo[0], answer) || strings.EqualFold(musicInfo[0], answer):
return message.Text("太棒了,你猜对歌曲名了!答案是\n", musicInfo[len(musicInfo)-1], "\n\n下面欣赏猜歌的歌曲"), answerTimes, tickTimes, true
return message.Text("太棒了,你猜对歌曲名了!答案是\n", musicInfo[len(musicInfo)-1]), answerTimes, tickTimes, true
case strings.Contains(musicInfo[1], answer) || strings.EqualFold(musicInfo[1], answer):
return message.Text("太棒了,你猜对歌手名了!答案是\n", musicInfo[len(musicInfo)-1], "\n\n下面欣赏猜歌的歌曲"), answerTimes, tickTimes, true
return message.Text("太棒了,你猜对歌手名了!答案是\n", musicInfo[len(musicInfo)-1]), answerTimes, tickTimes, true
case len(musicInfo) == 4 && (strings.Contains(musicInfo[2], answer) || strings.EqualFold(musicInfo[2], answer)):
return message.Text("太棒了,你猜对相关信息了!答案是\n", musicInfo[len(musicInfo)-1], "\n\n下面欣赏猜歌的歌曲"), answerTimes, tickTimes, true
return message.Text("太棒了,你猜对相关信息了!答案是\n", musicInfo[len(musicInfo)-1]), answerTimes, tickTimes, true
default:
answerTimes++
tickTimes++
switch {
case tickTimes > 2 && answerTimes < 6:
return message.Text("答案不对哦,还有", 6-answerTimes, "次答题,加油啊~"), answerTimes, tickTimes, false
case tickTimes > 2:
return message.Text("次数到了,没能猜出来。答案是\n", musicInfo[len(musicInfo)-1], "\n\n下面欣赏猜歌的歌曲"), answerTimes, tickTimes, true
return message.Text("次数到了,没能猜出来。答案是\n", musicInfo[len(musicInfo)-1]), answerTimes, tickTimes, true
default:
return message.Text("答案不对,再听这段音频,要仔细听哦"), answerTimes, tickTimes, false
}
Expand Down
43 changes: 43 additions & 0 deletions plugin/guessmusic/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,49 @@ type qrInfo struct {
} `json:"data"`
}

type musicDownloadURL struct {
Data struct {
ID int `json:"id"`
URL string `json:"url"`
Br int `json:"br"`
Size int `json:"size"`
Md5 interface{} `json:"md5"`
Code int `json:"code"`
Expi int `json:"expi"`
Type interface{} `json:"type"`
Gain int `json:"gain"`
Peak interface{} `json:"peak"`
Fee int `json:"fee"`
Uf interface{} `json:"uf"`
Payed int `json:"payed"`
Flag int `json:"flag"`
CanExtend bool `json:"canExtend"`
FreeTrialInfo interface{} `json:"freeTrialInfo"`
Level interface{} `json:"level"`
EncodeType interface{} `json:"encodeType"`
ChannelLayout interface{} `json:"channelLayout"`
FreeTrialPrivilege struct {
ResConsumable bool `json:"resConsumable"`
UserConsumable bool `json:"userConsumable"`
ListenType interface{} `json:"listenType"`
CannotListenReason int `json:"cannotListenReason"`
PlayReason interface{} `json:"playReason"`
} `json:"freeTrialPrivilege"`
FreeTimeTrialPrivilege struct {
ResConsumable bool `json:"resConsumable"`
UserConsumable bool `json:"userConsumable"`
Type int `json:"type"`
RemainTime int `json:"remainTime"`
} `json:"freeTimeTrialPrivilege"`
URLSource int `json:"urlSource"`
RightSource int `json:"rightSource"`
PodcastCtrp interface{} `json:"podcastCtrp"`
EffectTypes interface{} `json:"effectTypes"`
Time int `json:"time"`
} `json:"data"`
Code int `json:"code"`
}

// 获取歌单信息
type listInfoOfAPI struct {
Code int `json:"code"`
Expand Down

0 comments on commit 7b934e7

Please sign in to comment.