Skip to content

Commit

Permalink
Add Acknowledge function to ApplicationCommandInteractionCreate for H…
Browse files Browse the repository at this point in the history
…TTP server interactions (#409)

feat: add Acknowledge response type for interaction handling
  • Loading branch information
BowsiePup authored Jan 20, 2025
1 parent 3bdb88c commit 69944d0
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
5 changes: 5 additions & 0 deletions discord/interaction_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ package discord
// InteractionResponseType indicates the type of slash command response, whether it's responding immediately or deferring to edit your response later
type InteractionResponseType int

// InteractionResponseTypeAcknowledge is stricly internal and will never be sent to discord.
//
// It is used to indicate that the HTTP response should be 202 Accepted
const InteractionResponseTypeAcknowledge InteractionResponseType = -1

// Constants for the InteractionResponseType(s)
const (
InteractionResponseTypePong InteractionResponseType = iota + 1
Expand Down
76 changes: 76 additions & 0 deletions events/interaction_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ func (e *ApplicationCommandInteractionCreate) Guild() (discord.Guild, bool) {
return discord.Guild{}, false
}

// Acknowledge acknowledges the interaction.
//
// This is used strictly for acknowledging the HTTP interaction request from discord. This responds with 202 Accepted.
//
// When using this, your first http request must be [rest.Interactions.CreateInteractionResponse] or [rest.Interactions.CreateInteractionResponseWithCallback]
//
// This does not produce a visible loading state to the user.
// You are expected to send a new http request within 3 seconds to respond to the interaction.
// This allows you to gracefully handle errors with your sent response & access the resulting message.
//
// If you want to create a visible loading state, use DeferCreateMessage.
//
// Source docs: [Discord Source docs]
//
// [Discord Source docs]: https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-callback
func (e *ApplicationCommandInteractionCreate) Acknowledge(opts ...rest.RequestOpt) error {
return e.Respond(discord.InteractionResponseTypeAcknowledge, nil, opts...)
}

// CreateMessage responds to the interaction with a new message.
func (e *ApplicationCommandInteractionCreate) CreateMessage(messageCreate discord.MessageCreate, opts ...rest.RequestOpt) error {
return e.Respond(discord.InteractionResponseTypeCreateMessage, messageCreate, opts...)
Expand Down Expand Up @@ -89,6 +108,25 @@ func (e *ComponentInteractionCreate) Guild() (discord.Guild, bool) {
return discord.Guild{}, false
}

// Acknowledge acknowledges the interaction.
//
// This is used strictly for acknowledging the HTTP interaction request from discord. This responds with 202 Accepted.
//
// When using this, your first http request must be [rest.Interactions.CreateInteractionResponse] or [rest.Interactions.CreateInteractionResponseWithCallback]
//
// This does not produce a visible loading state to the user.
// You are expected to send a new http request within 3 seconds to respond to the interaction.
// This allows you to gracefully handle errors with your sent response & access the resulting message.
//
// If you want to create a visible loading state, use DeferCreateMessage.
//
// Source docs: [Discord Source docs]
//
// [Discord Source docs]: https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-callback
func (e *ComponentInteractionCreate) Acknowledge(opts ...rest.RequestOpt) error {
return e.Respond(discord.InteractionResponseTypeAcknowledge, nil, opts...)
}

// CreateMessage responds to the interaction with a new message.
func (e *ComponentInteractionCreate) CreateMessage(messageCreate discord.MessageCreate, opts ...rest.RequestOpt) error {
return e.Respond(discord.InteractionResponseTypeCreateMessage, messageCreate, opts...)
Expand Down Expand Up @@ -146,6 +184,25 @@ func (e *AutocompleteInteractionCreate) Guild() (discord.Guild, bool) {
return discord.Guild{}, false
}

// Acknowledge acknowledges the interaction.
//
// This is used strictly for acknowledging the HTTP interaction request from discord. This responds with 202 Accepted.
//
// When using this, your first http request must be [rest.Interactions.CreateInteractionResponse] or [rest.Interactions.CreateInteractionResponseWithCallback]
//
// This does not produce a visible loading state to the user.
// You are expected to send a new http request within 3 seconds to respond to the interaction.
// This allows you to gracefully handle errors with your sent response & access the resulting message.
//
// If you want to create a visible loading state, use DeferCreateMessage.
//
// Source docs: [Discord Source docs]
//
// [Discord Source docs]: https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-callback
func (e *AutocompleteInteractionCreate) Acknowledge(opts ...rest.RequestOpt) error {
return e.Respond(discord.InteractionResponseTypeAcknowledge, nil, opts...)
}

// AutocompleteResult responds to the interaction with a slice of choices.
func (e *AutocompleteInteractionCreate) AutocompleteResult(choices []discord.AutocompleteChoice, opts ...rest.RequestOpt) error {
return e.Respond(discord.InteractionResponseTypeAutocompleteResult, discord.AutocompleteResult{Choices: choices}, opts...)
Expand All @@ -168,6 +225,25 @@ func (e *ModalSubmitInteractionCreate) Guild() (discord.Guild, bool) {
return discord.Guild{}, false
}

// Acknowledge acknowledges the interaction.
//
// This is used strictly for acknowledging the HTTP interaction request from discord. This responds with 202 Accepted.
//
// When using this, your first http request must be [rest.Interactions.CreateInteractionResponse] or [rest.Interactions.CreateInteractionResponseWithCallback]
//
// This does not produce a visible loading state to the user.
// You are expected to send a new http request within 3 seconds to respond to the interaction.
// This allows you to gracefully handle errors with your sent response & access the resulting message.
//
// If you want to create a visible loading state, use DeferCreateMessage.
//
// Source docs: [Discord Source docs]
//
// [Discord Source docs]: https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-callback
func (e *ModalSubmitInteractionCreate) Acknowledge(opts ...rest.RequestOpt) error {
return e.Respond(discord.InteractionResponseTypeAcknowledge, nil, opts...)
}

// CreateMessage responds to the interaction with a new message.
func (e *ModalSubmitInteractionCreate) CreateMessage(messageCreate discord.MessageCreate, opts ...rest.RequestOpt) error {
return e.Respond(discord.InteractionResponseTypeCreateMessage, messageCreate, opts...)
Expand Down
8 changes: 8 additions & 0 deletions httpserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ func HandleInteraction(publicKey PublicKey, logger *slog.Logger, handleFunc Even
defer cancel()
select {
case response := <-responseChannel:

// if we only acknowledge the interaction, we don't need to send a response body
// we just need to send a 202 Accepted status
if response.Type == discord.InteractionResponseTypeAcknowledge {
w.WriteHeader(http.StatusAccepted)
return
}

if body, err = response.ToBody(); err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
errorChannel <- err
Expand Down

0 comments on commit 69944d0

Please sign in to comment.