Skip to content

Commit

Permalink
Check if the bot is still in the server
Browse files Browse the repository at this point in the history
  • Loading branch information
rxdn committed Aug 8, 2024
1 parent 49b05ba commit afaabaf
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 9 deletions.
11 changes: 10 additions & 1 deletion cmd/cleanupdaemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"github.com/TicketsBot/database"
"github.com/getsentry/sentry-go"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/rxdn/gdl/rest/request"
"go.uber.org/zap"
"net/http"
"time"
)

Expand Down Expand Up @@ -61,7 +63,14 @@ func main() {

logger.Debug("Built archiver client")

daemon := daemon.NewDaemon(logger, &client, db)
request.RegisterPreRequestHook(func(token string, req *http.Request) {
if len(conf.DiscordProxyUrl) > 0 {
req.URL.Scheme = "http"
req.URL.Host = conf.DiscordProxyUrl
}
})

daemon := daemon.NewDaemon(logger, conf, &client, db)
daemon.Run()

if !conf.OneShot {
Expand Down
4 changes: 3 additions & 1 deletion envvars.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
- DB_URI
- LOG_ARCHIVER_URI
- ONESHOT
- PRODUCTION_MODE
- PRODUCTION_MODE
- MAIN_BOT_TOKEN
- DISCORD_PROXY_URL
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ require (
github.com/getsentry/sentry-go v0.22.0 // indirect
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 // indirect
github.com/jackc/pgx/v4 v4.6.0
github.com/rxdn/gdl v0.0.0-20240708003854-64917d2277cf // indirect
go.uber.org/zap v1.24.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ github.com/rxdn/gdl v0.0.0-20200925114730-3a808c6d330e/go.mod h1:2gPBB++1s9Zh11A
github.com/rxdn/gdl v0.0.0-20211030160619-a8772c268ca4/go.mod h1:rENs8TxMsoYSJRssegNS/+fy18NCI9EUdCJX8R83PlY=
github.com/rxdn/gdl v0.0.0-20230622203838-cad65ada73f0 h1:Oe8RWW9dHSYUsDjRUzNXgY+JIqjzJiHe1yFWzQptl7w=
github.com/rxdn/gdl v0.0.0-20230622203838-cad65ada73f0/go.mod h1:HtxfLp4OaoPoDJHQ4JOx/QeLH2d40VgT3wNOf7ETsRE=
github.com/rxdn/gdl v0.0.0-20240708003854-64917d2277cf h1:TieDplNMS5jqxzHirSPwy2QLPCu11FB+fyrPc32Hmik=
github.com/rxdn/gdl v0.0.0-20240708003854-64917d2277cf/go.mod h1:HtxfLp4OaoPoDJHQ4JOx/QeLH2d40VgT3wNOf7ETsRE=
github.com/sanity-io/litter v1.5.5/go.mod h1:9gzJgR2i4ZpjZHsKvUXIRQVk7P+yM3e+jAF7bU2UI5U=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/schollz/closestmatch v2.1.0+incompatible/go.mod h1:RtP1ddjLong6gTkbtmuhtR2uUrrJOpYzYRvbcPAid+g=
Expand Down
12 changes: 7 additions & 5 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package config
import "github.com/caarlos0/env/v6"

type Config struct {
DatabaseUri string `env:"DB_URI"`
LogArchiverUri string `env:"LOG_ARCHIVER_URI"`
OneShot bool `env:"ONESHOT"`
ProductionMode bool `env:"PRODUCTION_MODE" envDefault:"false"`
SentryDsn string `env:"SENTRY_DSN"`
DatabaseUri string `env:"DB_URI"`
LogArchiverUri string `env:"LOG_ARCHIVER_URI"`
OneShot bool `env:"ONESHOT"`
ProductionMode bool `env:"PRODUCTION_MODE" envDefault:"false"`
SentryDsn string `env:"SENTRY_DSN"`
MainBotToken string `env:"MAIN_BOT_TOKEN"`
DiscordProxyUrl string `env:"DISCORD_PROXY_URL"`
}

func Parse() (conf Config) {
Expand Down
64 changes: 62 additions & 2 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package daemon

import (
"context"
"errors"
"github.com/TicketsBot/archiverclient"
"github.com/TicketsBot/cleanupdaemon/pkg/config"
"github.com/TicketsBot/database"
"github.com/rxdn/gdl/rest"
"github.com/rxdn/gdl/rest/request"
"go.uber.org/zap"
"log"
"math"
Expand All @@ -14,13 +18,15 @@ const BreakTime = time.Second

type Daemon struct {
logger *zap.Logger
config config.Config
client *archiverclient.ArchiverClient
database *database.Database
}

func NewDaemon(logger *zap.Logger, client *archiverclient.ArchiverClient, database *database.Database) *Daemon {
func NewDaemon(logger *zap.Logger, config config.Config, client *archiverclient.ArchiverClient, database *database.Database) *Daemon {
return &Daemon{
logger,
config,
client,
database,
}
Expand All @@ -36,9 +42,30 @@ func (d *Daemon) Run() {
}

for _, guildId := range guildIds {
logger := d.logger.With(zap.Uint64("guild", guildId))

// Add a 1s delay as we're making a REST request
time.Sleep(BreakTime)

inServer, err := d.isBotInServer(guildId)
if err != nil {
logger.Error("error while checking if bot is in server", zap.Error(err))
continue
}

if inServer {
logger.Warn("Bot is still in server, skipping purge")

if err := d.database.GuildLeaveTime.Delete(guildId); err != nil {
logger.Error("Error while deleting leave time", zap.Error(err))
}

continue
}

if d.purgeGuild(guildId) {
if err := d.database.GuildLeaveTime.Delete(guildId); err != nil {
d.logger.Error("error while deleting leave times", zap.Error(err))
logger.Error("error while deleting leave times", zap.Error(err))
}
}
}
Expand Down Expand Up @@ -123,3 +150,36 @@ func (d *Daemon) purgeGuild(guildId uint64) bool {
}
}
}

func (d *Daemon) isBotInServer(guildId uint64) (bool, error) {
botId, ok, err := d.database.WhitelabelGuilds.GetBotByGuild(guildId)
if err != nil {
return false, err
}

var token string
if ok {
bot, err := d.database.Whitelabel.GetByBotId(botId)
if err != nil {
return false, err
}

token = bot.Token
} else {
token = d.config.MainBotToken
}

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

if _, err := rest.GetGuild(ctx, token, nil, guildId); err != nil {
var restError request.RestError
if errors.As(err, &restError) && (restError.StatusCode == 403 || restError.StatusCode == 404) {
return false, nil
} else {
return false, err
}
}

return true, nil
}

0 comments on commit afaabaf

Please sign in to comment.