From 98d39b6b51b7ac7081e325e5a65f37e1679c92ce Mon Sep 17 00:00:00 2001 From: wood chen Date: Wed, 18 Sep 2024 15:44:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D(service/link=5Ffilter.go):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E9=93=BE=E6=8E=A5=E8=BF=87=E6=BB=A4?= =?UTF-8?q?=E5=99=A8=EF=BC=8C=E5=A2=9E=E5=BC=BA=E9=93=BE=E6=8E=A5=E5=8C=B9?= =?UTF-8?q?=E9=85=8D=E5=87=86=E7=A1=AE=E6=80=A7=E3=80=82=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?(service/prompt=5Freply.go):=20=E4=BC=98=E5=8C=96=E6=8F=90?= =?UTF-8?q?=E7=A4=BA=E8=AF=8D=E6=9C=8D=E5=8A=A1=EF=BC=8C=E5=A2=9E=E5=BC=BA?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E7=A8=B3=E5=AE=9A=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/link_filter.go | 4 ++-- service/prompt_reply.go | 28 ++++++++++++---------------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/service/link_filter.go b/service/link_filter.go index 171b9f6..52ce835 100644 --- a/service/link_filter.go +++ b/service/link_filter.go @@ -21,8 +21,8 @@ type LinkFilter struct { linkPattern *regexp.Regexp } -func NewLinkFilter(dbFile string) (*LinkFilter, error) { - db, err := core.NewDatabase(dbFile) +func NewLinkFilter() (*LinkFilter, error) { + db, err := core.NewDatabase() if err != nil { return nil, err } diff --git a/service/prompt_reply.go b/service/prompt_reply.go index e5220a8..18c83a7 100644 --- a/service/prompt_reply.go +++ b/service/prompt_reply.go @@ -4,28 +4,21 @@ import ( "fmt" "log" "strings" - "sync" "github.com/woodchen-ink/Q58Bot/core" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" ) -var ( - promptReplies = make(map[string]string) - promptMutex sync.RWMutex - db *core.Database -) +var db *core.Database -func InitPromptService(database *core.Database) error { - db = database - return loadPromptRepliesFromDB() -} - -func loadPromptRepliesFromDB() error { +func InitPromptService() error { var err error - promptReplies, err = db.GetAllPromptReplies() - return err + db, err = core.NewDatabase() + if err != nil { + return fmt.Errorf("failed to initialize database: %v", err) + } + return nil } func SetPromptReply(prompt, reply string) error { @@ -52,8 +45,11 @@ func GetPromptReply(message string) (string, bool) { } func ListPromptReplies() string { - promptMutex.RLock() - defer promptMutex.RUnlock() + promptReplies, err := db.GetAllPromptReplies() + if err != nil { + log.Printf("Error getting prompt replies: %v", err) + return "获取提示词回复时发生错误。" + } if len(promptReplies) == 0 { return "目前没有设置任何提示词回复。"