discordBots/cmd/domaincheckbot/main.go
2023-04-23 01:23:52 +02:00

129 lines
3.2 KiB
Go

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/domaincheckbot/config"
"grow.rievo.dev/discordBots/cmd/domaincheckbot/dns"
"grow.rievo.dev/discordBots/cmd/domaincheckbot/repository"
"log"
"os"
"os/signal"
"reflect"
"syscall"
"time"
)
var (
webhookID = snowflake.GetEnv("webhook_id")
webhookToken = os.Getenv("webhook_token")
)
// 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)
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:
for _, d := range config.Domains {
go checkDomain(0, d, repo, client)
}
case <-tickerGC.C:
err := repo.RunGC()
if err != nil && !errors.Is(err, badger.ErrNoRewrite) {
log.Printf("ERROR: with GC: %v", err)
} else {
log.Printf("DEBUG: GC successful")
}
case <-quit:
ticker.Stop()
tickerGC.Stop()
return
}
}
}()
log.Printf("INFO: domaincheckbot (%v) is now running. Press CTRL-C to exit.", release)
sentry.CaptureMessage("DEBUG: domaincheckbot started")
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-s
}
func checkDomain(counter int, d string, repo *repository.DomainRepository, client webhook.Client) {
domain := dns.CheckDomain(d)
retrievedDomain, _ := repo.GetValue(d)
if reflect.DeepEqual(domain, retrievedDomain) {
log.Printf("DEBUG: %v: did not change", d)
return
}
log.Printf("INFO: !%v: changed", d)
counter += 1
if counter >= 2 {
go sendWebhook(client, domain, retrievedDomain)
repo.SetValue(domain)
return
}
time.Sleep(1 * time.Minute)
checkDomain(counter, d, repo, client)
}
func sendWebhook(client webhook.Client, domain repository.Domain, oldDomain repository.Domain) {
var status string
status = fmt.Sprintf("```md\n# %v", domain.Name)
status = fmt.Sprintf("%v\n - %v", status, oldDomain.NS)
status = fmt.Sprintf("%v\n + %v", status, domain.NS)
status = fmt.Sprintf("%v```\n", status)
if _, err := client.CreateMessage(discord.NewWebhookMessageCreateBuilder().
SetContent(status).Build(),
rest.WithDelay(2*time.Second),
); err != nil {
log.Printf("ERROR: sending message %v", err.Error())
}
}