Beta server and role come from the dashboard
The owner sets them on the admin page; the bot polls the shared beta_settings row (30s) and moves the beta commands if the server changes. BETA_* env vars are gone. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
1 parent
a364435bab
commit
1ee6280d91
7 files changed
+157
-24
No files matched your search
+57
-5
@@ -27,6 +27,7 @@ type Bot struct {
|
||||
DBHost string
|
||||
LavalinkHost string
|
||||
LavalinkPass string
|
||||
betaGuild string // where beta commands are registered right now
|
||||
}
|
||||
|
||||
func NewBot(token, appID, guildID, lavalinkHost, lavalinkPass string, cmds []*discordgo.ApplicationCommand, services *services.Services, musicRepo *musicrepo.Repo, dbHost string) (*Bot, error) {
|
||||
@@ -114,8 +115,6 @@ func (b *Bot) Start() error {
|
||||
b.registeredCommands = append(b.registeredCommands, created)
|
||||
}
|
||||
|
||||
b.registerBetaCommands()
|
||||
|
||||
b.Session.AddHandler(commands.HandleInteraction)
|
||||
|
||||
b.Session.AddHandler(func(s *discordgo.Session, c *discordgo.ChannelDelete) {
|
||||
@@ -203,12 +202,65 @@ func (b *Bot) Start() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// betaPollEvery is how often the bot looks for a change to the beta settings
|
||||
// made on the dashboard.
|
||||
const betaPollEvery = 30 * time.Second
|
||||
|
||||
// WatchBeta keeps the bot's idea of who the beta testers are in step with the
|
||||
// settings the owner saves on the dashboard. When the beta server changes, the
|
||||
// beta commands move with it. If the settings can't be read, the last known
|
||||
// ones stay in force (and before the first read, beta stays locked).
|
||||
func (b *Bot) WatchBeta(ctx context.Context, load func(context.Context) (guildID, roleID string, err error)) {
|
||||
sync := func() {
|
||||
guildID, roleID, err := load(ctx)
|
||||
if err != nil {
|
||||
log.Printf("beta: cannot read settings, keeping the last ones: %v", err)
|
||||
return
|
||||
}
|
||||
beta.Configure(guildID, roleID)
|
||||
if now := beta.GuildID(); now != b.betaGuild {
|
||||
b.registerBetaCommands(b.betaGuild, now)
|
||||
b.betaGuild = now
|
||||
}
|
||||
}
|
||||
sync()
|
||||
go func() {
|
||||
t := time.NewTicker(betaPollEvery)
|
||||
defer t.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-t.C:
|
||||
sync()
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// registerBetaCommands puts the beta commands in the beta server, and only
|
||||
// there, so nobody else sees them. Commands this bot left there that are no
|
||||
// longer beta (or no longer exist) are removed. When the beta server is also
|
||||
// the bot's main server, its regular commands are kept.
|
||||
func (b *Bot) registerBetaCommands() {
|
||||
guildID := beta.GuildID()
|
||||
// the bot's main server, its regular commands are kept. If the beta server
|
||||
// changed, the beta commands are taken out of the old one.
|
||||
func (b *Bot) registerBetaCommands(previous, guildID string) {
|
||||
if previous != "" && previous != guildID {
|
||||
isBeta := map[string]bool{}
|
||||
for _, cmd := range commands.BetaCommands {
|
||||
isBeta[cmd.Name] = true
|
||||
}
|
||||
if old, err := b.Session.ApplicationCommands(b.AppID, previous); err != nil {
|
||||
log.Printf("beta: cannot list commands in the previous server: %v", err)
|
||||
} else {
|
||||
for _, cmd := range old {
|
||||
if isBeta[cmd.Name] {
|
||||
if err := b.Session.ApplicationCommandDelete(b.AppID, previous, cmd.ID); err != nil {
|
||||
log.Printf("beta: cannot remove '%v' from the previous server: %v", cmd.Name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if guildID == "" {
|
||||
return
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user