Use this file to discover all available pages before exploring further.
Slash commands are Discord’s modern way of handling bot commands. They provide autocomplete, validation, and a better user experience than traditional text commands.
var commands = []discord.ApplicationCommandCreate{ discord.SlashCommandCreate{ Name: "say", Description: "says what you say", Options: []discord.ApplicationCommandOption{ discord.ApplicationCommandOptionString{ Name: "message", Description: "What to say", Required: true, }, discord.ApplicationCommandOptionBool{ Name: "ephemeral", Description: "If the response should only be visible to you", Required: true, }, }, },}
2
Register the commands
Register your commands with Discord after creating your client:
// For guild-specific commands (faster, recommended for development)if _, err = client.Rest.SetGuildCommands(client.ApplicationID, guildID, commands); err != nil { slog.Error("error while registering commands", slog.Any("err", err))}// Or for global commands (slower, up to 1 hour to propagate)if _, err = client.Rest.SetGlobalCommands(client.ApplicationID, commands); err != nil { slog.Error("error while registering commands", slog.Any("err", err))}
Guild commands update instantly, while global commands can take up to an hour to propagate. Use guild commands during development.
3
Handle command interactions
Add an event listener to handle when users run your commands:
client, err := disgo.New(token, bot.WithDefaultGateway(), bot.WithEventListenerFunc(commandListener),)func commandListener(event *events.ApplicationCommandInteractionCreate) { data := event.SlashCommandInteractionData() if data.CommandName() == "say" { err := event.CreateMessage(discord.MessageCreate{ Content: data.String("message"), Flags: discord.MessageFlags( data.Bool("ephemeral"), ), }) if err != nil { slog.Error("error on sending response", slog.Any("err", err)) } }}
discord.SlashCommandCreate{ Name: "say", NameLocalizations: map[discord.Locale]string{ discord.LocaleEnglishGB: "say", discord.LocaleGerman: "sagen", discord.LocaleFrench: "dire", }, Description: "says what you say", DescriptionLocalizations: map[discord.Locale]string{ discord.LocaleEnglishGB: "says what you say", discord.LocaleGerman: "sagt was du sagst", discord.LocaleFrench: "dit ce que vous dites", },}