🎨 优化vtb插件代码,更换shadiao的api (#206)

* 🎨 优化vtb插件代码,更换shadiao的api

* 🎨 正则全局变量

Co-authored-by: haibaraguo <haibaraguo@yeahka.com>
This commit is contained in:
himawari 2022-04-28 17:37:57 +08:00 committed by GitHub
parent ba4ca11c83
commit 484fef1228
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 109 additions and 146 deletions

View File

@ -42,7 +42,7 @@ func init() {
}) })
cachePath := engine.DataFolder() + "cache/" cachePath := engine.DataFolder() + "cache/"
go func() { go func() {
os.RemoveAll(cachePath) _ = os.RemoveAll(cachePath)
err := os.MkdirAll(cachePath, 0755) err := os.MkdirAll(cachePath, 0755)
if err != nil { if err != nil {
panic(err) panic(err)
@ -234,7 +234,7 @@ func init() {
err = graph.Render(chart.PNG, f) err = graph.Render(chart.PNG, f)
_ = f.Close() _ = f.Close()
if err != nil { if err != nil {
os.Remove(drawedFile) _ = os.Remove(drawedFile)
ctx.SendChain(message.Text("ERROR:", err)) ctx.SendChain(message.Text("ERROR:", err))
return return
} }

View File

@ -6,9 +6,9 @@ import (
) )
const ( const (
chpURL = "https://chp.shadiao.app/api.php" chpURL = "https://chp.shadiao.app/chp"
duURL = "https://du.shadiao.app/api.php" duURL = "https://api.shadiao.app/du"
pyqURL = "https://pyq.shadiao.app/api.php" pyqURL = "https://api.shadiao.app/pyq"
yduanziURL = "http://www.yduanzi.com/duanzi/getduanzi" yduanziURL = "http://www.yduanzi.com/duanzi/getduanzi"
chayiURL = "https://api.lovelive.tools/api/SweetNothings/Web/0" chayiURL = "https://api.lovelive.tools/api/SweetNothings/Web/0"
ganhaiURL = "https://api.lovelive.tools/api/SweetNothings/Web/1" ganhaiURL = "https://api.lovelive.tools/api/SweetNothings/Web/1"

View File

@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"path"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -26,12 +27,7 @@ import (
"github.com/FloatTech/ZeroBot-Plugin/plugin/vtb_quotation/model" "github.com/FloatTech/ZeroBot-Plugin/plugin/vtb_quotation/model"
) )
const regStr = ".*/(.*)" var reg = regexp.MustCompile(".*/(.*)")
const recordRe = "(\\.mp3|\\.wav|\\.wma|\\.mpa|\\.ram|\\.ra|\\.aac|\\.aif|\\.m4a|\\.tsa)"
var (
re = regexp.MustCompile(recordRe)
)
func init() { func init() {
engine := control.Register("vtbquotation", &control.Options{ engine := control.Register("vtbquotation", &control.Options{
@ -57,9 +53,7 @@ func init() {
engine.OnFullMatch("vtb语录", getdb).SetBlock(true). engine.OnFullMatch("vtb语录", getdb).SetBlock(true).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
var firstIndex int indexs := [3]int{}
var secondIndex int
var thirdIndex int
echo, cancel := ctx.FutureEvent("message", echo, cancel := ctx.FutureEvent("message",
ctx.CheckSession()). // 只复读开启复读模式的人的消息 ctx.CheckSession()). // 只复读开启复读模式的人的消息
Repeat() // 不断监听复读 Repeat() // 不断监听复读
@ -95,21 +89,21 @@ func init() {
ctx.SendChain(message.Reply(c.Event.MessageID), message.Text("输入错误太多,请重新发指令")) ctx.SendChain(message.Reply(c.Event.MessageID), message.Text("输入错误太多,请重新发指令"))
return return
} }
msg := c.Event.Message.ExtractPlainText()
num, err := strconv.Atoi(msg)
if err != nil {
ctx.SendChain(message.Reply(c.Event.MessageID), message.Text("请输入正确的序号,三次输入错误,指令可退出重输"))
errorCount++
continue
}
switch step { switch step {
case 0: case 0:
firstIndex, err = strconv.Atoi(c.Event.RawMessage) indexs[0] = num
// log.Debugln(fmt.Sprintf("当前在第%d步", step)) secondStepMessage, err := db.GetAllSecondCategoryMessageByFirstIndex(indexs[0])
// log.Debugln(fmt.Sprintf("firstIndex:%d,secondIndex:%d,thirdIndex:%d", firstIndex, secondIndex, thirdIndex))
if err != nil {
ctx.SendChain(message.Reply(c.Event.MessageID), message.Text("请输入正确的序号,三次输入错误,指令可退出重输"))
errorCount++
} else {
secondStepMessage, err := db.GetAllSecondCategoryMessageByFirstIndex(firstIndex)
if err != nil { if err != nil {
ctx.SendChain(message.Text("ERROR:", err)) ctx.SendChain(message.Text("ERROR:", err))
return return
} }
// log.Debugln(secondStepMessage)
if secondStepMessage == "" { if secondStepMessage == "" {
ctx.SendChain(message.Reply(c.Event.MessageID), message.Text("你选择的序号没有内容,请重新选择,三次输入错误,指令可退出重输")) ctx.SendChain(message.Reply(c.Event.MessageID), message.Text("你选择的序号没有内容,请重新选择,三次输入错误,指令可退出重输"))
r, err := db.GetAllFirstCategoryMessage() r, err := db.GetAllFirstCategoryMessage()
@ -137,24 +131,17 @@ func init() {
} }
step++ step++
} }
}
case 1: case 1:
secondIndex, err = strconv.Atoi(c.Event.RawMessage) indexs[1] = num
// log.Debugln(fmt.Sprintf("当前在第%d步", step)) thirdStepMessage, err := db.GetAllThirdCategoryMessageByFirstIndexAndSecondIndex(indexs[0], indexs[1])
// log.Debugln(fmt.Sprintf("firstIndex:%d,secondIndex:%d,thirdIndex:%d", firstIndex, secondIndex, thirdIndex))
if err != nil {
ctx.SendChain(message.Reply(c.Event.MessageID), message.Text("请输入正确的序号,三次输入错误,指令可退出重输"))
errorCount++
} else {
thirdStepMessage, err := db.GetAllThirdCategoryMessageByFirstIndexAndSecondIndex(firstIndex, secondIndex)
if err != nil { if err != nil {
ctx.SendChain(message.Text("ERROR:", err)) ctx.SendChain(message.Text("ERROR:", err))
return return
} }
// log.Debugln(thirdStepMessage)
if thirdStepMessage == "" { if thirdStepMessage == "" {
ctx.SendChain(message.Reply(c.Event.MessageID), message.Text("你选择的序号没有内容,请重新选择,三次输入错误,指令可退出重输")) ctx.SendChain(message.Reply(c.Event.MessageID), message.Text("你选择的序号没有内容,请重新选择,三次输入错误,指令可退出重输"))
r, err := db.GetAllSecondCategoryMessageByFirstIndex(firstIndex) r, err := db.GetAllSecondCategoryMessageByFirstIndex(indexs[0])
if err != nil { if err != nil {
ctx.SendChain(message.Text("ERROR:", err)) ctx.SendChain(message.Text("ERROR:", err))
return return
@ -179,17 +166,9 @@ func init() {
} }
step++ step++
} }
}
case 2: case 2:
thirdIndex, err = strconv.Atoi(c.Event.RawMessage) indexs[2] = num
// log.Debugln(fmt.Sprintf("当前在第%d步", step)) tc := db.GetThirdCategory(indexs[0], indexs[1], indexs[2])
// log.Debugln(fmt.Sprintf("firstIndex:%d,secondIndex:%d,thirdIndex:%d", firstIndex, secondIndex, thirdIndex))
if err != nil {
ctx.SendChain(message.Reply(c.Event.MessageID), message.Text("请输入正确的序号,三次输入错误,指令可退出重输"))
errorCount++
} else {
tc := db.GetThirdCategory(firstIndex, secondIndex, thirdIndex)
reg := regexp.MustCompile(regStr)
recURL := tc.ThirdCategoryPath recURL := tc.ThirdCategoryPath
if recURL == "" { if recURL == "" {
ctx.SendChain(message.Reply(c.Event.MessageID), message.Text("没有内容请重新选择,三次输入错误,指令可退出重输")) ctx.SendChain(message.Reply(c.Event.MessageID), message.Text("没有内容请重新选择,三次输入错误,指令可退出重输"))
@ -210,20 +189,11 @@ func init() {
step = 1 step = 1
} else { } else {
if reg.MatchString(recURL) { if reg.MatchString(recURL) {
// log.Debugln(reg.FindStringSubmatch(recordUrl)[1])
// log.Debugln(url.QueryEscape(reg.FindStringSubmatch(recordUrl)[1]))
recURL = strings.ReplaceAll(recURL, reg.FindStringSubmatch(recURL)[1], url.QueryEscape(reg.FindStringSubmatch(recURL)[1])) recURL = strings.ReplaceAll(recURL, reg.FindStringSubmatch(recURL)[1], url.QueryEscape(reg.FindStringSubmatch(recURL)[1]))
recURL = strings.ReplaceAll(recURL, "+", "%20") recURL = strings.ReplaceAll(recURL, "+", "%20")
// log.Debugln(recordUrl)
} }
ctx.SendChain(message.Reply(c.Event.MessageID), message.Text("请欣赏《"+tc.ThirdCategoryName+"》")) ctx.SendChain(message.Reply(c.Event.MessageID), message.Text("请欣赏《"+tc.ThirdCategoryName+"》"))
recordFile := storePath + fmt.Sprintf("%d-%d-%d", indexs[0], indexs[1], indexs[2]) + path.Ext(recURL)
if !re.MatchString(recURL) {
ctx.SendChain(message.Text("ERROR:文件格式不匹配"))
return
}
format := re.FindStringSubmatch(recURL)[1]
recordFile := storePath + fmt.Sprintf("%d-%d-%d", firstIndex, secondIndex, thirdIndex) + format
if file.IsExist(recordFile) { if file.IsExist(recordFile) {
ctx.SendChain(message.Record("file:///" + file.BOTPATH + "/" + recordFile)) ctx.SendChain(message.Record("file:///" + file.BOTPATH + "/" + recordFile))
return return
@ -236,7 +206,6 @@ func init() {
ctx.SendChain(message.Record("file:///" + file.BOTPATH + "/" + recordFile)) ctx.SendChain(message.Record("file:///" + file.BOTPATH + "/" + recordFile))
return return
} }
}
default: default:
return return
} }
@ -257,19 +226,13 @@ func init() {
tc := db.RandomVtb() tc := db.RandomVtb()
fc := db.GetFirstCategoryByFirstUID(tc.FirstCategoryUID) fc := db.GetFirstCategoryByFirstUID(tc.FirstCategoryUID)
if (tc != model.ThirdCategory{}) && (fc != model.FirstCategory{}) { if (tc != model.ThirdCategory{}) && (fc != model.FirstCategory{}) {
reg := regexp.MustCompile(regStr)
recURL := tc.ThirdCategoryPath recURL := tc.ThirdCategoryPath
if reg.MatchString(recURL) { if reg.MatchString(recURL) {
recURL = strings.ReplaceAll(recURL, reg.FindStringSubmatch(recURL)[1], url.QueryEscape(reg.FindStringSubmatch(recURL)[1])) recURL = strings.ReplaceAll(recURL, reg.FindStringSubmatch(recURL)[1], url.QueryEscape(reg.FindStringSubmatch(recURL)[1]))
recURL = strings.ReplaceAll(recURL, "+", "%20") recURL = strings.ReplaceAll(recURL, "+", "%20")
} }
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("请欣赏"+fc.FirstCategoryName+"的《"+tc.ThirdCategoryName+"》")) ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("请欣赏"+fc.FirstCategoryName+"的《"+tc.ThirdCategoryName+"》"))
if !re.MatchString(recURL) { recordFile := storePath + fmt.Sprintf("%d-%d-%d", fc.FirstCategoryIndex, tc.SecondCategoryIndex, tc.ThirdCategoryIndex) + path.Ext(recURL)
ctx.SendChain(message.Text("ERROR:文件格式不匹配"))
return
}
format := re.FindStringSubmatch(recURL)[1]
recordFile := storePath + fmt.Sprintf("%d-%d-%d", fc.FirstCategoryIndex, tc.SecondCategoryIndex, tc.ThirdCategoryIndex) + format
if file.IsExist(recordFile) { if file.IsExist(recordFile) {
ctx.SendChain(message.Record("file:///" + file.BOTPATH + "/" + recordFile)) ctx.SendChain(message.Record("file:///" + file.BOTPATH + "/" + recordFile))
return return