From 3ab37d88f47539c4e08254bafb1586b4b3cef08f Mon Sep 17 00:00:00 2001 From: bdnugget Date: Wed, 28 May 2025 15:42:30 +0200 Subject: [PATCH] Add shutdown and panic notifications and admin only commands --- commands/admin.go | 12 ++++++++++++ commands/debug.go | 37 +++++++++++++++++++++++++++++++++++++ main.go | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 commands/admin.go create mode 100644 commands/debug.go diff --git a/commands/admin.go b/commands/admin.go new file mode 100644 index 0000000..f64f72d --- /dev/null +++ b/commands/admin.go @@ -0,0 +1,12 @@ +package commands + +var AdminIDs = []int64{126131628} + +func IsAdmin(userID int64) bool { + for _, id := range AdminIDs { + if id == userID { + return true + } + } + return false +} diff --git a/commands/debug.go b/commands/debug.go new file mode 100644 index 0000000..96c58dc --- /dev/null +++ b/commands/debug.go @@ -0,0 +1,37 @@ +package commands + +import ( + "fmt" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" +) + +type DebugCommand struct{} + +func (d DebugCommand) Name() string { + return "debug" +} + +func (d DebugCommand) Help() string { + return "Show debug info (admin only)" +} + +func (d DebugCommand) Execute(update tgbotapi.Update, bot *tgbotapi.BotAPI) { + user := update.Message.From + if user == nil || !IsAdmin(user.ID) { + msg := tgbotapi.NewMessage(update.Message.Chat.ID, "You are not authorized to use this command.") + bot.Send(msg) + return + } + info := fmt.Sprintf("UserID: %d\nChatID: %d\nText: %s", user.ID, update.Message.Chat.ID, update.Message.Text) + msg := tgbotapi.NewMessage(update.Message.Chat.ID, info) + bot.Send(msg) + + if update.Message.CommandArguments() == "panic" { + panic("test panic") + } +} + +func init() { + Register(DebugCommand{}) +} diff --git a/main.go b/main.go index 4b94a40..41c8047 100644 --- a/main.go +++ b/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 {