package discord import ( r "math/rand" "net/http" "time" "github.com/rs/zerolog/log" "git.jojodev.com/Minecraft/go-serverapi" "git.jojodev.com/Minecraft/canopeas/database" ) type rateLimit struct { rate time.Duration next time.Time } func NewRateLimit(rate time.Duration) *rateLimit { return &rateLimit{ rate: rate, next: time.Now(), } } func (r *rateLimit) Try() bool { now := time.Now() if now.After(r.next) { r.next = now.Add(r.rate) return true } return false } var rand = r.New(r.NewSource(time.Now().Unix())) func random(list []string) string { size := len(list) if size == 0 { return "" } else if size == 1 { return list[0] } idx := rand.Intn(size) return list[idx] } type unbanSchedule struct { db *database.Database sapi *serverapi.Client } func (u *unbanSchedule) Run() { u.check() ticker := time.NewTicker(time.Minute * 5) for { <-ticker.C u.check() } } func (u *unbanSchedule) check() { log.Debug().Msg("Running unban schedule") now := time.Now() for _, record := range u.db.ListUnbans() { if now.After(record.Expiration) { log.Info().Msgf("Unbanning %s", record.Username) unban := serverapi.Unban{ Target: record.Username, } status, err := u.sapi.Unban(unban) if err != nil { log.Err(err).Msg("") } if status != http.StatusOK { log.Error().Msgf("ServerAPI returned status %d when trying to ban %s", status, record.Username) } if err := u.db.RemoveUnban(record.Username); err != nil { log.Error().Msgf("could not remove unban for %s in database", record.Username) } } } }