Skip to content

Commit

Permalink
添加log并且在panic时自动recover
Browse files Browse the repository at this point in the history
  • Loading branch information
singer233 committed Apr 8, 2024
1 parent 46654c1 commit a6b6da6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/core/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,6 @@ func Serve() {
bot.TeleBot.NewCommandProcessor("reg", system.RegulationHandle)
bot.TeleBot.NewCommandProcessor("clear", system.ClearHandle)
bot.TeleBot.NewCommandProcessor("kill", system.KillHandle)
log.Println("Run pulling")
bot.TeleBot.Run(bot.Arknights.GetUpdatesChan(u), bot.Arknights)
}
14 changes: 13 additions & 1 deletion src/utils/telebot/bot.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package telebot

import (
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/spf13/viper"
"log"
Expand Down Expand Up @@ -87,6 +88,16 @@ func (b *Bot) addProcessor(command string, processor callbackFunction, funcMap m
}
funcMap[command] = processor
}
func recoverWarp(function callbackFunction) callbackFunction {
return func(msg tgbotapi.Update) error {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered in f", r)
}
}()
return function(msg)
}
}
func (b *Bot) selectFunction(msg tgbotapi.Update) callbackFunction {
// generic first
for _, k := range b.matchProcessorSlice {
Expand Down Expand Up @@ -168,9 +179,10 @@ func (b *Bot) Run(updates tgbotapi.UpdatesChannel, ark *tgbotapi.BotAPI) {
}
continue
}

process := b.selectFunction(msg)
if process != nil {
err := process(msg)
err := recoverWarp(process)(msg)
if err != nil {
log.Println("Plugin Error", err.Error())
}
Expand Down

0 comments on commit a6b6da6

Please sign in to comment.