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
+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