package main import ( "context" "errors" "fmt" "github.com/dgraph-io/badger/v4" "github.com/disgoorg/disgo" "github.com/disgoorg/disgo/discord" "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" "os" "os/signal" "reflect" "syscall" "time" ) var ( webhookID = snowflake.GetEnv("webhook_id") webhookToken = os.Getenv("webhook_token") ) // sentry var release string func main() { // query different sources store to db // try to incorporate operagx apiUrl: // - https://gx-proxy.operacdn.com/content/free-games?_limit=300&_sort=order%3AASC // send messages to discord // ideas: // - https://github.com/TheLovinator1/discord-free-game-notifier // - https://gg.deals/news/free-gog-games/ // - 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) client := webhook.New(webhookID, webhookToken) defer client.Close(context.TODO()) repo := repository.InitDb() defer repo.Close() ticker := time.NewTicker(10 * time.Minute) tickerGC := time.NewTicker(15 * time.Minute) quit := make(chan struct{}) go func() { for { select { case <-ticker.C: var apis []api.Api apis = append(apis, api.NewUbsioftApi(), api.NewEpicApi(), api.NewSteamApi(), api.NewGogApi(), api.NewHumbleBundleApi()) 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) } } var deals []api.Deal for _, a := range apis { apiDeals := a.Get() deals = append(deals, apiDeals...) } for _, deal := range deals { retrievedDeal, _ := repo.GetValue(deal.Id) if deal.Id == retrievedDeal.Id { log.Printf("DEBUG: %v is already published", deal.Id) } else if reflect.DeepEqual(deal, retrievedDeal) { log.Printf("ERROR: %v is published but not equal", deal.Id) } else { log.Printf("INFO: %v is new and will be published", deal.Id) go sendWebhook(client, deal) err := repo.SetValue(deal) if err != nil { log.Printf("ERROR: %v", err) } } } case <-tickerGC.C: err := repo.RunGC() if err != nil && !errors.Is(err, badger.ErrNoRewrite) { log.Printf("ERROR: GC: %v", err) } else { log.Printf("DEBUG: GC successful") } case <-quit: ticker.Stop() tickerGC.Stop() return } } }() 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") }) s := make(chan os.Signal, 1) signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) <-s } func sendWebhook(client webhook.Client, deal api.Deal) { var status string status = fmt.Sprintf("currently free: %v\n", deal.Url) if _, err := client.CreateMessage(discord.NewWebhookMessageCreateBuilder(). SetContent(status).Build(), rest.WithDelay(2*time.Second), ); err != nil { log.Printf("ERROR: sending message %v", err.Error()) } }