From 9cbcc3227f8025ce47facc352b4da5cfa9a18233 Mon Sep 17 00:00:00 2001 From: lixiangwuxian Date: Sun, 20 Oct 2024 03:25:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8=E6=AD=A3?= =?UTF-8?q?=E5=88=99=E5=88=86=E5=89=B2=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/xibao/xibao.go | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/handler/xibao/xibao.go b/handler/xibao/xibao.go index 3ecec18..14bcc50 100644 --- a/handler/xibao/xibao.go +++ b/handler/xibao/xibao.go @@ -2,7 +2,7 @@ package xibao import ( "fmt" - "strings" + "regexp" "git.lxtend.com/qqbot/constants" "git.lxtend.com/qqbot/handler" @@ -21,7 +21,17 @@ func init() { func xiBao(msg model.Message) (reply model.Reply) { fileName := uuid.New().String() filePath := "./tmp/" + fileName + ".png" - xibao.GenerateCongratulationImage(strings.Split(msg.RawMsg, " ")[1], "./resource/xibao_background.png", filePath, true) + + re := regexp.MustCompile(`\s+`) + tokens := re.Split(msg.RawMsg, 2) + if len(tokens) < 2 { + return model.Reply{ + ReplyMsg: "参数不足, 请使用\"喜报 [内容]\"生成喜报图片", + ReferOriginMsg: true, + FromMsg: msg, + } + } + 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"), ReferOriginMsg: true, @@ -32,7 +42,16 @@ func xiBao(msg model.Message) (reply model.Reply) { func beiBao(msg model.Message) (reply model.Reply) { fileName := uuid.New().String() filePath := "./tmp/" + fileName + ".png" - xibao.GenerateCongratulationImage(strings.Split(msg.RawMsg, " ")[1], "./resource/beibao_background.png", filePath, false) + re := regexp.MustCompile(`\s+`) + tokens := re.Split(msg.RawMsg, 2) + if len(tokens) < 2 { + return model.Reply{ + ReplyMsg: "参数不足, 请使用\"悲报 [内容]\"生成悲报图片", + ReferOriginMsg: true, + FromMsg: msg, + } + } + 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"), ReferOriginMsg: true,