From 5d2b8f39d98f5a7583af2d73aa7ed71887cdb488 Mon Sep 17 00:00:00 2001 From: Seraphim Strub Date: Sun, 5 Nov 2023 16:03:52 +0000 Subject: [PATCH] remove sentry and move to slog --- cmd/dcimgproxy/main.go | 21 +++++----- cmd/dealsbot/api/epic.go | 8 ++-- cmd/dealsbot/api/gog.go | 5 ++- cmd/dealsbot/api/gogfront.go | 5 ++- cmd/dealsbot/api/humblebundle.go | 5 ++- cmd/dealsbot/api/steam.go | 5 ++- cmd/dealsbot/api/ubisoft.go | 5 ++- cmd/dealsbot/main.go | 57 ++++++++++----------------- cmd/dealsbot/repository/repository.go | 11 +++--- cmd/delcombot/main.go | 18 +++++---- cmd/domaincheckadd/main.go | 11 ++++-- cmd/domaincheckbot/main.go | 40 +++++-------------- cmd/funbot/main.go | 46 +++++++-------------- cmd/tempbot/main.go | 44 ++++++--------------- cmd/vcbot/main.go | 49 +++++++---------------- cmd/welcomebot/main.go | 24 +---------- go.mod | 4 +- go.sum | 8 ---- 18 files changed, 134 insertions(+), 232 deletions(-) diff --git a/cmd/dcimgproxy/main.go b/cmd/dcimgproxy/main.go index c8cad91..93652a2 100644 --- a/cmd/dcimgproxy/main.go +++ b/cmd/dcimgproxy/main.go @@ -4,10 +4,9 @@ import ( "github.com/disgoorg/disgo" "github.com/disgoorg/disgo/bot" "github.com/disgoorg/disgo/cache" - "github.com/disgoorg/log" "github.com/disgoorg/snowflake/v2" - "golang.org/x/exp/slog" "io" + "log/slog" "net/http" "os" "strings" @@ -18,21 +17,24 @@ var ( avatarCache map[snowflake.ID][]byte ) +var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil)) + func init() { avatarCache = make(map[snowflake.ID][]byte) } func main() { + + logger.Info("starting dealsbot...") + mux := http.NewServeMux() - client, err := disgo.New(token, bot.WithCacheConfigOpts( - cache.WithCaches( - cache.FlagsAll, - ), - ), + client, err := disgo.New(token, + bot.WithCacheConfigOpts( + cache.WithCaches(cache.FlagsAll)), ) if err != nil { - slog.Error(err.Error()) + logger.Error("error initializing disgo client", slog.Any("error", err)) return } rest := client.Rest() @@ -71,5 +73,6 @@ func main() { return }) - log.Fatal(http.ListenAndServe(":8080", mux)) + err = http.ListenAndServe(":8080", mux) + logger.Error("error from http listener", slog.Any("error", err)) } diff --git a/cmd/dealsbot/api/epic.go b/cmd/dealsbot/api/epic.go index 66133f6..5a1b985 100644 --- a/cmd/dealsbot/api/epic.go +++ b/cmd/dealsbot/api/epic.go @@ -4,7 +4,7 @@ import ( "encoding/json" "fmt" "io" - "log" + "log/slog" "net/http" "time" ) @@ -15,15 +15,17 @@ type EpicStruct struct { idPrefix string headers map[string]string deals DealsMap + logger *slog.Logger } -func NewEpicApi() EpicStruct { +func NewEpicApi(logger *slog.Logger) EpicStruct { epic := EpicStruct{ url: "https://store-site-backend-static-ipv4.ak.epicgames.com/freeGamesPromotions", baseUrl: "https://store.epicgames.com/p/", idPrefix: "epic-", headers: make(map[string]string), deals: make(map[string]Deal), + logger: logger, } epic.headers["Accept-Language"] = "en" epic.headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0" @@ -113,7 +115,7 @@ func (e EpicStruct) Load() error { productSlug = element.OfferMappings[0].PageSlug } if productSlug == "" { - log.Printf("ERROR: product slug not found for: %v", element.Title) + e.logger.Error("product slug not found", slog.String("title", element.Title)) continue } diff --git a/cmd/dealsbot/api/gog.go b/cmd/dealsbot/api/gog.go index 4fe634f..518ead9 100644 --- a/cmd/dealsbot/api/gog.go +++ b/cmd/dealsbot/api/gog.go @@ -3,6 +3,7 @@ package api import ( "fmt" "golang.org/x/net/html" + "log/slog" "net/http" "regexp" ) @@ -13,15 +14,17 @@ type GogStruct struct { idPrefix string headers map[string]string deals DealsMap + logger *slog.Logger } -func NewGogApi() GogStruct { +func NewGogApi(logger *slog.Logger) GogStruct { gog := GogStruct{ url: "https://www.gog.com/en/games?priceRange=0,0&discounted=true", baseUrl: "https://www.gog.com/game/", idPrefix: "gog-", headers: make(map[string]string), deals: make(map[string]Deal), + logger: logger, } gog.headers["Accept-Language"] = "en" gog.headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0" diff --git a/cmd/dealsbot/api/gogfront.go b/cmd/dealsbot/api/gogfront.go index 819c96a..52dd6ae 100644 --- a/cmd/dealsbot/api/gogfront.go +++ b/cmd/dealsbot/api/gogfront.go @@ -3,6 +3,7 @@ package api import ( "fmt" "golang.org/x/net/html" + "log/slog" "net/http" "regexp" ) @@ -13,15 +14,17 @@ type GogFrontStruct struct { idPrefix string headers map[string]string deals DealsMap + logger *slog.Logger } -func NewGogFrontApi() GogFrontStruct { +func NewGogFrontApi(logger *slog.Logger) GogFrontStruct { gog := GogFrontStruct{ url: "https://www.gog.com/", baseUrl: "https://www.gog.com/game/", idPrefix: "gog-", headers: make(map[string]string), deals: make(map[string]Deal), + logger: logger, } gog.headers["Accept-Language"] = "en" gog.headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0" diff --git a/cmd/dealsbot/api/humblebundle.go b/cmd/dealsbot/api/humblebundle.go index d5917f6..4aff02b 100644 --- a/cmd/dealsbot/api/humblebundle.go +++ b/cmd/dealsbot/api/humblebundle.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "golang.org/x/net/html" + "log/slog" "net/http" "regexp" ) @@ -14,15 +15,17 @@ type HumbleBundleStruct struct { idPrefix string headers map[string]string deals DealsMap + logger *slog.Logger } -func NewHumbleBundleApi() HumbleBundleStruct { +func NewHumbleBundleApi(logger *slog.Logger) HumbleBundleStruct { humbleBundle := HumbleBundleStruct{ url: "https://www.humblebundle.com/", baseUrl: "https://www.humblebundle.com/store/", idPrefix: "humblebundle-", headers: make(map[string]string), deals: make(map[string]Deal), + logger: logger, } humbleBundle.headers["Accept-Language"] = "en" humbleBundle.headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0" diff --git a/cmd/dealsbot/api/steam.go b/cmd/dealsbot/api/steam.go index 9956a21..8f6a025 100644 --- a/cmd/dealsbot/api/steam.go +++ b/cmd/dealsbot/api/steam.go @@ -5,6 +5,7 @@ import ( "fmt" "golang.org/x/net/html" "io" + "log/slog" "net/http" "regexp" ) @@ -16,9 +17,10 @@ type SteamStruct struct { idPrefix string headers map[string]string deals DealsMap + logger *slog.Logger } -func NewSteamApi() SteamStruct { +func NewSteamApi(logger *slog.Logger) SteamStruct { steam := SteamStruct{ url: "https://store.steampowered.com/search/results?force_infinite=1&maxprice=free&specials=1", baseUrl: "https://store.steampowered.com/app/", @@ -26,6 +28,7 @@ func NewSteamApi() SteamStruct { idPrefix: "steam-", headers: make(map[string]string), deals: make(map[string]Deal), + logger: logger, } steam.headers["Accept-Language"] = "en" steam.headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0" diff --git a/cmd/dealsbot/api/ubisoft.go b/cmd/dealsbot/api/ubisoft.go index 4b5a887..d7f3138 100644 --- a/cmd/dealsbot/api/ubisoft.go +++ b/cmd/dealsbot/api/ubisoft.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io" + "log/slog" "net/http" "regexp" ) @@ -14,14 +15,16 @@ type UbisoftStruct struct { idPrefix string headers map[string]string deals DealsMap + logger *slog.Logger } -func NewUbsioftApi() UbisoftStruct { +func NewUbsioftApi(logger *slog.Logger) UbisoftStruct { ubisoft := UbisoftStruct{ url: "https://free.ubisoft.com/configuration.js", idPrefix: "ubisoft-", headers: make(map[string]string), deals: make(map[string]Deal), + logger: logger, } ubisoft.headers["referer"] = "https://free.ubisoft.com/" ubisoft.headers["origin"] = "https://free.ubisoft.com" diff --git a/cmd/dealsbot/main.go b/cmd/dealsbot/main.go index 1e01b22..9fdf18e 100644 --- a/cmd/dealsbot/main.go +++ b/cmd/dealsbot/main.go @@ -10,10 +10,9 @@ import ( "github.com/disgoorg/disgo/rest" "github.com/disgoorg/disgo/webhook" "github.com/disgoorg/snowflake/v2" - "github.com/getsentry/sentry-go" "grow.rievo.dev/discordBots/cmd/dealsbot/api" "grow.rievo.dev/discordBots/cmd/dealsbot/repository" - "log" + "log/slog" "os" "os/signal" "reflect" @@ -26,7 +25,8 @@ var ( webhookToken = os.Getenv("webhook_token") ) -// sentry +var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil)) + var release string func main() { @@ -40,28 +40,12 @@ func main() { // - origin // - check ubisoft works - err := sentry.Init(sentry.ClientOptions{ - // Either set your DSN here or set the SENTRY_DSN environment variable. - //Dsn: "", - // Set TracesSampleRate to 1.0 to capture 100% - // of transactions for performance monitoring. - // We recommend adjusting this value in production, - TracesSampleRate: 1.0, - Release: release, - }) - if err != nil { - log.Fatalf("sentry.Init: %s", err) - } - // Flush buffered events before the program terminates. - defer sentry.Flush(2 * time.Second) - - log.Printf("INFO: starting dealsbot...") - log.Printf("INFO: disgo version: %v", disgo.Version) + logger.Info("starting dealsbot...", slog.String("disgo version", disgo.Version)) client := webhook.New(webhookID, webhookToken) defer client.Close(context.TODO()) - repo := repository.InitDb() + repo := repository.InitDb(logger) defer repo.Close() ticker := time.NewTicker(10 * time.Minute) @@ -73,12 +57,17 @@ func main() { select { case <-ticker.C: var apis []api.Api - apis = append(apis, api.NewUbsioftApi(), api.NewEpicApi(), api.NewSteamApi(), api.NewGogFrontApi(), api.NewGogApi(), api.NewHumbleBundleApi()) + apis = append(apis, + api.NewUbsioftApi(logger), + api.NewEpicApi(logger), + api.NewSteamApi(logger), + api.NewGogFrontApi(logger), + api.NewGogApi(logger), + api.NewHumbleBundleApi(logger)) for _, a := range apis { err := a.Load() if err != nil { - sentry.CaptureException(fmt.Errorf("ERROR: loading from api: %w", err)) - log.Printf("ERROR: %v", err) + logger.Error("failed loading api", slog.Any("error", err)) } } var deals []api.Deal @@ -91,15 +80,15 @@ func main() { retrievedDeal, _ := repo.GetValue(deal.Id) if deal.Id == retrievedDeal.Id { - log.Printf("DEBUG: %v is already published", deal.Id) + logger.Debug("deal is already published", slog.String("deal", deal.Id)) } else if reflect.DeepEqual(deal, retrievedDeal) { - log.Printf("ERROR: %v is published but not equal", deal.Id) + logger.Error("deal is published but not equal", slog.String("deal", deal.Id)) } else { - log.Printf("INFO: %v is new and will be published", deal.Id) + logger.Info("deal is new and will be published", slog.String("deal", deal.Id)) go sendWebhook(client, deal) err := repo.SetValue(deal) if err != nil { - log.Printf("ERROR: %v", err) + logger.Error("failed saving deal", slog.Any("error", err)) } } } @@ -107,9 +96,9 @@ func main() { case <-tickerGC.C: err := repo.RunGC() if err != nil && !errors.Is(err, badger.ErrNoRewrite) { - log.Printf("ERROR: GC: %v", err) + logger.Error("GC failed", slog.Any("error", err)) } else { - log.Printf("DEBUG: GC successful") + logger.Debug("GC successful") } case <-quit: ticker.Stop() @@ -119,11 +108,7 @@ func main() { } }() - log.Printf("INFO: dealsbot (%v) is now running. Press CTRL-C to exit.", release) - sentry.ConfigureScope(func(scope *sentry.Scope) { - scope.SetLevel(sentry.LevelDebug) - sentry.CaptureMessage("DEBUG: dealsbot started") - }) + logger.Info("dealsbot is now running. Press CTRL-C to exit.", slog.String("version", release)) s := make(chan os.Signal, 1) signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) @@ -139,6 +124,6 @@ func sendWebhook(client webhook.Client, deal api.Deal) { SetContent(status).Build(), rest.WithDelay(2*time.Second), ); err != nil { - log.Printf("ERROR: sending message %v", err.Error()) + logger.Error("sending message failed", slog.Any("error", err)) } } diff --git a/cmd/dealsbot/repository/repository.go b/cmd/dealsbot/repository/repository.go index 4c5845c..ee15807 100644 --- a/cmd/dealsbot/repository/repository.go +++ b/cmd/dealsbot/repository/repository.go @@ -4,7 +4,7 @@ import ( "encoding/json" "github.com/dgraph-io/badger/v4" "grow.rievo.dev/discordBots/cmd/dealsbot/api" - "log" + "log/slog" ) type Repository interface { @@ -16,17 +16,18 @@ type Repository interface { } type DealRepository struct { - db *badger.DB + db *badger.DB + logger *slog.Logger } -func InitDb() *DealRepository { +func InitDb(logger *slog.Logger) *DealRepository { opts := badger.DefaultOptions("./db") opts.Logger = nil db, err := badger.Open(opts) if err != nil { - log.Fatal(err) + logger.Error("error opening DB", slog.Any("error", err)) } - return &DealRepository{db} + return &DealRepository{db, logger} } func (d *DealRepository) Close() error { diff --git a/cmd/delcombot/main.go b/cmd/delcombot/main.go index 2f8c3a2..d6fbb57 100644 --- a/cmd/delcombot/main.go +++ b/cmd/delcombot/main.go @@ -7,10 +7,12 @@ import ( "github.com/disgoorg/disgo/cache" "github.com/disgoorg/disgo/discord" "github.com/disgoorg/disgo/gateway" - "log" + "log/slog" "os" ) +var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil)) + var ( token = os.Getenv("disgo_token") noCommands []discord.ApplicationCommandCreate @@ -18,8 +20,7 @@ var ( // this bot should in theory delete all registered commands for a certain bot func main() { - log.Printf("INFO: starting delcombot...") - log.Printf("INFO: disgo version: %v", disgo.Version) + logger.Info("starting delcombot...", slog.String("disgo version", disgo.Version)) // permissions: // intent: @@ -34,7 +35,7 @@ func main() { ), ) if err != nil { - log.Fatal("error while building disgo instance: ", err) + logger.Error("error while building disgo instance", slog.Any("error", err)) return } @@ -42,17 +43,18 @@ func main() { client.Caches().GuildsForEach(func(guild discord.Guild) { if _, err = client.Rest().SetGuildCommands(client.ApplicationID(), guild.ID, noCommands); err != nil { - log.Printf("INFO: error deleting guild commands from %v: %v", guild.Name, err) + logger.Info("error deleting guild commands", slog.String("guild name", guild.Name), slog.Any("error", err)) } }) if _, err = client.Rest().SetGlobalCommands(client.ApplicationID(), noCommands); err != nil { - log.Printf("INFO: error deleting global commands %v", err) + logger.Info("error deleting global commands", slog.Any("error", err)) } if err = client.OpenGateway(context.TODO()); err != nil { - log.Fatal("error while connecting to gateway: ", err) + logger.Error("error while connecting to gateway", slog.Any("error", err)) + return } - log.Printf("INFO: delcombot removed all guild and global commands") + logger.Info("delcombot removed all guild and global commands") } diff --git a/cmd/domaincheckadd/main.go b/cmd/domaincheckadd/main.go index 747e76f..6406194 100644 --- a/cmd/domaincheckadd/main.go +++ b/cmd/domaincheckadd/main.go @@ -5,14 +5,16 @@ import ( "encoding/json" "fmt" "grow.rievo.dev/discordBots/cmd/domaincheckbot/config" - "log" + "log/slog" "os" "sort" "strings" ) +var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil)) + func main() { - log.Println("add domains to domains.json") + logger.Info("add domains to domains.json") scanner := bufio.NewScanner(os.Stdin) @@ -40,7 +42,7 @@ func main() { newDomains = values } - log.Printf("domains to add: %v\n", newDomains) + logger.Info("domains to add", slog.Any("domains", newDomains)) fmt.Print("add to domains.json? [y|N]: ") if scanner.Scan() { @@ -48,7 +50,8 @@ func main() { if input == "y" || input == "Y" { err := storeDomains(newDomains) if err != nil { - log.Fatal("failed storing domains.json") + logger.Error("failed storing domains.json", slog.Any("error", err)) + return } fmt.Println("domains.json updated") } else { diff --git a/cmd/domaincheckbot/main.go b/cmd/domaincheckbot/main.go index c7c57c3..609af67 100644 --- a/cmd/domaincheckbot/main.go +++ b/cmd/domaincheckbot/main.go @@ -10,11 +10,10 @@ import ( "github.com/disgoorg/disgo/rest" "github.com/disgoorg/disgo/webhook" "github.com/disgoorg/snowflake/v2" - "github.com/getsentry/sentry-go" "grow.rievo.dev/discordBots/cmd/domaincheckbot/config" "grow.rievo.dev/discordBots/cmd/domaincheckbot/dns" "grow.rievo.dev/discordBots/cmd/domaincheckbot/repository" - "log" + "log/slog" "os" "os/signal" "reflect" @@ -27,30 +26,15 @@ var ( webhookToken = os.Getenv("webhook_token") ) +var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil)) + // TODO: clear db from domains removed from json -// sentry var release string func main() { - err := sentry.Init(sentry.ClientOptions{ - // Either set your DSN here or set the SENTRY_DSN environment variable. - //Dsn: "", - // Set TracesSampleRate to 1.0 to capture 100% - // of transactions for performance monitoring. - // We recommend adjusting this value in production, - TracesSampleRate: 1.0, - Release: release, - }) - if err != nil { - log.Fatalf("sentry.Init: %s", err) - } - // Flush buffered events before the program terminates. - defer sentry.Flush(2 * time.Second) - - log.Printf("INFO: starting domainCheck...") - log.Printf("INFO: disgo version: %v", disgo.Version) + logger.Info("starting domainCheck...", slog.String("disgo version", disgo.Version)) client := webhook.New(webhookID, webhookToken) defer client.Close(context.TODO()) @@ -73,9 +57,9 @@ func main() { case <-tickerGC.C: err := repo.RunGC() if err != nil && !errors.Is(err, badger.ErrNoRewrite) { - log.Printf("ERROR: with GC: %v", err) + logger.Error("GC failed", slog.Any("error", err)) } else { - log.Printf("DEBUG: GC successful") + logger.Debug("GC successful") } case <-quit: @@ -86,11 +70,7 @@ func main() { } }() - log.Printf("INFO: domaincheckbot (%v) is now running. Press CTRL-C to exit.", release) - sentry.ConfigureScope(func(scope *sentry.Scope) { - scope.SetLevel(sentry.LevelDebug) - sentry.CaptureMessage("DEBUG: domaincheckbot started") - }) + logger.Info("domaincheckbot is now running. Press CTRL-C to exit.", slog.String("version", release)) s := make(chan os.Signal, 1) signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) @@ -101,10 +81,10 @@ func checkDomain(counter int, d string, repo *repository.DomainRepository, clien domain := dns.CheckDomain(d) retrievedDomain, _ := repo.GetValue(d) if reflect.DeepEqual(domain, retrievedDomain) { - log.Printf("DEBUG: %v: did not change", d) + logger.Debug("domain did not change", slog.String("domain", d)) return } - log.Printf("INFO: !%v: changed", d) + logger.Info("domain changed changed", slog.String("domain", d)) counter += 1 if counter >= 2 { go sendWebhook(client, domain, retrievedDomain) @@ -127,6 +107,6 @@ func sendWebhook(client webhook.Client, domain repository.Domain, oldDomain repo SetContent(status).Build(), rest.WithDelay(2*time.Second), ); err != nil { - log.Printf("ERROR: sending message %v", err.Error()) + logger.Error("sending message failed", slog.Any("error", err)) } } diff --git a/cmd/funbot/main.go b/cmd/funbot/main.go index bfbb52e..c56482e 100644 --- a/cmd/funbot/main.go +++ b/cmd/funbot/main.go @@ -6,39 +6,22 @@ import ( "github.com/disgoorg/disgo/bot" "github.com/disgoorg/disgo/cache" "github.com/disgoorg/disgo/gateway" - "github.com/getsentry/sentry-go" "grow.rievo.dev/discordBots/cmd/funbot/command" "grow.rievo.dev/discordBots/cmd/funbot/config" - "log" + "log/slog" "os" "os/signal" "strings" "syscall" - "time" ) -// sentry +var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil)) + var release string func main() { - err := sentry.Init(sentry.ClientOptions{ - // Either set your DSN here or set the SENTRY_DSN environment variable. - //Dsn: "", - // Set TracesSampleRate to 1.0 to capture 100% - // of transactions for performance monitoring. - // We recommend adjusting this value in production, - TracesSampleRate: 1.0, - Release: release, - }) - if err != nil { - log.Fatalf("sentry.Init: %s", err) - } - // Flush buffered events before the program terminates. - defer sentry.Flush(2 * time.Second) - - log.Printf("INFO: starting funbot...") - log.Printf("INFO: disgo version: %v", disgo.Version) + logger.Info("starting funbot...", slog.String("disgo version", disgo.Version)) // permissions: // intent: @@ -55,7 +38,7 @@ func main() { bot.WithEventListenerFunc(command.Listener), ) if err != nil { - log.Fatal("error while building disgo instance: ", err) + logger.Error("error while building disgo instance", slog.Any("error", err)) return } @@ -68,31 +51,30 @@ func main() { if globalComands { if _, err = client.Rest().SetGlobalCommands(client.ApplicationID(), config.Commands); err != nil { - log.Fatal("error while registering commands: ", err) + logger.Error("error while registering commands", slog.Any("error", err)) + return } if config.RegisterGuildID != 0 { if _, err = client.Rest().SetGuildCommands(client.ApplicationID(), config.RegisterGuildID, config.NoCommands); err != nil { - log.Printf("INFO: error deleting guild commands %v", err) + logger.Info("error deleting guild commands", slog.Any("error", err)) } } } else { if _, err = client.Rest().SetGuildCommands(client.ApplicationID(), config.RegisterGuildID, config.Commands); err != nil { - log.Fatal("error while registering commands: ", err) + logger.Error("error while registering commands", slog.Any("error", err)) + return } if _, err = client.Rest().SetGlobalCommands(client.ApplicationID(), config.NoCommands); err != nil { - log.Printf("INFO: error deleting global commands %v", err) + logger.Info("error deleting global commands", slog.Any("error", err)) } } if err = client.OpenGateway(context.TODO()); err != nil { - log.Fatal("error while connecting to gateway: ", err) + logger.Error("error while connecting to gateway", slog.Any("error", err)) + return } - log.Printf("INFO: funbot (%v) is now running. Press CTRL-C to exit.", release) - sentry.ConfigureScope(func(scope *sentry.Scope) { - scope.SetLevel(sentry.LevelDebug) - sentry.CaptureMessage("DEBUG: funbot started") - }) + logger.Info("funbot is now running. Press CTRL-C to exit.", slog.String("version", release)) s := make(chan os.Signal, 1) signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) diff --git a/cmd/tempbot/main.go b/cmd/tempbot/main.go index b5d7630..1baa7f4 100644 --- a/cmd/tempbot/main.go +++ b/cmd/tempbot/main.go @@ -7,8 +7,7 @@ import ( "github.com/disgoorg/disgo/cache" "github.com/disgoorg/disgo/gateway" "github.com/disgoorg/snowflake/v2" - "github.com/getsentry/sentry-go" - "log" + "log/slog" "os" "os/signal" "syscall" @@ -21,28 +20,13 @@ var ( channelTempID snowflake.ID ) -// sentry +var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil)) + var release string func main() { - err := sentry.Init(sentry.ClientOptions{ - // Either set your DSN here or set the SENTRY_DSN environment variable. - //Dsn: "", - // Set TracesSampleRate to 1.0 to capture 100% - // of transactions for performance monitoring. - // We recommend adjusting this value in production, - TracesSampleRate: 1.0, - Release: release, - }) - if err != nil { - log.Fatalf("sentry.Init: %s", err) - } - // Flush buffered events before the program terminates. - defer sentry.Flush(2 * time.Second) - - log.Printf("INFO: starting tempbot...") - log.Printf("INFO: disgo version: %v", disgo.Version) + logger.Info("starting tempbot...", slog.String("disgo version", disgo.Version)) // permissions: Manage Messages // intent: @@ -58,14 +42,14 @@ func main() { ), ) if err != nil { - log.Fatal("error while building disgo instance: ", err) + logger.Error("error while building disgo instance", slog.Any("error", err)) return } defer client.Close(context.TODO()) if err = client.OpenGateway(context.TODO()); err != nil { - log.Fatal("error while connecting to gateway: ", err) + logger.Error("error while connecting to gateway", slog.Any("error", err)) } channels, err := client.Rest().GetGuildChannels(registerGuildID) @@ -76,7 +60,7 @@ func main() { } } if channelTempID == 0 { - log.Fatal("couldn't find needed channel") + logger.Error("couldn't find needed channel") } ticker := time.NewTicker(1 * time.Minute) @@ -87,7 +71,7 @@ func main() { case <-ticker.C: messages, err := client.Rest().GetMessages(channelTempID, 0, 0, 0, 100) if err != nil { - log.Printf("ERROR: getting messages: %v", err) + logger.Error("getting messages", slog.Any("error", err)) } var messageIDs []snowflake.ID for _, message := range messages { @@ -95,17 +79,17 @@ func main() { messageIDs = append(messageIDs, message.ID) } } - log.Printf("DEBUG: deleting messages: %v", len(messageIDs)) + logger.Debug("deleting messages", slog.Int("number", len(messageIDs))) if len(messageIDs) == 1 { err = client.Rest().DeleteMessage(channelTempID, messageIDs[0]) if err != nil { - log.Printf("ERROR: deleting messages: %v", err) + logger.Error("failed deleting messages", slog.Any("error", err)) } } if len(messageIDs) > 1 { err = client.Rest().BulkDeleteMessages(channelTempID, messageIDs) if err != nil { - log.Printf("ERROR: deleting messages: %v", err) + logger.Error("failed deleting messages", slog.Any("error", err)) } } case <-quit: @@ -115,11 +99,7 @@ func main() { } }() - log.Printf("INFO: tempbot is now running. Press CTRL-C to exit.") - sentry.ConfigureScope(func(scope *sentry.Scope) { - scope.SetLevel(sentry.LevelDebug) - sentry.CaptureMessage("DEBUG: tempbot started") - }) + logger.Info("tempbot is now running. Press CTRL-C to exit.", slog.String("version", release)) s := make(chan os.Signal, 1) signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) diff --git a/cmd/vcbot/main.go b/cmd/vcbot/main.go index cc97133..815858a 100644 --- a/cmd/vcbot/main.go +++ b/cmd/vcbot/main.go @@ -7,44 +7,25 @@ import ( "github.com/disgoorg/disgo/cache" "github.com/disgoorg/disgo/events" "github.com/disgoorg/disgo/gateway" - disgolog "github.com/disgoorg/log" - "github.com/getsentry/sentry-go" "grow.rievo.dev/discordBots/cmd/vcbot/config" "grow.rievo.dev/discordBots/cmd/vcbot/event" - "log" + "log/slog" "os" "os/signal" "syscall" - "time" ) -// sentry +var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil)) + var release string func main() { - disgolog.SetLevel(disgolog.LevelDebug) - err := sentry.Init(sentry.ClientOptions{ - // Either set your DSN here or set the SENTRY_DSN environment variable. - //Dsn: "", - // Set TracesSampleRate to 1.0 to capture 100% - // of transactions for performance monitoring. - // We recommend adjusting this value in production, - TracesSampleRate: 1.0, - Release: release, - }) - if err != nil { - log.Fatalf("sentry.Init: %s", err) - } - // Flush buffered events before the program terminates. - defer sentry.Flush(2 * time.Second) + logger.Info("starting vcbot...", slog.String("disgo version", disgo.Version)) - log.Printf("INFO: starting vcbot...") - log.Printf("INFO: disgo version: %v", disgo.Version) - - err = config.LoadAppleList() + err := config.LoadAppleList() if err != nil { - log.Fatal(err) + logger.Error("could not load apple list", slog.Any("error", err)) return } @@ -66,21 +47,22 @@ func main() { bot.WithEventListenerFunc(event.MoveEvent), bot.WithEventListenerFunc(event.LeaveEvent), bot.WithEventListenerFunc(func(event *events.Ready) { - log.Println("DEBUG: Connection Ready") + logger.Debug("Connection Ready") }), bot.WithEventListenerFunc(func(event *events.Resumed) { - log.Println("DEBUG: Connection Resumed") + logger.Debug("Connection Resumed") }), ) if err != nil { - log.Fatal("error while building vcbot instance: ", err) + logger.Error("error while building vcbot instance", slog.Any("error", err)) return } defer client.Close(context.TODO()) if err = client.OpenGateway(context.TODO()); err != nil { - log.Fatal("error while connecting to gateway: ", err) + logger.Error("error while connecting to gateway", slog.Any("error", err)) + return } channels, err := client.Rest().GetGuildChannels(config.RegisterGuildID) @@ -93,14 +75,11 @@ func main() { } } if config.ChannelVoiceGroupID == 0 { - log.Fatal("couldn't find needed channel") + logger.Error("couldn't find needed channel") + return } - log.Printf("ERROR: vcbot is now running. Press CTRL-C to exit.") - sentry.ConfigureScope(func(scope *sentry.Scope) { - scope.SetLevel(sentry.LevelDebug) - sentry.CaptureMessage("DEBUG: vcbot started") - }) + logger.Info("INFO: vcbot is now running. Press CTRL-C to exit.", slog.String("version", release)) s := make(chan os.Signal, 1) signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) diff --git a/cmd/welcomebot/main.go b/cmd/welcomebot/main.go index 0f633eb..1d22932 100644 --- a/cmd/welcomebot/main.go +++ b/cmd/welcomebot/main.go @@ -6,36 +6,18 @@ import ( "github.com/disgoorg/disgo/bot" "github.com/disgoorg/disgo/cache" "github.com/disgoorg/disgo/gateway" - "github.com/getsentry/sentry-go" "grow.rievo.dev/discordBots/cmd/welcomebot/config" "grow.rievo.dev/discordBots/cmd/welcomebot/event" "log" "os" "os/signal" "syscall" - "time" ) -// sentry var release string func main() { - err := sentry.Init(sentry.ClientOptions{ - // Either set your DSN here or set the SENTRY_DSN environment variable. - //Dsn: "", - // Set TracesSampleRate to 1.0 to capture 100% - // of transactions for performance monitoring. - // We recommend adjusting this value in production, - TracesSampleRate: 1.0, - Release: release, - }) - if err != nil { - log.Fatalf("sentry.Init: %s", err) - } - // Flush buffered events before the program terminates. - defer sentry.Flush(2 * time.Second) - log.Printf("INFO: starting welcomebot...") log.Printf("INFO: disgo version: %v", disgo.Version) @@ -94,11 +76,7 @@ func main() { log.Fatal("couldn't find needed role") } - log.Printf("INFO: welcomebot is now running. Press CTRL-C to exit.") - sentry.ConfigureScope(func(scope *sentry.Scope) { - scope.SetLevel(sentry.LevelDebug) - sentry.CaptureMessage("DEBUG: welcomebot started") - }) + log.Printf("INFO: welcomebot (%v) is now running. Press CTRL-C to exit.", release) s := make(chan os.Signal, 1) signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) diff --git a/go.mod b/go.mod index 73098c3..c13fe89 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,6 @@ require ( github.com/disgoorg/disgo v0.16.11 github.com/disgoorg/log v1.2.1 github.com/disgoorg/snowflake/v2 v2.0.1 - github.com/getsentry/sentry-go v0.25.0 golang.org/x/net v0.17.0 ) @@ -22,14 +21,15 @@ require ( github.com/golang/protobuf v1.5.3 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/google/flatbuffers v23.5.26+incompatible // indirect + github.com/google/go-cmp v0.5.9 // indirect github.com/gorilla/websocket v1.5.0 // indirect github.com/klauspost/compress v1.16.6 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b // indirect + github.com/stretchr/testify v1.8.2 // indirect go.opencensus.io v0.24.0 // indirect golang.org/x/crypto v0.14.0 // indirect golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b // indirect golang.org/x/sys v0.13.0 // indirect - golang.org/x/text v0.13.0 // indirect google.golang.org/protobuf v1.30.0 // indirect ) diff --git a/go.sum b/go.sum index 8c94a42..b71694b 100644 --- a/go.sum +++ b/go.sum @@ -30,10 +30,6 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/getsentry/sentry-go v0.25.0 h1:q6Eo+hS+yoJlTO3uu/azhQadsD8V+jQn2D8VvX1eOyI= -github.com/getsentry/sentry-go v0.25.0/go.mod h1:lc76E2QywIyW8WuBnwl8Lc4bkmQH4+w1gwTf25trprY= -github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA= -github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= @@ -75,8 +71,6 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/compress v1.16.6 h1:91SKEy4K37vkp255cJ8QesJhjyRO0hn9i9G0GoUwLsk= github.com/klauspost/compress v1.16.6/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= -github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4= -github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -136,8 +130,6 @@ golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= -golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=