Skip to content
Merged
4 changes: 3 additions & 1 deletion discord/interaction_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ type InteractionResponseType int

// Constants for the InteractionResponseType(s)
const (
InteractionResponseTypePong InteractionResponseType = iota + 1
// This is stricly internal and will never be sent to discord. It is used to indicate that the HTTP response should be 202 Accepted
InteractionResponseTypeAcknowledge InteractionResponseType = iota
InteractionResponseTypePong InteractionResponseType = iota
_
_
InteractionResponseTypeCreateMessage
Expand Down
14 changes: 14 additions & 0 deletions events/interaction_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ 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.
//
// 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: 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
9 changes: 9 additions & 0 deletions httpserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ 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)
w.Write(nil)
return
}

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