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:
FernandoJVideiraandClaude Sonnet 5.5 committed 2026-10-23 01:47:24 +01:00
1 parent 279607183a
commit 1fbc38ca56
10 files changed
+503 -30

No files matched your search

+1 -1
View File
@@ -8,7 +8,7 @@ import (
)
const (
betaGuild = "1170934850157547600"
betaGuild = "700000000000000001"
otherGuild = "999000000000000001"
testerRol = "555000000000000001"
testerID = "777000000000000001"
+20 -1
View File
@@ -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
View File
@@ -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()
}
@@ -153,7 +153,7 @@ func TestGetBetaSettings(t *testing.T) {
t.Fatalf("role = %q, want empty", role)
}
_, err = db.ExecContext(ctx, `INSERT INTO beta_settings (id, guild_id) VALUES (TRUE, 1170934850157547600)`)
_, err = db.ExecContext(ctx, `INSERT INTO beta_settings (id, guild_id) VALUES (TRUE, 700000000000000001)`)
if err != nil {
t.Fatal(err)
}
@@ -161,8 +161,8 @@ func TestGetBetaSettings(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if g != "1170934850157547600" {
t.Fatalf("g = %q, want %q", g, "1170934850157547600")
if g != "700000000000000001" {
t.Fatalf("g = %q, want %q", g, "700000000000000001")
}
if role != "" {
t.Fatalf("role = %q, want empty", role)