From 800a60c6599b1f9ab638170763b8a4b93483ccea Mon Sep 17 00:00:00 2001 From: wood chen Date: Wed, 18 Sep 2024 02:10:42 +0800 Subject: [PATCH] fix bugs --- core/bot_commands.go | 9 +++++---- service/binance.go | 7 ++++++- service/guard.go | 22 ++++++++++++++++++++-- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/core/bot_commands.go b/core/bot_commands.go index a7f51ab..4b0052b 100644 --- a/core/bot_commands.go +++ b/core/bot_commands.go @@ -7,7 +7,7 @@ import ( tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" ) -func RegisterCommands(bot *tgbotapi.BotAPI, adminID int64) error { +func RegisterCommands(bot *tgbotapi.BotAPI) error { commands := []tgbotapi.BotCommand{ {Command: "add", Description: "添加新的关键词"}, {Command: "delete", Description: "删除现有的关键词"}, @@ -18,10 +18,11 @@ func RegisterCommands(bot *tgbotapi.BotAPI, adminID int64) error { {Command: "listwhite", Description: "列出白名单域名"}, } - scope := tgbotapi.NewBotCommandScopeChatAdministrators(adminID) - config := tgbotapi.NewSetMyCommands(commands...) - config.Scope = &scope // 注意这里使用 &scope 来获取指针 + + // 不设置 Scope,这将使命令对所有用户可见 + // config.Scope = nil + config.LanguageCode = "" // 空字符串表示默认语言 _, err := bot.Request(config) diff --git a/service/binance.go b/service/binance.go index 9e56d94..07569c2 100644 --- a/service/binance.go +++ b/service/binance.go @@ -26,7 +26,12 @@ func init() { var err error botToken = os.Getenv("BOT_TOKEN") chatID = mustParseInt64(os.Getenv("CHAT_ID")) - symbols = strings.Split(os.Getenv("SYMBOLS"), ",") + + symbolsRaw := strings.Split(os.Getenv("SYMBOLS"), ",") + symbols = make([]string, len(symbolsRaw)) + for i, s := range symbolsRaw { + symbols[i] = strings.ReplaceAll(s, "/", "") + } // 初始化 singaporeTZ singaporeTZ = time.FixedZone("Asia/Singapore", 8*60*60) // UTC+8 diff --git a/service/guard.go b/service/guard.go index a1dffc0..4ffe938 100644 --- a/service/guard.go +++ b/service/guard.go @@ -106,7 +106,25 @@ func messageHandler(bot *tgbotapi.BotAPI, update tgbotapi.Update, linkFilter *co return } - if update.Message.Chat.Type != "private" || update.Message.From.ID != adminID { + // 检查是否是管理员发送的私聊消息 + if update.Message.Chat.Type == "private" && update.Message.From.ID == adminID { + 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) + default: + msg := tgbotapi.NewMessage(update.Message.Chat.ID, "未知命令") + bot.Send(msg) + } + return + } + + // 处理非管理员消息或群组消息 + if update.Message.Chat.Type != "private" { if rateLimiter.Allow() { processMessage(bot, update.Message, linkFilter) } @@ -145,7 +163,7 @@ func StartBot() error { log.Printf("Authorized on account %s", bot.Self.UserName) - err = core.RegisterCommands(bot, 0) + err = core.RegisterCommands(bot) if err != nil { return fmt.Errorf("error registering commands: %w", err) }