mirror of
https://github.com/woodchen-ink/Q58Bot.git
synced 2025-07-18 13:52:07 +08:00
fix bugs
This commit is contained in:
parent
a4c48cb298
commit
eb111a34da
@ -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)))
|
||||
}
|
||||
} else {
|
||||
bot.Send(tgbotapi.NewMessage(message.Chat.ID, fmt.Sprintf("关键词 '%s' 已存在。", keyword)))
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user