discordBots/cmd/dealsbot/main.go

130 lines
3.3 KiB
Go
Raw Normal View History

2023-01-26 00:28:18 +00:00
package main
2023-03-04 11:54:08 +00:00
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"
2023-03-06 19:53:07 +00:00
"grow.rievo.dev/discordBots/cmd/dealsbot/api"
"grow.rievo.dev/discordBots/cmd/dealsbot/repository"
2023-11-05 16:03:52 +00:00
"log/slog"
2023-03-04 11:54:08 +00:00
"os"
"os/signal"
"reflect"
"syscall"
"time"
)
var (
webhookID = snowflake.GetEnv("webhook_id")
webhookToken = os.Getenv("webhook_token")
)
2023-11-05 16:03:52 +00:00
var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
2023-04-22 23:05:35 +00:00
var release string
2023-01-26 00:28:18 +00:00
func main() {
// query different sources store to db
2023-03-04 11:54:08 +00:00
// try to incorporate operagx apiUrl:
// - https://gx-proxy.operacdn.com/content/free-games?_limit=300&_sort=order%3AASC
2023-01-26 00:28:18 +00:00
// send messages to discord
2023-03-04 11:54:08 +00:00
// ideas:
// - https://github.com/TheLovinator1/discord-free-game-notifier
// - https://gg.deals/news/free-gog-games/
// - origin
// - check ubisoft works
2023-11-05 16:03:52 +00:00
logger.Info("starting dealsbot...", slog.String("disgo version", disgo.Version))
2023-03-04 11:54:08 +00:00
client := webhook.New(webhookID, webhookToken)
defer client.Close(context.TODO())
2023-11-05 16:03:52 +00:00
repo := repository.InitDb(logger)
2023-03-04 11:54:08 +00:00
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:
2023-03-06 19:53:07 +00:00
var apis []api.Api
2023-11-05 16:03:52 +00:00
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()
2023-03-04 11:54:08 +00:00
if err != nil {
2023-11-05 16:03:52 +00:00
logger.Error("failed loading api", slog.Any("error", err))
2023-03-04 11:54:08 +00:00
}
}
2023-03-06 19:53:07 +00:00
var deals []api.Deal
for _, a := range apis {
apiDeals := a.Get()
2023-03-04 11:54:08 +00:00
deals = append(deals, apiDeals...)
}
for _, deal := range deals {
retrievedDeal, _ := repo.GetValue(deal.Id)
if deal.Id == retrievedDeal.Id {
2023-11-05 16:03:52 +00:00
logger.Debug("deal is already published", slog.String("deal", deal.Id))
2023-03-04 11:54:08 +00:00
} else if reflect.DeepEqual(deal, retrievedDeal) {
2023-11-05 16:03:52 +00:00
logger.Error("deal is published but not equal", slog.String("deal", deal.Id))
2023-03-04 11:54:08 +00:00
} else {
2023-11-05 16:03:52 +00:00
logger.Info("deal is new and will be published", slog.String("deal", deal.Id))
2023-03-04 11:54:08 +00:00
go sendWebhook(client, deal)
err := repo.SetValue(deal)
if err != nil {
2023-11-05 16:03:52 +00:00
logger.Error("failed saving deal", slog.Any("error", err))
2023-03-04 11:54:08 +00:00
}
}
}
case <-tickerGC.C:
err := repo.RunGC()
if err != nil && !errors.Is(err, badger.ErrNoRewrite) {
2023-11-05 16:03:52 +00:00
logger.Error("GC failed", slog.Any("error", err))
2023-03-04 11:54:08 +00:00
} else {
2023-11-05 16:03:52 +00:00
logger.Debug("GC successful")
2023-03-04 11:54:08 +00:00
}
case <-quit:
ticker.Stop()
tickerGC.Stop()
return
}
}
}()
2023-11-05 16:03:52 +00:00
logger.Info("dealsbot is now running. Press CTRL-C to exit.", slog.String("version", release))
2023-04-22 23:23:52 +00:00
2023-03-04 11:54:08 +00:00
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-s
}
2023-03-06 19:53:07 +00:00
func sendWebhook(client webhook.Client, deal api.Deal) {
2023-03-04 11:54:08 +00:00
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 {
2023-11-05 16:03:52 +00:00
logger.Error("sending message failed", slog.Any("error", err))
2023-03-04 11:54:08 +00:00
}
2023-01-26 00:28:18 +00:00
}