Skip to content

Commit 3776975

Browse files
BowsiePupapricotbucket28
authored andcommitted
Add Acknowledge function to ApplicationCommandInteractionCreate for HTTP server interactions (disgoorg#409)
feat: add Acknowledge response type for interaction handling
1 parent 6bf2fff commit 3776975

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

discord/interaction_response.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ package discord
33
// InteractionResponseType indicates the type of slash command response, whether it's responding immediately or deferring to edit your response later
44
type InteractionResponseType int
55

6+
// InteractionResponseTypeAcknowledge is stricly internal and will never be sent to discord.
7+
//
8+
// It is used to indicate that the HTTP response should be 202 Accepted
9+
const InteractionResponseTypeAcknowledge InteractionResponseType = -1
10+
611
// Constants for the InteractionResponseType(s)
712
const (
813
InteractionResponseTypePong InteractionResponseType = iota + 1

events/interaction_events.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,25 @@ func (e *ApplicationCommandInteractionCreate) Guild() (discord.Guild, bool) {
4242
return discord.Guild{}, false
4343
}
4444

45+
// Acknowledge acknowledges the interaction.
46+
//
47+
// This is used strictly for acknowledging the HTTP interaction request from discord. This responds with 202 Accepted.
48+
//
49+
// When using this, your first http request must be [rest.Interactions.CreateInteractionResponse] or [rest.Interactions.CreateInteractionResponseWithCallback]
50+
//
51+
// This does not produce a visible loading state to the user.
52+
// You are expected to send a new http request within 3 seconds to respond to the interaction.
53+
// This allows you to gracefully handle errors with your sent response & access the resulting message.
54+
//
55+
// If you want to create a visible loading state, use DeferCreateMessage.
56+
//
57+
// Source docs: [Discord Source docs]
58+
//
59+
// [Discord Source docs]: https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-callback
60+
func (e *ApplicationCommandInteractionCreate) Acknowledge(opts ...rest.RequestOpt) error {
61+
return e.Respond(discord.InteractionResponseTypeAcknowledge, nil, opts...)
62+
}
63+
4564
// CreateMessage responds to the interaction with a new message.
4665
func (e *ApplicationCommandInteractionCreate) CreateMessage(messageCreate discord.MessageCreate, opts ...rest.RequestOpt) error {
4766
return e.Respond(discord.InteractionResponseTypeCreateMessage, messageCreate, opts...)
@@ -89,6 +108,25 @@ func (e *ComponentInteractionCreate) Guild() (discord.Guild, bool) {
89108
return discord.Guild{}, false
90109
}
91110

111+
// Acknowledge acknowledges the interaction.
112+
//
113+
// This is used strictly for acknowledging the HTTP interaction request from discord. This responds with 202 Accepted.
114+
//
115+
// When using this, your first http request must be [rest.Interactions.CreateInteractionResponse] or [rest.Interactions.CreateInteractionResponseWithCallback]
116+
//
117+
// This does not produce a visible loading state to the user.
118+
// You are expected to send a new http request within 3 seconds to respond to the interaction.
119+
// This allows you to gracefully handle errors with your sent response & access the resulting message.
120+
//
121+
// If you want to create a visible loading state, use DeferCreateMessage.
122+
//
123+
// Source docs: [Discord Source docs]
124+
//
125+
// [Discord Source docs]: https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-callback
126+
func (e *ComponentInteractionCreate) Acknowledge(opts ...rest.RequestOpt) error {
127+
return e.Respond(discord.InteractionResponseTypeAcknowledge, nil, opts...)
128+
}
129+
92130
// CreateMessage responds to the interaction with a new message.
93131
func (e *ComponentInteractionCreate) CreateMessage(messageCreate discord.MessageCreate, opts ...rest.RequestOpt) error {
94132
return e.Respond(discord.InteractionResponseTypeCreateMessage, messageCreate, opts...)
@@ -146,6 +184,25 @@ func (e *AutocompleteInteractionCreate) Guild() (discord.Guild, bool) {
146184
return discord.Guild{}, false
147185
}
148186

187+
// Acknowledge acknowledges the interaction.
188+
//
189+
// This is used strictly for acknowledging the HTTP interaction request from discord. This responds with 202 Accepted.
190+
//
191+
// When using this, your first http request must be [rest.Interactions.CreateInteractionResponse] or [rest.Interactions.CreateInteractionResponseWithCallback]
192+
//
193+
// This does not produce a visible loading state to the user.
194+
// You are expected to send a new http request within 3 seconds to respond to the interaction.
195+
// This allows you to gracefully handle errors with your sent response & access the resulting message.
196+
//
197+
// If you want to create a visible loading state, use DeferCreateMessage.
198+
//
199+
// Source docs: [Discord Source docs]
200+
//
201+
// [Discord Source docs]: https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-callback
202+
func (e *AutocompleteInteractionCreate) Acknowledge(opts ...rest.RequestOpt) error {
203+
return e.Respond(discord.InteractionResponseTypeAcknowledge, nil, opts...)
204+
}
205+
149206
// AutocompleteResult responds to the interaction with a slice of choices.
150207
func (e *AutocompleteInteractionCreate) AutocompleteResult(choices []discord.AutocompleteChoice, opts ...rest.RequestOpt) error {
151208
return e.Respond(discord.InteractionResponseTypeAutocompleteResult, discord.AutocompleteResult{Choices: choices}, opts...)
@@ -168,6 +225,25 @@ func (e *ModalSubmitInteractionCreate) Guild() (discord.Guild, bool) {
168225
return discord.Guild{}, false
169226
}
170227

228+
// Acknowledge acknowledges the interaction.
229+
//
230+
// This is used strictly for acknowledging the HTTP interaction request from discord. This responds with 202 Accepted.
231+
//
232+
// When using this, your first http request must be [rest.Interactions.CreateInteractionResponse] or [rest.Interactions.CreateInteractionResponseWithCallback]
233+
//
234+
// This does not produce a visible loading state to the user.
235+
// You are expected to send a new http request within 3 seconds to respond to the interaction.
236+
// This allows you to gracefully handle errors with your sent response & access the resulting message.
237+
//
238+
// If you want to create a visible loading state, use DeferCreateMessage.
239+
//
240+
// Source docs: [Discord Source docs]
241+
//
242+
// [Discord Source docs]: https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-callback
243+
func (e *ModalSubmitInteractionCreate) Acknowledge(opts ...rest.RequestOpt) error {
244+
return e.Respond(discord.InteractionResponseTypeAcknowledge, nil, opts...)
245+
}
246+
171247
// CreateMessage responds to the interaction with a new message.
172248
func (e *ModalSubmitInteractionCreate) CreateMessage(messageCreate discord.MessageCreate, opts ...rest.RequestOpt) error {
173249
return e.Respond(discord.InteractionResponseTypeCreateMessage, messageCreate, opts...)

httpserver/server.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ func HandleInteraction(publicKey PublicKey, logger *slog.Logger, handleFunc Even
167167
defer cancel()
168168
select {
169169
case response := <-responseChannel:
170+
171+
// if we only acknowledge the interaction, we don't need to send a response body
172+
// we just need to send a 202 Accepted status
173+
if response.Type == discord.InteractionResponseTypeAcknowledge {
174+
w.WriteHeader(http.StatusAccepted)
175+
return
176+
}
177+
170178
if body, err = response.ToBody(); err != nil {
171179
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
172180
errorChannel <- err

0 commit comments

Comments
 (0)