From eb111a34daec3a3be4d2765385f26775c0c9f58f Mon Sep 17 00:00:00 2001 From: wood chen Date: Wed, 18 Sep 2024 02:51:10 +0800 Subject: [PATCH] fix bugs --- core/link_filter.go | 4 +- service/guard.go | 92 +++++++++++++++++---------------------------- 2 files changed, 36 insertions(+), 60 deletions(-) diff --git a/core/link_filter.go b/core/link_filter.go index f041b29..c23f99c 100644 --- a/core/link_filter.go +++ b/core/link_filter.go @@ -193,9 +193,9 @@ func (lf *LinkFilter) HandleKeywordCommand(bot *tgbotapi.BotAPI, message *tgbota err = lf.AddKeyword(keyword) if err != nil { bot.Send(tgbotapi.NewMessage(message.Chat.ID, "添加关键词时发生错误。")) - return + } else { + bot.Send(tgbotapi.NewMessage(message.Chat.ID, fmt.Sprintf("关键词 '%s' 已添加。", keyword))) } - bot.Send(tgbotapi.NewMessage(message.Chat.ID, fmt.Sprintf("关键词 '%s' 已添加。", keyword))) } else { bot.Send(tgbotapi.NewMessage(message.Chat.ID, fmt.Sprintf("关键词 '%s' 已存在。", keyword))) } diff --git a/service/guard.go b/service/guard.go index 4ffe938..5a3d750 100644 --- a/service/guard.go +++ b/service/guard.go @@ -101,7 +101,40 @@ func processMessage(bot *tgbotapi.BotAPI, message *tgbotapi.Message, linkFilter } } -func messageHandler(bot *tgbotapi.BotAPI, update tgbotapi.Update, linkFilter *core.LinkFilter, rateLimiter *RateLimiter) { +func StartBot() error { + bot, err := tgbotapi.NewBotAPI(botToken) + if err != nil { + return fmt.Errorf("failed to create bot: %w", err) + } + + bot.Debug = debugMode + + log.Printf("Authorized on account %s", bot.Self.UserName) + + err = core.RegisterCommands(bot) + if err != nil { + return fmt.Errorf("error registering commands: %w", err) + } + + linkFilter, err := core.NewLinkFilter(dbFile) + if err != nil { + log.Fatalf("Failed to create LinkFilter: %v", err) + } + + rateLimiter := NewRateLimiter(10, time.Second) + + u := tgbotapi.NewUpdate(0) + u.Timeout = 60 + + updates := bot.GetUpdatesChan(u) + + for update := range updates { + go handleUpdate(bot, update, linkFilter, rateLimiter) + } + + return nil +} +func handleUpdate(bot *tgbotapi.BotAPI, update tgbotapi.Update, linkFilter *core.LinkFilter, rateLimiter *RateLimiter) { if update.Message == nil { return } @@ -131,63 +164,6 @@ func messageHandler(bot *tgbotapi.BotAPI, update tgbotapi.Update, linkFilter *co } } -func commandHandler(bot *tgbotapi.BotAPI, update tgbotapi.Update, linkFilter *core.LinkFilter) { - if update.Message == nil || update.Message.Chat.Type != "private" || update.Message.From.ID != adminID { - return - } - - linkFilter.LoadDataFromFile() - - command := update.Message.Command() - args := update.Message.CommandArguments() - - switch command { - case "add", "delete", "list", "deletecontaining": - linkFilter.HandleKeywordCommand(bot, update.Message, command, args) - case "addwhite", "delwhite", "listwhite": - linkFilter.HandleWhitelistCommand(bot, update.Message, command, args) - } - - if command == "add" || command == "delete" || command == "deletecontaining" || command == "list" || command == "addwhite" || command == "delwhite" || command == "listwhite" { - linkFilter.LoadDataFromFile() - } -} - -func StartBot() error { - bot, err := tgbotapi.NewBotAPI(botToken) - if err != nil { - return fmt.Errorf("failed to create bot: %w", err) - } - - bot.Debug = debugMode - - log.Printf("Authorized on account %s", bot.Self.UserName) - - err = core.RegisterCommands(bot) - if err != nil { - return fmt.Errorf("error registering commands: %w", err) - } - - linkFilter, err := core.NewLinkFilter(dbFile) - if err != nil { - log.Fatalf("Failed to create LinkFilter: %v", err) - } - - rateLimiter := NewRateLimiter(10, time.Second) - - u := tgbotapi.NewUpdate(0) - u.Timeout = 60 - - updates := bot.GetUpdatesChan(u) - - for update := range updates { - go messageHandler(bot, update, linkFilter, rateLimiter) - go commandHandler(bot, update, linkFilter) - } - - return nil // 如果 bot 正常退出,返回 nil -} - func RunGuard() { baseDelay := time.Second maxDelay := 5 * time.Minute