Prepare for open source: Apache-2.0 license, contributing guide, self-hosting guide, fix Close()
- LICENSE (Apache-2.0), NOTICE, CONTRIBUTING.md - docs/self-hosting.md and README sections for hosting and contributing - Close() no longer crashes: it only disconnects and leaves the slash commands registered, so they keep their IDs across restarts - Tests use a neutral server ID Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
1 parent
279607183a
commit
1fbc38ca56
10 files changed
+503
-30
No files matched your search
@@ -20,7 +20,7 @@ import (
|
||||
|
||||
const (
|
||||
appID = "100"
|
||||
betaServer = "1170934850157547600"
|
||||
betaServer = "700000000000000001"
|
||||
optedIn = "222"
|
||||
leftServer = "333"
|
||||
)
|
||||
@@ -219,3 +219,22 @@ func TestBetaCleanupIsRetriedAndGivesUpWhenTheBotIsGone(t *testing.T) {
|
||||
t.Error("a server the bot can't reach any more is forgotten")
|
||||
}
|
||||
}
|
||||
|
||||
// Close used to loop over a list that started with empty entries and read
|
||||
// their IDs, so shutting down crashed. It must just disconnect, and must not
|
||||
// touch the registered commands.
|
||||
func TestCloseDoesNotTouchCommands(t *testing.T) {
|
||||
d := &fakeDiscord{cmds: map[string][]*discordgo.ApplicationCommand{
|
||||
"1": {{ID: "7", Name: "ping"}},
|
||||
}, failures: map[string]int{}}
|
||||
b := newTestBot(t, d)
|
||||
b.GuildID = "1"
|
||||
b.Commands = []*discordgo.ApplicationCommand{{Name: "ping"}}
|
||||
|
||||
if err := b.Close(); err != nil {
|
||||
t.Fatalf("Close on a bot that never connected: %v", err)
|
||||
}
|
||||
if len(d.deleted) != 0 {
|
||||
t.Errorf("Close must not delete commands: %v", d.deleted)
|
||||
}
|
||||
}
|
||||
+19
-24
@@ -21,20 +21,19 @@ import (
|
||||
)
|
||||
|
||||
type Bot struct {
|
||||
Session *discordgo.Session
|
||||
AppID string
|
||||
GuildID string
|
||||
Commands []*discordgo.ApplicationCommand
|
||||
registeredCommands []*discordgo.ApplicationCommand
|
||||
Services *services.Services
|
||||
MusicRepo *musicrepo.Repo
|
||||
DBHost string
|
||||
LavalinkHost string
|
||||
LavalinkPass string
|
||||
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
|
||||
Session *discordgo.Session
|
||||
AppID string
|
||||
GuildID string
|
||||
Commands []*discordgo.ApplicationCommand
|
||||
Services *services.Services
|
||||
MusicRepo *musicrepo.Repo
|
||||
DBHost string
|
||||
LavalinkHost string
|
||||
LavalinkPass string
|
||||
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) {
|
||||
@@ -114,14 +113,12 @@ func (b *Bot) Start() error {
|
||||
}()
|
||||
}
|
||||
|
||||
b.registeredCommands = make([]*discordgo.ApplicationCommand, len(b.Commands))
|
||||
// Registering is an upsert by name: existing commands are updated in
|
||||
// place and keep their IDs (links like </help:ID> keep working).
|
||||
for _, cmd := range b.Commands {
|
||||
created, err := b.Session.ApplicationCommandCreate(b.AppID, b.GuildID, cmd)
|
||||
if err != nil {
|
||||
if _, err := b.Session.ApplicationCommandCreate(b.AppID, b.GuildID, cmd); err != nil {
|
||||
log.Printf("Cannot create '%v' command: %v", cmd.Name, err)
|
||||
continue
|
||||
}
|
||||
b.registeredCommands = append(b.registeredCommands, created)
|
||||
}
|
||||
|
||||
b.Session.AddHandler(commands.HandleInteraction)
|
||||
@@ -428,11 +425,9 @@ func (b *Bot) removeBetaCommands(guildID string) bool {
|
||||
return clean
|
||||
}
|
||||
|
||||
// Close disconnects from Discord. The slash commands are left registered on
|
||||
// purpose: deleting them on every shutdown would make them disappear during a
|
||||
// restart, and registering them again would give them new IDs.
|
||||
func (b *Bot) Close() error {
|
||||
for _, cmd := range b.registeredCommands {
|
||||
if err := b.Session.ApplicationCommandDelete(b.AppID, b.GuildID, cmd.ID); err != nil {
|
||||
log.Printf("cannot delete '%s' command: %v", cmd.Name, err)
|
||||
}
|
||||
}
|
||||
return b.Session.Close()
|
||||
}
|
||||
Reference in new issue
Block a user