Skip to content
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

Open
christiankaseburg opened this issue Apr 11, 2024 · 2 comments
Open

Registering slash commands #1518

christiankaseburg opened this issue Apr 11, 2024 · 2 comments

Comments

@christiankaseburg
Copy link

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.

@hazzardr
Copy link

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 / in the text box. Keep in mind that is separate from how your bot will be handling the event. The DiscordJS lib explains it that way at least.

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 ^ :)

@Sharrnah
Copy link

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 ApplicationCommandCreate also for updating)

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.

@Rpi-Dude Rpi-Dude mentioned this issue Aug 18, 2024
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants