Add shutdown and panic notifications and admin only commands
This commit is contained in:
37
main.go
37
main.go
@ -1,8 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"nignoggobot/commands"
|
||||
|
||||
@ -11,7 +14,41 @@ import (
|
||||
_ "nignoggobot/commands" // ensure init() is called
|
||||
)
|
||||
|
||||
var (
|
||||
devChannelID int64 = -1001327903329
|
||||
AdminIDs = []int64{126131628}
|
||||
)
|
||||
|
||||
func notifyShutdown(reason string) {
|
||||
token := os.Getenv("TELEGRAM_TOKEN")
|
||||
bot, err := tgbotapi.NewBotAPI(token)
|
||||
if err != nil {
|
||||
log.Println("Failed to create bot for shutdown notification:", err)
|
||||
return
|
||||
}
|
||||
msg := tgbotapi.NewMessage(devChannelID, "Bot shutting down/crashed: "+reason)
|
||||
bot.Send(msg)
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Signal handling for SIGINT/SIGTERM
|
||||
sigs := make(chan os.Signal, 1)
|
||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||
go func() {
|
||||
sig := <-sigs
|
||||
notifyShutdown(fmt.Sprintf("Received signal: %v", sig))
|
||||
os.Exit(0)
|
||||
}()
|
||||
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
notifyShutdown("panic: " + fmt.Sprint(r))
|
||||
panic(r) // re-throw after notifying
|
||||
} else {
|
||||
notifyShutdown("normal shutdown")
|
||||
}
|
||||
}()
|
||||
|
||||
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_TOKEN"))
|
||||
log.Println(os.Getenv("TELEGRAM_TOKEN"))
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user