Beta: clean up servers that opted out while the bot was offline
The bot remembers where it registered beta commands (beta_registered) and removes them from servers that are no longer in the beta at the next start. Failed cleanups are retried; a server the bot has left is forgotten. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
1 parent
fcf4f3ccbd
commit
9839bba50e
9 files changed
+393
-30
No files matched your search
+86
-19
@@ -2,7 +2,9 @@ package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
"velox-bot/internal/beta"
|
||||
@@ -29,8 +31,10 @@ type Bot struct {
|
||||
DBHost string
|
||||
LavalinkHost string
|
||||
LavalinkPass string
|
||||
betaRegistered map[string]bool // servers where the beta commands are registered right now
|
||||
betaRoles sync.Map // "guild:user" -> cachedRoles
|
||||
betaRegistered map[string]bool // servers where this run registered the beta commands
|
||||
betaKnown map[string]bool // servers where they were registered, now or in an earlier run
|
||||
betaLoaded bool
|
||||
betaRoles sync.Map // "guild:user" -> cachedRoles
|
||||
}
|
||||
|
||||
func NewBot(token, appID, guildID, lavalinkHost, lavalinkPass string, cmds []*discordgo.ApplicationCommand, services *services.Services, musicRepo *musicrepo.Repo, dbHost string) (*Bot, error) {
|
||||
@@ -54,6 +58,7 @@ func NewBot(token, appID, guildID, lavalinkHost, lavalinkPass string, cmds []*di
|
||||
|
||||
return &Bot{
|
||||
betaRegistered: map[string]bool{},
|
||||
betaKnown: map[string]bool{},
|
||||
Session: session,
|
||||
AppID: appID,
|
||||
GuildID: guildID,
|
||||
@@ -210,21 +215,37 @@ func (b *Bot) Start() error {
|
||||
// made on the dashboard.
|
||||
const betaPollEvery = 30 * time.Second
|
||||
|
||||
// BetaStore is where the bot reads the beta settings the dashboard writes,
|
||||
// and remembers which servers it registered beta commands in.
|
||||
type BetaStore interface {
|
||||
GetBetaSettings(ctx context.Context) (guildID, roleID string, err error)
|
||||
ListBetaGuilds(ctx context.Context) ([]string, error)
|
||||
ListBetaRegistered(ctx context.Context) ([]string, error)
|
||||
SetBetaRegistered(ctx context.Context, guildID string, registered bool) error
|
||||
}
|
||||
|
||||
// WatchBeta keeps the bot's idea of who the beta testers are, and which
|
||||
// servers opted in, in step with the dashboard. When the list of servers
|
||||
// changes, the beta commands follow: they appear in servers that opted in and
|
||||
// are removed from servers that opted out. If the settings can't be read, the
|
||||
// last known ones stay in force (and before the first read, beta stays off).
|
||||
func (b *Bot) WatchBeta(ctx context.Context, load func(context.Context) (guildID, roleID string, servers []string, err error)) {
|
||||
// are removed from servers that opted out, also if that happened while the bot
|
||||
// was offline (it remembers where it registered them). If the settings can't
|
||||
// be read, the last known ones stay in force (and before the first read, beta
|
||||
// stays off).
|
||||
func (b *Bot) WatchBeta(ctx context.Context, store BetaStore) {
|
||||
beta.SetRolesLookup(b.betaMemberRoles)
|
||||
sync := func() {
|
||||
guildID, roleID, servers, err := load(ctx)
|
||||
guildID, roleID, err := store.GetBetaSettings(ctx)
|
||||
if err != nil {
|
||||
log.Printf("beta: cannot read settings, keeping the last ones: %v", err)
|
||||
return
|
||||
}
|
||||
servers, err := store.ListBetaGuilds(ctx)
|
||||
if err != nil {
|
||||
log.Printf("beta: cannot read the opted-in servers, keeping the last ones: %v", err)
|
||||
return
|
||||
}
|
||||
beta.Configure(guildID, roleID, servers)
|
||||
b.syncBetaCommands()
|
||||
b.syncBetaCommands(ctx, store)
|
||||
}
|
||||
sync()
|
||||
go func() {
|
||||
@@ -274,8 +295,22 @@ func (b *Bot) betaMemberRoles(guildID, userID string) ([]string, error) {
|
||||
|
||||
// syncBetaCommands makes the beta commands exist in exactly the servers where
|
||||
// beta features are on (the beta server and the ones that opted in), and
|
||||
// nowhere else. A server whose registration failed is retried next time.
|
||||
func (b *Bot) syncBetaCommands() {
|
||||
// nowhere else. The servers it registered them in are remembered in the
|
||||
// database, so a server that opted out while the bot was offline is cleaned up
|
||||
// at the next start. A step that failed is retried on the next round.
|
||||
func (b *Bot) syncBetaCommands(ctx context.Context, store BetaStore) {
|
||||
if !b.betaLoaded {
|
||||
known, err := store.ListBetaRegistered(ctx)
|
||||
if err != nil {
|
||||
log.Printf("beta: cannot read where beta commands were registered: %v", err)
|
||||
return
|
||||
}
|
||||
for _, id := range known {
|
||||
b.betaKnown[id] = true
|
||||
}
|
||||
b.betaLoaded = true
|
||||
}
|
||||
|
||||
want := map[string]bool{}
|
||||
if g := beta.GuildID(); g != "" {
|
||||
want[g] = true
|
||||
@@ -283,16 +318,30 @@ func (b *Bot) syncBetaCommands() {
|
||||
for _, id := range beta.Servers() {
|
||||
want[id] = true
|
||||
}
|
||||
for id := range b.betaRegistered {
|
||||
if !want[id] {
|
||||
b.removeBetaCommands(id)
|
||||
delete(b.betaRegistered, id)
|
||||
for id := range b.betaKnown {
|
||||
if want[id] {
|
||||
continue
|
||||
}
|
||||
if !b.removeBetaCommands(id) {
|
||||
continue
|
||||
}
|
||||
if err := store.SetBetaRegistered(ctx, id, false); err != nil {
|
||||
log.Printf("beta: cannot forget server %s: %v", id, err)
|
||||
continue
|
||||
}
|
||||
delete(b.betaKnown, id)
|
||||
delete(b.betaRegistered, id)
|
||||
}
|
||||
for id := range want {
|
||||
if !b.betaRegistered[id] && b.addBetaCommands(id) {
|
||||
b.betaRegistered[id] = true
|
||||
if b.betaRegistered[id] || !b.addBetaCommands(id) {
|
||||
continue
|
||||
}
|
||||
b.betaRegistered[id] = true
|
||||
if err := store.SetBetaRegistered(ctx, id, true); err != nil {
|
||||
log.Printf("beta: cannot remember server %s: %v", id, err)
|
||||
continue
|
||||
}
|
||||
b.betaKnown[id] = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -339,26 +388,44 @@ func (b *Bot) addBetaCommands(guildID string) bool {
|
||||
return ok
|
||||
}
|
||||
|
||||
// goneFromServer reports whether an error from Discord means the bot isn't in
|
||||
// that server any more (or it no longer exists), so there is nothing left to
|
||||
// clean up there.
|
||||
func goneFromServer(err error) bool {
|
||||
var rest *discordgo.RESTError
|
||||
if errors.As(err, &rest) && rest.Response != nil {
|
||||
return rest.Response.StatusCode == http.StatusForbidden || rest.Response.StatusCode == http.StatusNotFound
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// removeBetaCommands takes the beta commands out of a server that is no longer
|
||||
// part of the beta, and leaves everything else there alone.
|
||||
func (b *Bot) removeBetaCommands(guildID string) {
|
||||
// part of the beta, and leaves everything else there alone. It reports whether
|
||||
// the server is clean (or out of reach for good).
|
||||
func (b *Bot) removeBetaCommands(guildID string) bool {
|
||||
isBeta := map[string]bool{}
|
||||
for _, cmd := range commands.BetaCommands {
|
||||
isBeta[cmd.Name] = true
|
||||
}
|
||||
existing, err := b.Session.ApplicationCommands(b.AppID, guildID)
|
||||
if err != nil {
|
||||
if goneFromServer(err) {
|
||||
return true
|
||||
}
|
||||
log.Printf("beta: cannot list commands in %s: %v", guildID, err)
|
||||
return
|
||||
return false
|
||||
}
|
||||
clean := true
|
||||
for _, cmd := range existing {
|
||||
if !isBeta[cmd.Name] {
|
||||
continue
|
||||
}
|
||||
if err := b.Session.ApplicationCommandDelete(b.AppID, guildID, cmd.ID); err != nil {
|
||||
if err := b.Session.ApplicationCommandDelete(b.AppID, guildID, cmd.ID); err != nil && !goneFromServer(err) {
|
||||
log.Printf("beta: cannot remove '%v' from %s: %v", cmd.Name, guildID, err)
|
||||
clean = false
|
||||
}
|
||||
}
|
||||
return clean
|
||||
}
|
||||
|
||||
func (b *Bot) Close() error {
|
||||
|
||||
Reference in new issue
Block a user