Documentation ¶
Overview ¶
Package disgoslash provides an easy way to create serverless Discord slash command applications
https://discord.com/developers/docs/interactions/slash-commands
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrAlreadyExists = errors.New("already exists")
ErrAlreadyExists is returned when attempting to create a command which already exists
var ErrForbidden = errors.New("forbidden - missing access")
ErrForbidden is returned when the Disord API responds with a 403
var ErrInvalidInteractionType = errors.New("invalid interaction type")
ErrInvalidInteractionType is returned when the request interaction type is invalid
var ErrMaxRetries = errors.New("max retries reached")
ErrMaxRetries is returned when the maximum number of retries is reached in a retry loop
var ErrNilInteractionResponse = errors.New("interaction response was nil")
ErrNilInteractionResponse is returned when a slash command action returns a nil interaction response
var ErrNotImplemented = errors.New("not implemented")
ErrNotImplemented is returned when whatever was requested hasn't been implemented yet
var ErrTooManyRequests = errors.New("too many requests")
ErrTooManyRequests is returned when the Disord API responds with a 429
var ErrTookTooLong = context.DeadlineExceeded.Error()
ErrTookTooLong is returned when the slash command action took longer than Discord's maximum allowed time
ErrUnauthorized is returned when the request signature is invalid or Discord API responded with 401
Functions ¶
This section is empty.
Types ¶
type Action ¶
type Action func(request *discord.InteractionRequest) *discord.InteractionResponse
Action is the function executed when a slash command interaction request is received by the Handler.
This is where you put the work to be done when a user invokes the slash command.
An action function does not include an error in its response signature because errors should be handled gracefully and a message informing the user that something went wrong should be included in the interaction response.
type Handler ¶
type Handler struct { SlashCommandMap SlashCommandMap Creds *discord.Credentials }
Handler is used to handle Discord slash command interaction requests.
func (*Handler) Handle ¶
func (handler *Handler) Handle(w http.ResponseWriter, r *http.Request)
Handle incoming interaction requests from Discord guilds, executing the SlashCommand's Action and responding with its InteractionResponse.
400 - An invalid Discord Interaction Type was passed in the request.
401 - Authorization failed.
500 - Something unexpected went wrong OR the Action did not respond within discord's maximum response time of 3 seconds.
501 - A SlashCommand that does not exist in the SlashCommandMap was requested.
Example ¶
package main import ( "net/http" "github.com/wafer-bw/disgoslash" "github.com/wafer-bw/disgoslash/discord" ) func main() { creds := &discord.Credentials{ PublicKey: "YOUR_DISCORD_APPLICATION_PUBLIC_KEY", ClientID: "YOUR_DISCORD_APPLICATION_CLIENT_ID", Token: "YOUR_DISCORD_BOT_TOKEN", } handler := &disgoslash.Handler{ SlashCommandMap: disgoslash.SlashCommandMap{}, Creds: creds, } http.HandleFunc("/", handler.Handle) }
Output:
type SlashCommand ¶
type SlashCommand struct { // The work to do when a slash command is invoked by a user Action Action // 1-32 character name matching ^[\w-]{1,32}$ // Ex: "/tableflip" Name string // The IDs of the guilds (servers) the application command // should be registered to. To register the command globally // include a blank string (""). GuildIDs []string // The application command object schema which will be // registered to your Discord servers ApplicationCommand *discord.ApplicationCommand }
SlashCommand holds the required information for disgoslash to execute a slash command Action and register the slash commands with Discord.
func NewSlashCommand ¶
func NewSlashCommand(appCommand *discord.ApplicationCommand, action Action, global bool, guildIDs []string) SlashCommand
NewSlashCommand creates a new SlashCommand
Example ¶
package main import ( "github.com/wafer-bw/disgoslash" "github.com/wafer-bw/disgoslash/discord" ) var slashCommand disgoslash.SlashCommand func action(request *discord.InteractionRequest) *discord.InteractionResponse { name, _ := request.Data.Options[0].StringValue() return &discord.InteractionResponse{ Type: discord.InteractionResponseTypeChannelMessageWithSource, Data: &discord.InteractionApplicationCommandCallbackData{ Content: "Hello " + name + "!", }, } } func main() { isGlobal := true guildIDs := []string{"GUILD_ID", "ANOTHER_GUILD_ID"} applicationCommand := &discord.ApplicationCommand{ Name: "hello", Description: "Says hello to the user", Options: []*discord.ApplicationCommandOption{ { Type: discord.ApplicationCommandOptionTypeString, Name: "Name", Description: "Enter your name", Required: true, }, }, DefaultPermission: true, } slashCommand = disgoslash.NewSlashCommand(applicationCommand, action, isGlobal, guildIDs) }
Output:
type SlashCommandMap ¶
type SlashCommandMap map[string]SlashCommand
SlashCommandMap using each slash command's application command name as a key. Used by disgoslash Handler to serve interaction requests or by disgoslash Syncer to register slash commands with discord.
func NewSlashCommandMap ¶
func NewSlashCommandMap(slashCommands ...SlashCommand) SlashCommandMap
NewSlashCommandMap creates a new SlashCommandMap
Example ¶
package main import ( "github.com/wafer-bw/disgoslash" ) var slashCommand disgoslash.SlashCommand var anotherSlashCommand disgoslash.SlashCommand var slashCommandMap disgoslash.SlashCommandMap func main() { slashCommands := []disgoslash.SlashCommand{slashCommand, anotherSlashCommand} slashCommandMap = disgoslash.NewSlashCommandMap(slashCommands...) }
Output:
type Syncer ¶
type Syncer struct { Creds *discord.Credentials SlashCommandMap SlashCommandMap GuildIDs []string // contains filtered or unexported fields }
Syncer is used to automatically update slash commands on Discord guilds (servers).
func (*Syncer) Sync ¶
Sync your Discord application's slash commands...
Registers new commands, unregisters old commands, and reregisters existing commands.
In order for a command to be registered to a guild (server), the bot will need to be granted the "appliations.commands" scope for that server.
A global command will be registered to all servers the bot has been granted access to.
Example ¶
package main import ( "github.com/wafer-bw/disgoslash" "github.com/wafer-bw/disgoslash/discord" ) func main() { guildIDs := []string{"YOUR_GUILD_(SERVER)_ID", "ANOTHER_GUILD_ID"} creds := &discord.Credentials{ PublicKey: "YOUR_DISCORD_APPLICATION_PUBLIC_KEY", ClientID: "YOUR_DISCORD_APPLICATION_CLIENT_ID", Token: "YOUR_DISCORD_BOT_TOKEN", } syncer := &disgoslash.Syncer{ SlashCommandMap: disgoslash.NewSlashCommandMap(disgoslash.SlashCommand{}), GuildIDs: guildIDs, Creds: creds, } syncer.Sync() }
Output:
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
examples
|
|
vercel/api
Package api provides a Vercel Serverless Function that uses disgoslash to serve Discord Slash Command Interactions.
|
Package api provides a Vercel Serverless Function that uses disgoslash to serve Discord Slash Command Interactions. |