-
Notifications
You must be signed in to change notification settings - Fork 812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Registering slash commands #1518
Comments
It shouldn't be required every time you start up your bot -- I am pretty sure this is just the command piece that shows up when you type the They also cover a command "reloading" pattern here. Let me know if this helps, I ran into similar issues but have been faring well so far off of this advice ^ :) |
I use this piece of code now right when i startup my bot: func syncCommands(s *discordgo.Session, guildID string, desiredCommandList []*discordgo.ApplicationCommand) {
existingCommands, err := s.ApplicationCommands(s.State.User.ID, guildID)
if err != nil {
log.Fatalf("Failed to fetch commands for guild %s: %v", guildID, err)
return
}
desiredMap := make(map[string]*discordgo.ApplicationCommand)
for _, cmd := range desiredCommandList {
desiredMap[cmd.Name] = cmd
}
existingMap := make(map[string]*discordgo.ApplicationCommand)
for _, cmd := range existingCommands {
existingMap[cmd.Name] = cmd
}
// Delete commands not in the desired list
for _, cmd := range existingCommands {
if _, found := desiredMap[cmd.Name]; !found {
err := s.ApplicationCommandDelete(s.State.User.ID, guildID, cmd.ID)
if err != nil {
log.Printf("Failed to delete command %s (%s) in guild %s: %v", cmd.Name, cmd.ID, guildID, err)
} else {
log.Printf("Successfully deleted command %s (%s) in guild %s", cmd.Name, cmd.ID, guildID)
}
}
}
// Create or update existing commands
for _, cmd := range desiredCommandList {
if existingCmd, found := existingMap[cmd.Name]; found {
// Edit existing command
_, err := s.ApplicationCommandEdit(s.State.User.ID, guildID, existingCmd.ID, cmd)
if err != nil {
log.Printf("Failed to edit command %s (%s) in guild %s: %v", cmd.Name, cmd.ID, guildID, err)
} else {
log.Printf("Successfully edited command %s (%s) in guild %s", cmd.Name, cmd.ID, guildID)
}
} else {
// Create new command
_, err := s.ApplicationCommandCreate(s.State.User.ID, guildID, cmd)
if err != nil {
log.Printf("Failed to create command %s in guild %s: %v", cmd.Name, guildID, err)
} else {
log.Printf("Successfully created command %s in guild %s", cmd.Name, guildID)
}
}
}
} That should delete commands not in the list, update existing ones, or create new ones if they do not yet exist. That makes sure the command IDs do not change. (I think i could use Not sure if bulk overwrite changes the ID, my guess would be no. I am not aware that my bot did hit any rate lmit this way, but then i only have a couple commands. |
Hey all,
I built a discord bot using this package and have a quick question about the best practice for registering application commands. Should I register the slash commands whenever the bot starts with ApplicationCommandCreate, ApplicationCommandBulkOverwrite, or follow some other pattern. I don’t want to rate limit the bot or tastefully re register the application commands if it is not needed. Does anyone have a good approach they suggest for registering slash commands?
currently, I register a single slash command called “bootstrap” that has support for registering all the slash commands. I use this command to bootstrap the server with the slash commands and when I need to update the slash commands if they changed. I’m not sure if this is the right approach though. I would prefer to have granular control on registering the application commands and decide if it is better to use create application command, bulk overwrite, or some other means. I really like the approach of having a single bootstrap command that handles registering slash commands and other bot services, but I’m not sure if I should be using the create, bulk overwrite or some other API when registering the commands.
The text was updated successfully, but these errors were encountered: