mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-19 05:30:07 +08:00
🎨 优化vtb插件代码,更换shadiao的api (#206)
* 🎨 优化vtb插件代码,更换shadiao的api * 🎨 正则全局变量 Co-authored-by: haibaraguo <haibaraguo@yeahka.com>
This commit is contained in:
parent
ba4ca11c83
commit
484fef1228
@ -42,7 +42,7 @@ func init() {
|
||||
})
|
||||
cachePath := engine.DataFolder() + "cache/"
|
||||
go func() {
|
||||
os.RemoveAll(cachePath)
|
||||
_ = os.RemoveAll(cachePath)
|
||||
err := os.MkdirAll(cachePath, 0755)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -234,7 +234,7 @@ func init() {
|
||||
err = graph.Render(chart.PNG, f)
|
||||
_ = f.Close()
|
||||
if err != nil {
|
||||
os.Remove(drawedFile)
|
||||
_ = os.Remove(drawedFile)
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
return
|
||||
}
|
||||
|
||||
@ -6,9 +6,9 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
chpURL = "https://chp.shadiao.app/api.php"
|
||||
duURL = "https://du.shadiao.app/api.php"
|
||||
pyqURL = "https://pyq.shadiao.app/api.php"
|
||||
chpURL = "https://chp.shadiao.app/chp"
|
||||
duURL = "https://api.shadiao.app/du"
|
||||
pyqURL = "https://api.shadiao.app/pyq"
|
||||
yduanziURL = "http://www.yduanzi.com/duanzi/getduanzi"
|
||||
chayiURL = "https://api.lovelive.tools/api/SweetNothings/Web/0"
|
||||
ganhaiURL = "https://api.lovelive.tools/api/SweetNothings/Web/1"
|
||||
|
||||
@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -26,12 +27,7 @@ import (
|
||||
"github.com/FloatTech/ZeroBot-Plugin/plugin/vtb_quotation/model"
|
||||
)
|
||||
|
||||
const regStr = ".*/(.*)"
|
||||
const recordRe = "(\\.mp3|\\.wav|\\.wma|\\.mpa|\\.ram|\\.ra|\\.aac|\\.aif|\\.m4a|\\.tsa)"
|
||||
|
||||
var (
|
||||
re = regexp.MustCompile(recordRe)
|
||||
)
|
||||
var reg = regexp.MustCompile(".*/(.*)")
|
||||
|
||||
func init() {
|
||||
engine := control.Register("vtbquotation", &control.Options{
|
||||
@ -57,9 +53,7 @@ func init() {
|
||||
|
||||
engine.OnFullMatch("vtb语录", getdb).SetBlock(true).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
var firstIndex int
|
||||
var secondIndex int
|
||||
var thirdIndex int
|
||||
indexs := [3]int{}
|
||||
echo, cancel := ctx.FutureEvent("message",
|
||||
ctx.CheckSession()). // 只复读开启复读模式的人的消息
|
||||
Repeat() // 不断监听复读
|
||||
@ -95,147 +89,122 @@ func init() {
|
||||
ctx.SendChain(message.Reply(c.Event.MessageID), message.Text("输入错误太多,请重新发指令"))
|
||||
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 {
|
||||
case 0:
|
||||
firstIndex, err = strconv.Atoi(c.Event.RawMessage)
|
||||
// log.Debugln(fmt.Sprintf("当前在第%d步", step))
|
||||
// log.Debugln(fmt.Sprintf("firstIndex:%d,secondIndex:%d,thirdIndex:%d", firstIndex, secondIndex, thirdIndex))
|
||||
indexs[0] = num
|
||||
secondStepMessage, err := db.GetAllSecondCategoryMessageByFirstIndex(indexs[0])
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Reply(c.Event.MessageID), message.Text("请输入正确的序号,三次输入错误,指令可退出重输"))
|
||||
errorCount++
|
||||
} else {
|
||||
secondStepMessage, err := db.GetAllSecondCategoryMessageByFirstIndex(firstIndex)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
return
|
||||
}
|
||||
// log.Debugln(secondStepMessage)
|
||||
if secondStepMessage == "" {
|
||||
ctx.SendChain(message.Reply(c.Event.MessageID), message.Text("你选择的序号没有内容,请重新选择,三次输入错误,指令可退出重输"))
|
||||
r, err := db.GetAllFirstCategoryMessage()
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
return
|
||||
}
|
||||
firstStepImageBytes, err := text.RenderToBase64(r, text.FontFile, 400, 20)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
return
|
||||
}
|
||||
if id := ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Image("base64://"+helper.BytesToString(firstStepImageBytes))); id.ID() == 0 {
|
||||
ctx.SendChain(message.Text("ERROR:可能被风控了"))
|
||||
}
|
||||
errorCount++
|
||||
} else {
|
||||
secondStepMessageBytes, err := text.RenderToBase64(secondStepMessage, text.FontFile, 400, 20)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
return
|
||||
}
|
||||
if id := ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Image("base64://"+helper.BytesToString(secondStepMessageBytes))); id.ID() == 0 {
|
||||
ctx.SendChain(message.Text("ERROR:可能被风控了"))
|
||||
}
|
||||
step++
|
||||
}
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
return
|
||||
}
|
||||
case 1:
|
||||
secondIndex, err = strconv.Atoi(c.Event.RawMessage)
|
||||
// log.Debugln(fmt.Sprintf("当前在第%d步", step))
|
||||
// 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 secondStepMessage == "" {
|
||||
ctx.SendChain(message.Reply(c.Event.MessageID), message.Text("你选择的序号没有内容,请重新选择,三次输入错误,指令可退出重输"))
|
||||
r, err := db.GetAllFirstCategoryMessage()
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
return
|
||||
}
|
||||
// log.Debugln(thirdStepMessage)
|
||||
if thirdStepMessage == "" {
|
||||
ctx.SendChain(message.Reply(c.Event.MessageID), message.Text("你选择的序号没有内容,请重新选择,三次输入错误,指令可退出重输"))
|
||||
r, err := db.GetAllSecondCategoryMessageByFirstIndex(firstIndex)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
return
|
||||
}
|
||||
secondStepMessageBytes, err := text.RenderToBase64(r, text.FontFile, 400, 20)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
return
|
||||
}
|
||||
if id := ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Image("base64://"+helper.BytesToString(secondStepMessageBytes))); id.ID() == 0 {
|
||||
ctx.SendChain(message.Text("ERROR:可能被风控了"))
|
||||
}
|
||||
errorCount++
|
||||
} else {
|
||||
thirdStepMessageBytes, err := text.RenderToBase64(thirdStepMessage, text.FontFile, 400, 20)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
return
|
||||
}
|
||||
if id := ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Image("base64://"+helper.BytesToString(thirdStepMessageBytes))); id.ID() == 0 {
|
||||
ctx.SendChain(message.Text("ERROR:可能被风控了"))
|
||||
}
|
||||
step++
|
||||
firstStepImageBytes, err := text.RenderToBase64(r, text.FontFile, 400, 20)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
return
|
||||
}
|
||||
if id := ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Image("base64://"+helper.BytesToString(firstStepImageBytes))); id.ID() == 0 {
|
||||
ctx.SendChain(message.Text("ERROR:可能被风控了"))
|
||||
}
|
||||
errorCount++
|
||||
} else {
|
||||
secondStepMessageBytes, err := text.RenderToBase64(secondStepMessage, text.FontFile, 400, 20)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
return
|
||||
}
|
||||
if id := ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Image("base64://"+helper.BytesToString(secondStepMessageBytes))); id.ID() == 0 {
|
||||
ctx.SendChain(message.Text("ERROR:可能被风控了"))
|
||||
}
|
||||
step++
|
||||
}
|
||||
|
||||
case 1:
|
||||
indexs[1] = num
|
||||
thirdStepMessage, err := db.GetAllThirdCategoryMessageByFirstIndexAndSecondIndex(indexs[0], indexs[1])
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
return
|
||||
}
|
||||
if thirdStepMessage == "" {
|
||||
ctx.SendChain(message.Reply(c.Event.MessageID), message.Text("你选择的序号没有内容,请重新选择,三次输入错误,指令可退出重输"))
|
||||
r, err := db.GetAllSecondCategoryMessageByFirstIndex(indexs[0])
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
return
|
||||
}
|
||||
secondStepMessageBytes, err := text.RenderToBase64(r, text.FontFile, 400, 20)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
return
|
||||
}
|
||||
if id := ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Image("base64://"+helper.BytesToString(secondStepMessageBytes))); id.ID() == 0 {
|
||||
ctx.SendChain(message.Text("ERROR:可能被风控了"))
|
||||
}
|
||||
errorCount++
|
||||
} else {
|
||||
thirdStepMessageBytes, err := text.RenderToBase64(thirdStepMessage, text.FontFile, 400, 20)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
return
|
||||
}
|
||||
if id := ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Image("base64://"+helper.BytesToString(thirdStepMessageBytes))); id.ID() == 0 {
|
||||
ctx.SendChain(message.Text("ERROR:可能被风控了"))
|
||||
}
|
||||
step++
|
||||
}
|
||||
case 2:
|
||||
thirdIndex, err = strconv.Atoi(c.Event.RawMessage)
|
||||
// log.Debugln(fmt.Sprintf("当前在第%d步", step))
|
||||
// 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("请输入正确的序号,三次输入错误,指令可退出重输"))
|
||||
indexs[2] = num
|
||||
tc := db.GetThirdCategory(indexs[0], indexs[1], indexs[2])
|
||||
recURL := tc.ThirdCategoryPath
|
||||
if recURL == "" {
|
||||
ctx.SendChain(message.Reply(c.Event.MessageID), message.Text("没有内容请重新选择,三次输入错误,指令可退出重输"))
|
||||
r, err := db.GetAllFirstCategoryMessage()
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
return
|
||||
}
|
||||
firstStepImageBytes, err := text.RenderToBase64(r, text.FontFile, 400, 20)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
return
|
||||
}
|
||||
if id := ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Image("base64://"+helper.BytesToString(firstStepImageBytes))); id.ID() == 0 {
|
||||
ctx.SendChain(message.Text("ERROR:可能被风控了"))
|
||||
}
|
||||
errorCount++
|
||||
step = 1
|
||||
} else {
|
||||
tc := db.GetThirdCategory(firstIndex, secondIndex, thirdIndex)
|
||||
reg := regexp.MustCompile(regStr)
|
||||
recURL := tc.ThirdCategoryPath
|
||||
if recURL == "" {
|
||||
ctx.SendChain(message.Reply(c.Event.MessageID), message.Text("没有内容请重新选择,三次输入错误,指令可退出重输"))
|
||||
r, err := db.GetAllFirstCategoryMessage()
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
return
|
||||
}
|
||||
firstStepImageBytes, err := text.RenderToBase64(r, text.FontFile, 400, 20)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
return
|
||||
}
|
||||
if id := ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Image("base64://"+helper.BytesToString(firstStepImageBytes))); id.ID() == 0 {
|
||||
ctx.SendChain(message.Text("ERROR:可能被风控了"))
|
||||
}
|
||||
errorCount++
|
||||
step = 1
|
||||
} else {
|
||||
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, "+", "%20")
|
||||
// log.Debugln(recordUrl)
|
||||
}
|
||||
ctx.SendChain(message.Reply(c.Event.MessageID), message.Text("请欣赏《"+tc.ThirdCategoryName+"》"))
|
||||
|
||||
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) {
|
||||
ctx.SendChain(message.Record("file:///" + file.BOTPATH + "/" + recordFile))
|
||||
return
|
||||
}
|
||||
err = initRecord(recordFile, recURL)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
return
|
||||
}
|
||||
if reg.MatchString(recURL) {
|
||||
recURL = strings.ReplaceAll(recURL, reg.FindStringSubmatch(recURL)[1], url.QueryEscape(reg.FindStringSubmatch(recURL)[1]))
|
||||
recURL = strings.ReplaceAll(recURL, "+", "%20")
|
||||
}
|
||||
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 file.IsExist(recordFile) {
|
||||
ctx.SendChain(message.Record("file:///" + file.BOTPATH + "/" + recordFile))
|
||||
return
|
||||
}
|
||||
err = initRecord(recordFile, recURL)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
return
|
||||
}
|
||||
ctx.SendChain(message.Record("file:///" + file.BOTPATH + "/" + recordFile))
|
||||
return
|
||||
}
|
||||
default:
|
||||
return
|
||||
@ -257,19 +226,13 @@ func init() {
|
||||
tc := db.RandomVtb()
|
||||
fc := db.GetFirstCategoryByFirstUID(tc.FirstCategoryUID)
|
||||
if (tc != model.ThirdCategory{}) && (fc != model.FirstCategory{}) {
|
||||
reg := regexp.MustCompile(regStr)
|
||||
recURL := tc.ThirdCategoryPath
|
||||
if reg.MatchString(recURL) {
|
||||
recURL = strings.ReplaceAll(recURL, reg.FindStringSubmatch(recURL)[1], url.QueryEscape(reg.FindStringSubmatch(recURL)[1]))
|
||||
recURL = strings.ReplaceAll(recURL, "+", "%20")
|
||||
}
|
||||
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("请欣赏"+fc.FirstCategoryName+"的《"+tc.ThirdCategoryName+"》"))
|
||||
if !re.MatchString(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
|
||||
recordFile := storePath + fmt.Sprintf("%d-%d-%d", fc.FirstCategoryIndex, tc.SecondCategoryIndex, tc.ThirdCategoryIndex) + path.Ext(recURL)
|
||||
if file.IsExist(recordFile) {
|
||||
ctx.SendChain(message.Record("file:///" + file.BOTPATH + "/" + recordFile))
|
||||
return
|
||||
|
||||
Loading…
Reference in New Issue
Block a user