mid day commit

This commit is contained in:
2025-12-09 10:54:36 +01:00
parent 06c17cc2e9
commit e188ceab79
4 changed files with 78 additions and 44 deletions

51
main.go
View File

@@ -26,13 +26,21 @@ func main() {
os.Exit(-1)
}
fmt.Printf("%v\n", cfg)
if err := commands.InitStorage(); err != nil {
slog.Error("Error initializing storage", "err", err)
os.Exit(-1)
}
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
AddSource: cfg.Log.AddSource,
Level: cfg.Log.Level,
})))
setupLogger(cfg.Log)
slog.Info(fmt.Sprintf("Starting %s...", cfg.Bot.Name))
b := relay.New(*cfg)
//
b := relay.New(*cfg)
h := handler.New()
h.Command("/ping", commands.PingHandler)
h.Command("/sync", commands.SyncHandler)
@@ -42,49 +50,32 @@ func main() {
os.Exit(-1)
}
defer func() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
b.Client.Close(ctx)
}()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer b.Client.Close(ctx)
defer cancel()
//
if err = handler.SyncCommands(b.Client, commands.Commands, cfg.Bot.DevGuilds); err != nil {
slog.Error("Failed to sync commands", slog.Any("err", err))
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err = b.Client.OpenGateway(ctx); err != nil {
slog.Error("Failed to open gateway", slog.Any("err", err))
os.Exit(-1)
}
slog.Info("Bot is running. Press CTRL-C to exit.")
slog.Info("Bot is running.")
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM)
<-s
slog.Info("Shutting down bot...")
}
func setupLogger(cfg relay.LogConfig) {
opts := &slog.HandlerOptions{
AddSource: cfg.AddSource,
Level: cfg.Level,
}
var sHandler slog.Handler
switch cfg.Format {
case "json":
sHandler = slog.NewJSONHandler(os.Stdout, opts)
case "text":
sHandler = slog.NewTextHandler(os.Stdout, opts)
default:
slog.Error("Unknown log format", slog.String("format", cfg.Format))
os.Exit(-1)
}
slog.SetDefault(slog.New(sHandler))
}
// $ git config user.name <user>
// $ git config user.email <email>
// $ go mod tidy; go mod download