refactor: centralize temporary file path generation and update file references

This commit is contained in:
lixiangwuxian
2025-02-22 18:40:26 +08:00
parent 388cfdf377
commit 38940fa07f
7 changed files with 25 additions and 14 deletions

View File

@@ -8,6 +8,7 @@ import (
"git.lxtend.com/qqbot/handler"
"git.lxtend.com/qqbot/model"
"git.lxtend.com/qqbot/service/xibao"
"git.lxtend.com/qqbot/util"
"github.com/google/uuid"
)
@@ -20,7 +21,7 @@ func init() {
func xiBao(msg model.Message) (reply model.Reply) {
fileName := uuid.New().String()
filePath := "./tmp/" + fileName + ".png"
filePath := util.GenTempFilePath(fmt.Sprintf("%s.png", fileName))
re := regexp.MustCompile(`\s+`)
tokens := re.Split(msg.RawMsg, 2)
if len(tokens) < 2 {
@@ -33,7 +34,7 @@ func xiBao(msg model.Message) (reply model.Reply) {
}
xibao.GenerateCongratulationImage(tokens[1], "./resource/xibao_background.png", filePath, true)
return model.Reply{
ReplyMsg: fmt.Sprintf("[CQ:image,file=file:///root/qqbot/tmp/%s]", fileName+".png"),
ReplyMsg: fmt.Sprintf("[CQ:image,file=file:///root/qqbot/%s]", fileName+".png"),
ReferOriginMsg: true,
FromMsg: msg,
}
@@ -42,10 +43,10 @@ func xiBao(msg model.Message) (reply model.Reply) {
func xiBaoTemp(msg model.Message) (reply model.Reply, isTrigger bool) {
handler.UnRegisterLiveHandler(msg.GroupInfo.GroupId, msg.UserId)
fileName := uuid.New().String()
filePath := "./tmp/" + fileName + ".png"
filePath := util.GenTempFilePath(fmt.Sprintf("%s.png", fileName))
xibao.GenerateCongratulationImage(msg.RawMsg, "./resource/xibao_background.png", filePath, true)
return model.Reply{
ReplyMsg: fmt.Sprintf("[CQ:image,file=file:///root/qqbot/tmp/%s]", fileName+".png"),
ReplyMsg: fmt.Sprintf("[CQ:image,file=file:///root/qqbot/%s]", fileName+".png"),
ReferOriginMsg: true,
FromMsg: msg,
}, true
@@ -53,7 +54,7 @@ func xiBaoTemp(msg model.Message) (reply model.Reply, isTrigger bool) {
func beiBao(msg model.Message) (reply model.Reply) {
fileName := uuid.New().String()
filePath := "./tmp/" + fileName + ".png"
filePath := util.GenTempFilePath(fmt.Sprintf("%s.png", fileName))
re := regexp.MustCompile(`\s+`)
tokens := re.Split(msg.RawMsg, 2)
if len(tokens) < 2 {
@@ -66,7 +67,7 @@ func beiBao(msg model.Message) (reply model.Reply) {
}
xibao.GenerateCongratulationImage(tokens[1], "./resource/beibao_background.png", filePath, false)
return model.Reply{
ReplyMsg: fmt.Sprintf("[CQ:image,file=file:///root/qqbot/tmp/%s]", fileName+".png"),
ReplyMsg: fmt.Sprintf("[CQ:image,file=file:///root/qqbot/%s]", fileName+".png"),
ReferOriginMsg: true,
FromMsg: msg,
}
@@ -75,10 +76,10 @@ func beiBao(msg model.Message) (reply model.Reply) {
func beiBaoTemp(msg model.Message) (reply model.Reply, isTrigger bool) {
handler.UnRegisterLiveHandler(msg.GroupInfo.GroupId, msg.UserId)
fileName := uuid.New().String()
filePath := "./tmp/" + fileName + ".png"
filePath := util.GenTempFilePath(fmt.Sprintf("%s.png", fileName))
xibao.GenerateCongratulationImage(msg.RawMsg, "./resource/beibao_background.png", filePath, false)
return model.Reply{
ReplyMsg: fmt.Sprintf("[CQ:image,file=file:///root/qqbot/tmp/%s]", fileName+".png"),
ReplyMsg: fmt.Sprintf("[CQ:image,file=file:///root/qqbot/%s]", fileName+".png"),
ReferOriginMsg: true,
FromMsg: msg,
}, true