Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add InteractionCallbackResponse #1584

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/autocomplete/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var (
switch i.Type {
case discordgo.InteractionApplicationCommand:
data := i.ApplicationCommandData()
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
_, err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf(
Expand Down Expand Up @@ -120,7 +120,7 @@ var (
})
}

err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
_, err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionApplicationCommandAutocompleteResult,
Data: &discordgo.InteractionResponseData{
Choices: choices, // This is basically the whole purpose of autocomplete interaction - return custom options to the user.
Expand All @@ -135,7 +135,7 @@ var (
switch i.Type {
case discordgo.InteractionApplicationCommand:
data := i.ApplicationCommandData()
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
_, err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf(
Expand Down Expand Up @@ -206,7 +206,7 @@ var (
}
}

err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
_, err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionApplicationCommandAutocompleteResult,
Data: &discordgo.InteractionResponseData{
Choices: choices,
Expand Down
14 changes: 7 additions & 7 deletions examples/components/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func init() {
var (
componentsHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"fd_no": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
_, err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Huh. I see, maybe some of these resources might help you?",
Expand Down Expand Up @@ -78,7 +78,7 @@ var (
}
},
"fd_yes": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
_, err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Great! If you wanna know more or just have questions, feel free to visit Discord Devs and Discord Gophers server. " +
Expand Down Expand Up @@ -134,7 +134,7 @@ var (
},
}
}
err := s.InteractionRespond(i.Interaction, response)
_, err := s.InteractionRespond(i.Interaction, response)
if err != nil {
panic(err)
}
Expand All @@ -153,7 +153,7 @@ var (

const stackoverflowFormat = `https://stackoverflow.com/questions/tagged/%s`

err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
_, err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Here is your stackoverflow URL: " + fmt.Sprintf(stackoverflowFormat, strings.Join(data.Values, "+")),
Expand All @@ -173,7 +173,7 @@ var (
}
},
"channel_select": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
_, err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "This is it. You've reached your destination. Your choice was <#" + i.MessageComponentData().Values[0] + ">\n" +
Expand Down Expand Up @@ -219,7 +219,7 @@ var (
}
commandsHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"buttons": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
_, err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Are you comfortable with buttons and other message components?",
Expand Down Expand Up @@ -417,7 +417,7 @@ var (
},
}
}
err := s.InteractionRespond(i.Interaction, response)
_, err := s.InteractionRespond(i.Interaction, response)
if err != nil {
panic(err)
}
Expand Down
12 changes: 6 additions & 6 deletions examples/context_menus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ var (
}
commandsHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"rickroll-em": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
_, err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Operation rickroll has begun",
Expand Down Expand Up @@ -102,7 +102,7 @@ var (
}
},
"google-it": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
_, err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: searchLink(
Expand All @@ -116,7 +116,7 @@ var (
}
},
"stackoverflow-it": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
_, err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: searchLink(
Expand All @@ -130,7 +130,7 @@ var (
}
},
"godoc-it": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
_, err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: searchLink(
Expand All @@ -144,7 +144,7 @@ var (
}
},
"discordjs-it": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
_, err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: searchLink(
Expand All @@ -158,7 +158,7 @@ var (
}
},
"discordpy-it": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
_, err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: searchLink(
Expand Down
2 changes: 1 addition & 1 deletion examples/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func handleEcho(s *discordgo.Session, i *discordgo.InteractionCreate, opts optio
}
builder.WriteString(opts["message"].StringValue())

err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
_, err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: builder.String(),
Expand Down
4 changes: 2 additions & 2 deletions examples/modals/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var (
}
commandsHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"modals-survey": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
_, err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseModal,
Data: &discordgo.InteractionResponseData{
CustomID: "modals_survey_" + i.Interaction.Member.User.ID,
Expand Down Expand Up @@ -95,7 +95,7 @@ func main() {
h(s, i)
}
case discordgo.InteractionModalSubmit:
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
_, err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Thank you for taking your time to fill this survey",
Expand Down
6 changes: 3 additions & 3 deletions examples/slash_commands/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ var (
if r, ok := responses[i.Locale]; ok {
response = r
}
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
_, err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: response,
Expand Down Expand Up @@ -452,7 +452,7 @@ var (
content +=
"\nAlso... you can edit your response, wait 5 seconds and this message will be changed"
default:
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
_, err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseType(i.ApplicationCommandData().Options[0].IntValue()),
})
if err != nil {
Expand All @@ -463,7 +463,7 @@ var (
return
}

err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
_, err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseType(i.ApplicationCommandData().Options[0].IntValue()),
Data: &discordgo.InteractionResponseData{
Content: content,
Expand Down
41 changes: 41 additions & 0 deletions interactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ func (o ApplicationCommandInteractionDataOption) UserValue(s *Session) *User {
type InteractionResponseType uint8

// Interaction response types.
// https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-response-object-interaction-callback-type
const (
// InteractionResponsePong is for ACK ping event.
InteractionResponsePong InteractionResponseType = 1
Expand All @@ -582,6 +583,8 @@ const (
InteractionApplicationCommandAutocompleteResult InteractionResponseType = 8
// InteractionResponseModal is for responding to an interaction with a modal window.
InteractionResponseModal InteractionResponseType = 9
// InteractionResponseLaunchActivity is for launching the Activity associated with the app
InteractionResponseLaunchActivity InteractionResponseType = 12
)

// InteractionResponse represents a response for an interaction event.
Expand Down Expand Up @@ -613,6 +616,44 @@ type InteractionResponseData struct {
Title string `json:"title,omitempty"`
}

// InteractionCallbackResponse is returned on interaction callback
// https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-callback-interaction-callback-response-object
type InteractionCallbackResponse struct {
Interaction InteractionCallback `json:"interaction"`
Resource *InteractionCallbackResource `json:"resource,omitempty"`
}

// InteractionCallback is returned as part of InteractionCallbackResponse
// https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-callback-interaction-callback-object
type InteractionCallback struct {
ID string `json:"id"`
Type InteractionType `json:"type"`
ActivityInstanceID string `json:"activity_instance_id,omitempty"`
ResponseMessageID string `json:"response_message_id,omitempty"`
ResponseMessageLoading bool `json:"response_message_loading,omitempty"`
ResponseMessageEphemeral bool `json:"response_message_ephemeral,omitempty"`
}

// InteractionCallbackResource is returned as part of InteractionCallbackResponse.
// https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-callback-interaction-callback-resource-object
type InteractionCallbackResource struct {
Type InteractionResponseType `json:"type"`

// ActivityInstance resource represents the Activity launched by this interaction
// Only present if Type is InteractionResponseLaunchActivity
ActivityInstance *InteractionCallbackActivityInstance `json:"activity_instance"`

// Message is only present if Type is either InteractionResponseChannelMessageWithSource or InteractionResponseUpdateMessage
Message *Message `json:"message"`
}

// InteractionCallbackActivityInstance represents the Activity launched by this interaction
// Only present if InteractionCallbackResource.Type is InteractionResponseLaunchActivity
// https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-callback-interaction-callback-activity-instance-resource
type InteractionCallbackActivityInstance struct {
ID string `json:"id"`
}

// VerifyInteraction implements message verification of the discord interactions api
// signing algorithm, as documented here:
// https://discord.com/developers/docs/interactions/receiving-and-responding#security-and-authorization
Expand Down
18 changes: 13 additions & 5 deletions restapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3133,21 +3133,29 @@ func (s *Session) ApplicationCommandPermissionsBatchEdit(appID, guildID string,
// InteractionRespond creates the response to an interaction.
// interaction : Interaction instance.
// resp : Response message data.
func (s *Session) InteractionRespond(interaction *Interaction, resp *InteractionResponse, options ...RequestOption) error {
// If the interaction type returns a body, then interactionCallbackResponse will be returned
func (s *Session) InteractionRespond(interaction *Interaction, resp *InteractionResponse, options ...RequestOption) (interactionCallbackResponse *InteractionCallbackResponse, err error) {
endpoint := EndpointInteractionResponse(interaction.ID, interaction.Token)

if resp.Data != nil && len(resp.Data.Files) > 0 {
contentType, body, err := MultipartBodyWithJSON(resp, resp.Data.Files)
if err != nil {
return err
return nil, err
}

_, err = s.request("POST", endpoint, contentType, body, endpoint, 0, options...)
return err
return nil, err
}

_, err := s.RequestWithBucketID("POST", endpoint, *resp, endpoint, options...)
return err
body, err := s.RequestWithBucketID("POST", endpoint, *resp, endpoint, options...)

if err != nil || len(body) == 0 {
return
}

err = unmarshal(body, &interactionCallbackResponse)

return
}

// InteractionResponse gets the response to an interaction.
Expand Down
Loading