2023-01-26 00:28:18 +00:00
|
|
|
package main
|
|
|
|
|
2023-03-04 11:54:08 +00:00
|
|
|
import (
|
|
|
|
"context"
|
2024-07-12 13:51:16 +00:00
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
2023-03-04 11:54:08 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/disgoorg/disgo"
|
|
|
|
"github.com/disgoorg/disgo/discord"
|
|
|
|
"github.com/disgoorg/disgo/rest"
|
|
|
|
"github.com/disgoorg/disgo/webhook"
|
|
|
|
"github.com/disgoorg/snowflake/v2"
|
2024-07-12 13:51:16 +00:00
|
|
|
"grow.rievo.dev/discordBots"
|
2023-03-06 19:53:07 +00:00
|
|
|
"grow.rievo.dev/discordBots/cmd/dealsbot/api"
|
2024-07-12 13:51:16 +00:00
|
|
|
"grow.rievo.dev/discordBots/pkg/db"
|
2023-11-05 16:03:52 +00:00
|
|
|
"log/slog"
|
2023-03-04 11:54:08 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"reflect"
|
2024-06-06 19:32:23 +00:00
|
|
|
"strings"
|
2023-03-04 11:54:08 +00:00
|
|
|
"syscall"
|
|
|
|
"time"
|
2024-07-12 13:51:16 +00:00
|
|
|
|
|
|
|
_ "modernc.org/sqlite"
|
2023-03-04 11:54:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2024-07-12 13:51:16 +00:00
|
|
|
// - need new ubisoft api if it even exists
|
|
|
|
// - add more supported types for steam (bundles,software,dlc)
|
2023-03-04 11:54:08 +00:00
|
|
|
|
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())
|
|
|
|
|
2024-07-12 13:51:16 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
con, err := sql.Open("sqlite", "file:db/deals.db?cache=shared")
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("error opening db", slog.Any("error", err))
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer con.Close()
|
|
|
|
|
|
|
|
// create tables
|
|
|
|
if _, err := con.ExecContext(ctx, discordBots.Schema); err != nil {
|
|
|
|
logger.Error("error creating db schema", slog.Any("error", err))
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
query := db.New(con)
|
|
|
|
|
|
|
|
ticker := time.NewTicker(1 * time.Minute)
|
2023-03-04 11:54:08 +00:00
|
|
|
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.NewEpicApi(logger),
|
|
|
|
api.NewSteamApi(logger),
|
|
|
|
api.NewGogFrontApi(logger),
|
|
|
|
api.NewGogApi(logger),
|
2024-06-21 15:15:03 +00:00
|
|
|
api.NewHumbleBundleApi(logger),
|
|
|
|
)
|
2023-04-22 22:51:02 +00:00
|
|
|
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
|
2023-04-22 22:51:02 +00:00
|
|
|
for _, a := range apis {
|
|
|
|
apiDeals := a.Get()
|
2023-03-04 11:54:08 +00:00
|
|
|
deals = append(deals, apiDeals...)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, deal := range deals {
|
2024-07-12 13:51:16 +00:00
|
|
|
retrievedDealJson, dbErr := query.GetItem(ctx, deal.Id)
|
|
|
|
retrievedDeal := api.Deal{}
|
|
|
|
if err := json.Unmarshal(retrievedDealJson.Data, &retrievedDeal); !errors.Is(dbErr, sql.ErrNoRows) && err != nil {
|
|
|
|
logger.Error("failed unmarshalling deal", slog.Any("error", err))
|
|
|
|
}
|
2023-03-04 11:54:08 +00:00
|
|
|
|
|
|
|
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)
|
2024-07-12 13:51:16 +00:00
|
|
|
dealJson, _ := json.Marshal(deal)
|
|
|
|
err = query.CreateItem(ctx, db.CreateItemParams{
|
|
|
|
ID: deal.Id,
|
|
|
|
Data: dealJson,
|
|
|
|
})
|
|
|
|
|
2023-03-04 11:54:08 +00:00
|
|
|
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 <-quit:
|
|
|
|
ticker.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) {
|
2024-05-29 21:35:12 +00:00
|
|
|
content := "currently free:"
|
|
|
|
message := discord.NewWebhookMessageCreateBuilder()
|
|
|
|
|
|
|
|
if deal.Image == "" {
|
|
|
|
message.SetContent(fmt.Sprintf("%v %v\n", content, deal.Url))
|
|
|
|
} else {
|
2024-06-06 19:32:23 +00:00
|
|
|
embed := discord.NewEmbedBuilder().
|
|
|
|
SetTitle(deal.Title).
|
|
|
|
SetURL(deal.Url).
|
|
|
|
SetImage(deal.Image)
|
|
|
|
var author, icon string
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(deal.Id, "epic-"):
|
|
|
|
author = "Epic Games"
|
|
|
|
icon = "https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Epic_Games_logo.svg/207px-Epic_Games_logo.svg.png"
|
|
|
|
case strings.HasPrefix(deal.Id, "gog-"):
|
|
|
|
author = "GOG.com"
|
|
|
|
icon = "https://upload.wikimedia.org/wikipedia/commons/thumb/2/2e/GOG.com_logo.svg/391px-GOG.com_logo.svg.png"
|
|
|
|
case strings.HasPrefix(deal.Id, "humblebundle-"):
|
|
|
|
author = "Humble Bundle"
|
|
|
|
icon = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/0c/Humble_Bundle_logo.svg/320px-Humble_Bundle_logo.svg.png"
|
|
|
|
case strings.HasPrefix(deal.Id, "steam-"):
|
|
|
|
author = "Steam"
|
|
|
|
icon = "https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Steam_icon_logo.svg/240px-Steam_icon_logo.svg.png"
|
|
|
|
case strings.HasPrefix(deal.Id, "ubisoft-"):
|
|
|
|
author = "Ubisoft"
|
|
|
|
icon = "https://upload.wikimedia.org/wikipedia/commons/thumb/7/78/Ubisoft_logo.svg/262px-Ubisoft_logo.svg.png"
|
|
|
|
}
|
|
|
|
embed.SetAuthor(author, "", icon)
|
|
|
|
message.SetContent(content).SetEmbeds(embed.Build())
|
2024-05-29 21:35:12 +00:00
|
|
|
}
|
2023-03-04 11:54:08 +00:00
|
|
|
|
2024-05-29 21:35:12 +00:00
|
|
|
if _, err := client.CreateMessage(message.Build(),
|
2023-03-04 11:54:08 +00:00
|
|
|
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
|
|
|
}
|