github.com/meowstercatel/discordgo@v0.0.0-20250615204139-29b43d676735/interactions.go (about)

     1  package discordgo
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/ed25519"
     6  	"encoding/hex"
     7  	"encoding/json"
     8  	"fmt"
     9  	"io"
    10  	"io/ioutil"
    11  	"net/http"
    12  	"strconv"
    13  	"time"
    14  )
    15  
    16  // InteractionDeadline is the time allowed to respond to an interaction.
    17  const InteractionDeadline = time.Second * 3
    18  
    19  // ApplicationCommandType represents the type of application command.
    20  type ApplicationCommandType uint8
    21  
    22  // Application command types
    23  const (
    24  	// ChatApplicationCommand is default command type. They are slash commands (i.e. called directly from the chat).
    25  	ChatApplicationCommand ApplicationCommandType = 1
    26  	// UserApplicationCommand adds command to user context menu.
    27  	UserApplicationCommand ApplicationCommandType = 2
    28  	// MessageApplicationCommand adds command to message context menu.
    29  	MessageApplicationCommand ApplicationCommandType = 3
    30  )
    31  
    32  // ApplicationCommand represents an application's slash command.
    33  type ApplicationCommand struct {
    34  	ID                string                 `json:"id,omitempty"`
    35  	ApplicationID     string                 `json:"application_id,omitempty"`
    36  	GuildID           string                 `json:"guild_id,omitempty"`
    37  	Version           string                 `json:"version,omitempty"`
    38  	Type              ApplicationCommandType `json:"type,omitempty"`
    39  	Name              string                 `json:"name"`
    40  	NameLocalizations *map[Locale]string     `json:"name_localizations,omitempty"`
    41  
    42  	// NOTE: DefaultPermission will be soon deprecated. Use DefaultMemberPermissions and Contexts instead.
    43  	DefaultPermission        *bool  `json:"default_permission,omitempty"`
    44  	DefaultMemberPermissions *int64 `json:"default_member_permissions,string,omitempty"`
    45  	NSFW                     *bool  `json:"nsfw,omitempty"`
    46  
    47  	// Deprecated: use Contexts instead.
    48  	DMPermission     *bool                         `json:"dm_permission,omitempty"`
    49  	Contexts         *[]InteractionContextType     `json:"contexts,omitempty"`
    50  	IntegrationTypes *[]ApplicationIntegrationType `json:"integration_types,omitempty"`
    51  
    52  	// NOTE: Chat commands only. Otherwise it mustn't be set.
    53  
    54  	Description              string                      `json:"description,omitempty"`
    55  	DescriptionLocalizations *map[Locale]string          `json:"description_localizations,omitempty"`
    56  	Options                  []*ApplicationCommandOption `json:"options"`
    57  }
    58  
    59  // ApplicationCommandOptionType indicates the type of a slash command's option.
    60  type ApplicationCommandOptionType uint8
    61  
    62  // Application command option types.
    63  const (
    64  	ApplicationCommandOptionSubCommand      ApplicationCommandOptionType = 1
    65  	ApplicationCommandOptionSubCommandGroup ApplicationCommandOptionType = 2
    66  	ApplicationCommandOptionString          ApplicationCommandOptionType = 3
    67  	ApplicationCommandOptionInteger         ApplicationCommandOptionType = 4
    68  	ApplicationCommandOptionBoolean         ApplicationCommandOptionType = 5
    69  	ApplicationCommandOptionUser            ApplicationCommandOptionType = 6
    70  	ApplicationCommandOptionChannel         ApplicationCommandOptionType = 7
    71  	ApplicationCommandOptionRole            ApplicationCommandOptionType = 8
    72  	ApplicationCommandOptionMentionable     ApplicationCommandOptionType = 9
    73  	ApplicationCommandOptionNumber          ApplicationCommandOptionType = 10
    74  	ApplicationCommandOptionAttachment      ApplicationCommandOptionType = 11
    75  )
    76  
    77  func (t ApplicationCommandOptionType) String() string {
    78  	switch t {
    79  	case ApplicationCommandOptionSubCommand:
    80  		return "SubCommand"
    81  	case ApplicationCommandOptionSubCommandGroup:
    82  		return "SubCommandGroup"
    83  	case ApplicationCommandOptionString:
    84  		return "String"
    85  	case ApplicationCommandOptionInteger:
    86  		return "Integer"
    87  	case ApplicationCommandOptionBoolean:
    88  		return "Boolean"
    89  	case ApplicationCommandOptionUser:
    90  		return "User"
    91  	case ApplicationCommandOptionChannel:
    92  		return "Channel"
    93  	case ApplicationCommandOptionRole:
    94  		return "Role"
    95  	case ApplicationCommandOptionMentionable:
    96  		return "Mentionable"
    97  	case ApplicationCommandOptionNumber:
    98  		return "Number"
    99  	case ApplicationCommandOptionAttachment:
   100  		return "Attachment"
   101  	}
   102  	return fmt.Sprintf("ApplicationCommandOptionType(%d)", t)
   103  }
   104  
   105  // ApplicationCommandOption represents an option/subcommand/subcommands group.
   106  type ApplicationCommandOption struct {
   107  	Type                     ApplicationCommandOptionType `json:"type"`
   108  	Name                     string                       `json:"name"`
   109  	NameLocalizations        map[Locale]string            `json:"name_localizations,omitempty"`
   110  	Description              string                       `json:"description,omitempty"`
   111  	DescriptionLocalizations map[Locale]string            `json:"description_localizations,omitempty"`
   112  	// NOTE: This feature was on the API, but at some point developers decided to remove it.
   113  	// So I commented it, until it will be officially on the docs.
   114  	// Default     bool                              `json:"default"`
   115  
   116  	ChannelTypes []ChannelType               `json:"channel_types"`
   117  	Required     bool                        `json:"required"`
   118  	Options      []*ApplicationCommandOption `json:"options"`
   119  
   120  	// NOTE: mutually exclusive with Choices.
   121  	Autocomplete bool                              `json:"autocomplete"`
   122  	Choices      []*ApplicationCommandOptionChoice `json:"choices"`
   123  	// Minimal value of number/integer option.
   124  	MinValue *float64 `json:"min_value,omitempty"`
   125  	// Maximum value of number/integer option.
   126  	MaxValue float64 `json:"max_value,omitempty"`
   127  	// Minimum length of string option.
   128  	MinLength *int `json:"min_length,omitempty"`
   129  	// Maximum length of string option.
   130  	MaxLength int `json:"max_length,omitempty"`
   131  }
   132  
   133  // ApplicationCommandOptionChoice represents a slash command option choice.
   134  type ApplicationCommandOptionChoice struct {
   135  	Name              string            `json:"name"`
   136  	NameLocalizations map[Locale]string `json:"name_localizations,omitempty"`
   137  	Value             interface{}       `json:"value"`
   138  }
   139  
   140  // ApplicationCommandPermissions represents a single user or role permission for a command.
   141  type ApplicationCommandPermissions struct {
   142  	ID         string                           `json:"id"`
   143  	Type       ApplicationCommandPermissionType `json:"type"`
   144  	Permission bool                             `json:"permission"`
   145  }
   146  
   147  // GuildAllChannelsID is a helper function which returns guild_id-1.
   148  // It is used in ApplicationCommandPermissions to target all the channels within a guild.
   149  func GuildAllChannelsID(guild string) (id string, err error) {
   150  	var v uint64
   151  	v, err = strconv.ParseUint(guild, 10, 64)
   152  	if err != nil {
   153  		return
   154  	}
   155  
   156  	return strconv.FormatUint(v-1, 10), nil
   157  }
   158  
   159  // ApplicationCommandPermissionsList represents a list of ApplicationCommandPermissions, needed for serializing to JSON.
   160  type ApplicationCommandPermissionsList struct {
   161  	Permissions []*ApplicationCommandPermissions `json:"permissions"`
   162  }
   163  
   164  // GuildApplicationCommandPermissions represents all permissions for a single guild command.
   165  type GuildApplicationCommandPermissions struct {
   166  	ID            string                           `json:"id"`
   167  	ApplicationID string                           `json:"application_id"`
   168  	GuildID       string                           `json:"guild_id"`
   169  	Permissions   []*ApplicationCommandPermissions `json:"permissions"`
   170  }
   171  
   172  // ApplicationCommandPermissionType indicates whether a permission is user or role based.
   173  type ApplicationCommandPermissionType uint8
   174  
   175  // Application command permission types.
   176  const (
   177  	ApplicationCommandPermissionTypeRole    ApplicationCommandPermissionType = 1
   178  	ApplicationCommandPermissionTypeUser    ApplicationCommandPermissionType = 2
   179  	ApplicationCommandPermissionTypeChannel ApplicationCommandPermissionType = 3
   180  )
   181  
   182  // InteractionType indicates the type of an interaction event.
   183  type InteractionType uint8
   184  
   185  // Interaction types
   186  const (
   187  	InteractionPing                           InteractionType = 1
   188  	InteractionApplicationCommand             InteractionType = 2
   189  	InteractionMessageComponent               InteractionType = 3
   190  	InteractionApplicationCommandAutocomplete InteractionType = 4
   191  	InteractionModalSubmit                    InteractionType = 5
   192  )
   193  
   194  func (t InteractionType) String() string {
   195  	switch t {
   196  	case InteractionPing:
   197  		return "Ping"
   198  	case InteractionApplicationCommand:
   199  		return "ApplicationCommand"
   200  	case InteractionMessageComponent:
   201  		return "MessageComponent"
   202  	case InteractionModalSubmit:
   203  		return "ModalSubmit"
   204  	}
   205  	return fmt.Sprintf("InteractionType(%d)", t)
   206  }
   207  
   208  // InteractionContextType represents the context in which interaction can be used or was triggered from.
   209  type InteractionContextType uint
   210  
   211  const (
   212  	// InteractionContextGuild indicates that interaction can be used within guilds.
   213  	InteractionContextGuild InteractionContextType = 0
   214  	// InteractionContextBotDM indicates that interaction can be used within DMs with the bot.
   215  	InteractionContextBotDM InteractionContextType = 1
   216  	// InteractionContextPrivateChannel indicates that interaction can be used within group DMs and DMs with other users.
   217  	InteractionContextPrivateChannel InteractionContextType = 2
   218  )
   219  
   220  // Interaction represents data of an interaction.
   221  type Interaction struct {
   222  	ID        string          `json:"id"`
   223  	AppID     string          `json:"application_id"`
   224  	Type      InteractionType `json:"type"`
   225  	Data      InteractionData `json:"data"`
   226  	GuildID   string          `json:"guild_id"`
   227  	ChannelID string          `json:"channel_id"`
   228  
   229  	// The message on which interaction was used.
   230  	// NOTE: this field is only filled when a button click triggered the interaction. Otherwise it will be nil.
   231  	Message *Message `json:"message"`
   232  
   233  	// Bitwise set of permissions the app or bot has within the channel the interaction was sent from
   234  	AppPermissions int64 `json:"app_permissions,string"`
   235  
   236  	// The member who invoked this interaction.
   237  	// NOTE: this field is only filled when the slash command was invoked in a guild;
   238  	// if it was invoked in a DM, the `User` field will be filled instead.
   239  	// Make sure to check for `nil` before using this field.
   240  	Member *Member `json:"member"`
   241  	// The user who invoked this interaction.
   242  	// NOTE: this field is only filled when the slash command was invoked in a DM;
   243  	// if it was invoked in a guild, the `Member` field will be filled instead.
   244  	// Make sure to check for `nil` before using this field.
   245  	User *User `json:"user"`
   246  
   247  	// The user's discord client locale.
   248  	Locale Locale `json:"locale"`
   249  	// The guild's locale. This defaults to EnglishUS
   250  	// NOTE: this field is only filled when the interaction was invoked in a guild.
   251  	GuildLocale *Locale `json:"guild_locale"`
   252  
   253  	Context                      InteractionContextType                `json:"context"`
   254  	AuthorizingIntegrationOwners map[ApplicationIntegrationType]string `json:"authorizing_integration_owners"`
   255  
   256  	Token   string `json:"token"`
   257  	Version int    `json:"version"`
   258  
   259  	// Any entitlements for the invoking user, representing access to premium SKUs.
   260  	// NOTE: this field is only filled in monetized apps
   261  	Entitlements []*Entitlement `json:"entitlements"`
   262  }
   263  
   264  type interaction Interaction
   265  
   266  type rawInteraction struct {
   267  	interaction
   268  	Data json.RawMessage `json:"data"`
   269  }
   270  
   271  // UnmarshalJSON is a method for unmarshalling JSON object to Interaction.
   272  func (i *Interaction) UnmarshalJSON(raw []byte) error {
   273  	var tmp rawInteraction
   274  	err := json.Unmarshal(raw, &tmp)
   275  	if err != nil {
   276  		return err
   277  	}
   278  
   279  	*i = Interaction(tmp.interaction)
   280  
   281  	switch tmp.Type {
   282  	case InteractionApplicationCommand, InteractionApplicationCommandAutocomplete:
   283  		v := ApplicationCommandInteractionData{}
   284  		err = json.Unmarshal(tmp.Data, &v)
   285  		if err != nil {
   286  			return err
   287  		}
   288  		i.Data = v
   289  	case InteractionMessageComponent:
   290  		v := MessageComponentInteractionData{}
   291  		err = json.Unmarshal(tmp.Data, &v)
   292  		if err != nil {
   293  			return err
   294  		}
   295  		i.Data = v
   296  	case InteractionModalSubmit:
   297  		v := ModalSubmitInteractionData{}
   298  		err = json.Unmarshal(tmp.Data, &v)
   299  		if err != nil {
   300  			return err
   301  		}
   302  		i.Data = v
   303  	}
   304  	return nil
   305  }
   306  
   307  // MessageComponentData is helper function to assert the inner InteractionData to MessageComponentInteractionData.
   308  // Make sure to check that the Type of the interaction is InteractionMessageComponent before calling.
   309  func (i Interaction) MessageComponentData() (data MessageComponentInteractionData) {
   310  	if i.Type != InteractionMessageComponent {
   311  		panic("MessageComponentData called on interaction of type " + i.Type.String())
   312  	}
   313  	return i.Data.(MessageComponentInteractionData)
   314  }
   315  
   316  // ApplicationCommandData is helper function to assert the inner InteractionData to ApplicationCommandInteractionData.
   317  // Make sure to check that the Type of the interaction is InteractionApplicationCommand before calling.
   318  func (i Interaction) ApplicationCommandData() (data ApplicationCommandInteractionData) {
   319  	if i.Type != InteractionApplicationCommand && i.Type != InteractionApplicationCommandAutocomplete {
   320  		panic("ApplicationCommandData called on interaction of type " + i.Type.String())
   321  	}
   322  	return i.Data.(ApplicationCommandInteractionData)
   323  }
   324  
   325  // ModalSubmitData is helper function to assert the inner InteractionData to ModalSubmitInteractionData.
   326  // Make sure to check that the Type of the interaction is InteractionModalSubmit before calling.
   327  func (i Interaction) ModalSubmitData() (data ModalSubmitInteractionData) {
   328  	if i.Type != InteractionModalSubmit {
   329  		panic("ModalSubmitData called on interaction of type " + i.Type.String())
   330  	}
   331  	return i.Data.(ModalSubmitInteractionData)
   332  }
   333  
   334  // InteractionData is a common interface for all types of interaction data.
   335  type InteractionData interface {
   336  	Type() InteractionType
   337  }
   338  
   339  // ApplicationCommandInteractionData contains the data of application command interaction.
   340  type ApplicationCommandInteractionData struct {
   341  	ID          string                                     `json:"id"`
   342  	Name        string                                     `json:"name"`
   343  	CommandType ApplicationCommandType                     `json:"type"`
   344  	Resolved    *ApplicationCommandInteractionDataResolved `json:"resolved"`
   345  
   346  	// Slash command options
   347  	Options []*ApplicationCommandInteractionDataOption `json:"options"`
   348  	// Target (user/message) id on which context menu command was called.
   349  	// The details are stored in Resolved according to command type.
   350  	TargetID string `json:"target_id"`
   351  }
   352  
   353  // GetOption finds and returns an application command option by its name.
   354  func (d ApplicationCommandInteractionData) GetOption(name string) (option *ApplicationCommandInteractionDataOption) {
   355  	for _, opt := range d.Options {
   356  		if opt.Name == name {
   357  			option = opt
   358  			break
   359  		}
   360  	}
   361  
   362  	return
   363  }
   364  
   365  // ApplicationCommandInteractionDataResolved contains resolved data of command execution.
   366  // Partial Member objects are missing user, deaf and mute fields.
   367  // Partial Channel objects only have id, name, type and permissions fields.
   368  type ApplicationCommandInteractionDataResolved struct {
   369  	Users       map[string]*User              `json:"users"`
   370  	Members     map[string]*Member            `json:"members"`
   371  	Roles       map[string]*Role              `json:"roles"`
   372  	Channels    map[string]*Channel           `json:"channels"`
   373  	Messages    map[string]*Message           `json:"messages"`
   374  	Attachments map[string]*MessageAttachment `json:"attachments"`
   375  }
   376  
   377  // Type returns the type of interaction data.
   378  func (ApplicationCommandInteractionData) Type() InteractionType {
   379  	return InteractionApplicationCommand
   380  }
   381  
   382  // MessageComponentInteractionData contains the data of message component interaction.
   383  type MessageComponentInteractionData struct {
   384  	CustomID      string                                  `json:"custom_id"`
   385  	ComponentType ComponentType                           `json:"component_type"`
   386  	Resolved      MessageComponentInteractionDataResolved `json:"resolved"`
   387  
   388  	// NOTE: Only filled when ComponentType is SelectMenuComponent (3). Otherwise is nil.
   389  	Values []string `json:"values"`
   390  }
   391  
   392  // MessageComponentInteractionDataResolved contains the resolved data of selected option.
   393  type MessageComponentInteractionDataResolved struct {
   394  	Users    map[string]*User    `json:"users"`
   395  	Members  map[string]*Member  `json:"members"`
   396  	Roles    map[string]*Role    `json:"roles"`
   397  	Channels map[string]*Channel `json:"channels"`
   398  }
   399  
   400  // Type returns the type of interaction data.
   401  func (MessageComponentInteractionData) Type() InteractionType {
   402  	return InteractionMessageComponent
   403  }
   404  
   405  // ModalSubmitInteractionData contains the data of modal submit interaction.
   406  type ModalSubmitInteractionData struct {
   407  	CustomID   string             `json:"custom_id"`
   408  	Components []MessageComponent `json:"-"`
   409  }
   410  
   411  // Type returns the type of interaction data.
   412  func (ModalSubmitInteractionData) Type() InteractionType {
   413  	return InteractionModalSubmit
   414  }
   415  
   416  // UnmarshalJSON is a helper function to correctly unmarshal Components.
   417  func (d *ModalSubmitInteractionData) UnmarshalJSON(data []byte) error {
   418  	type modalSubmitInteractionData ModalSubmitInteractionData
   419  	var v struct {
   420  		modalSubmitInteractionData
   421  		RawComponents []unmarshalableMessageComponent `json:"components"`
   422  	}
   423  	err := json.Unmarshal(data, &v)
   424  	if err != nil {
   425  		return err
   426  	}
   427  	*d = ModalSubmitInteractionData(v.modalSubmitInteractionData)
   428  	d.Components = make([]MessageComponent, len(v.RawComponents))
   429  	for i, v := range v.RawComponents {
   430  		d.Components[i] = v.MessageComponent
   431  	}
   432  	return err
   433  }
   434  
   435  // ApplicationCommandInteractionDataOption represents an option of a slash command.
   436  type ApplicationCommandInteractionDataOption struct {
   437  	Name string                       `json:"name"`
   438  	Type ApplicationCommandOptionType `json:"type"`
   439  	// NOTE: Contains the value specified by Type.
   440  	Value   interface{}                                `json:"value,omitempty"`
   441  	Options []*ApplicationCommandInteractionDataOption `json:"options,omitempty"`
   442  
   443  	// NOTE: autocomplete interaction only.
   444  	Focused bool `json:"focused,omitempty"`
   445  }
   446  
   447  // GetOption finds and returns an application command option by its name.
   448  func (o ApplicationCommandInteractionDataOption) GetOption(name string) (option *ApplicationCommandInteractionDataOption) {
   449  	for _, opt := range o.Options {
   450  		if opt.Name == name {
   451  			option = opt
   452  			break
   453  		}
   454  	}
   455  
   456  	return
   457  }
   458  
   459  // IntValue is a utility function for casting option value to integer
   460  func (o ApplicationCommandInteractionDataOption) IntValue() int64 {
   461  	if o.Type != ApplicationCommandOptionInteger {
   462  		panic("IntValue called on data option of type " + o.Type.String())
   463  	}
   464  	return int64(o.Value.(float64))
   465  }
   466  
   467  // UintValue is a utility function for casting option value to unsigned integer
   468  func (o ApplicationCommandInteractionDataOption) UintValue() uint64 {
   469  	if o.Type != ApplicationCommandOptionInteger {
   470  		panic("UintValue called on data option of type " + o.Type.String())
   471  	}
   472  	return uint64(o.Value.(float64))
   473  }
   474  
   475  // FloatValue is a utility function for casting option value to float
   476  func (o ApplicationCommandInteractionDataOption) FloatValue() float64 {
   477  	if o.Type != ApplicationCommandOptionNumber {
   478  		panic("FloatValue called on data option of type " + o.Type.String())
   479  	}
   480  	return o.Value.(float64)
   481  }
   482  
   483  // StringValue is a utility function for casting option value to string
   484  func (o ApplicationCommandInteractionDataOption) StringValue() string {
   485  	if o.Type != ApplicationCommandOptionString {
   486  		panic("StringValue called on data option of type " + o.Type.String())
   487  	}
   488  	return o.Value.(string)
   489  }
   490  
   491  // BoolValue is a utility function for casting option value to bool
   492  func (o ApplicationCommandInteractionDataOption) BoolValue() bool {
   493  	if o.Type != ApplicationCommandOptionBoolean {
   494  		panic("BoolValue called on data option of type " + o.Type.String())
   495  	}
   496  	return o.Value.(bool)
   497  }
   498  
   499  // ChannelValue is a utility function for casting option value to channel object.
   500  // s : Session object, if not nil, function additionally fetches all channel's data
   501  func (o ApplicationCommandInteractionDataOption) ChannelValue(s *Session) *Channel {
   502  	if o.Type != ApplicationCommandOptionChannel {
   503  		panic("ChannelValue called on data option of type " + o.Type.String())
   504  	}
   505  	chanID := o.Value.(string)
   506  
   507  	if s == nil {
   508  		return &Channel{ID: chanID}
   509  	}
   510  
   511  	ch, err := s.State.Channel(chanID)
   512  	if err != nil {
   513  		ch, err = s.Channel(chanID)
   514  		if err != nil {
   515  			return &Channel{ID: chanID}
   516  		}
   517  	}
   518  
   519  	return ch
   520  }
   521  
   522  // RoleValue is a utility function for casting option value to role object.
   523  // s : Session object, if not nil, function additionally fetches all role's data
   524  func (o ApplicationCommandInteractionDataOption) RoleValue(s *Session, gID string) *Role {
   525  	if o.Type != ApplicationCommandOptionRole && o.Type != ApplicationCommandOptionMentionable {
   526  		panic("RoleValue called on data option of type " + o.Type.String())
   527  	}
   528  	roleID := o.Value.(string)
   529  
   530  	if s == nil || gID == "" {
   531  		return &Role{ID: roleID}
   532  	}
   533  
   534  	r, err := s.State.Role(gID, roleID)
   535  	if err != nil {
   536  		roles, err := s.GuildRoles(gID)
   537  		if err == nil {
   538  			for _, r = range roles {
   539  				if r.ID == roleID {
   540  					return r
   541  				}
   542  			}
   543  		}
   544  		return &Role{ID: roleID}
   545  	}
   546  
   547  	return r
   548  }
   549  
   550  // UserValue is a utility function for casting option value to user object.
   551  // s : Session object, if not nil, function additionally fetches all user's data
   552  func (o ApplicationCommandInteractionDataOption) UserValue(s *Session) *User {
   553  	if o.Type != ApplicationCommandOptionUser && o.Type != ApplicationCommandOptionMentionable {
   554  		panic("UserValue called on data option of type " + o.Type.String())
   555  	}
   556  	userID := o.Value.(string)
   557  
   558  	if s == nil {
   559  		return &User{ID: userID}
   560  	}
   561  
   562  	u, err := s.User(userID)
   563  	if err != nil {
   564  		return &User{ID: userID}
   565  	}
   566  
   567  	return u
   568  }
   569  
   570  // InteractionResponseType is type of interaction response.
   571  type InteractionResponseType uint8
   572  
   573  // Interaction response types.
   574  const (
   575  	// InteractionResponsePong is for ACK ping event.
   576  	InteractionResponsePong InteractionResponseType = 1
   577  	// InteractionResponseChannelMessageWithSource is for responding with a message, showing the user's input.
   578  	InteractionResponseChannelMessageWithSource InteractionResponseType = 4
   579  	// InteractionResponseDeferredChannelMessageWithSource acknowledges that the event was received, and that a follow-up will come later.
   580  	InteractionResponseDeferredChannelMessageWithSource InteractionResponseType = 5
   581  	// InteractionResponseDeferredMessageUpdate acknowledges that the message component interaction event was received, and message will be updated later.
   582  	InteractionResponseDeferredMessageUpdate InteractionResponseType = 6
   583  	// InteractionResponseUpdateMessage is for updating the message to which message component was attached.
   584  	InteractionResponseUpdateMessage InteractionResponseType = 7
   585  	// InteractionApplicationCommandAutocompleteResult shows autocompletion results. Autocomplete interaction only.
   586  	InteractionApplicationCommandAutocompleteResult InteractionResponseType = 8
   587  	// InteractionResponseModal is for responding to an interaction with a modal window.
   588  	InteractionResponseModal InteractionResponseType = 9
   589  )
   590  
   591  // InteractionResponse represents a response for an interaction event.
   592  type InteractionResponse struct {
   593  	Type InteractionResponseType  `json:"type,omitempty"`
   594  	Data *InteractionResponseData `json:"data,omitempty"`
   595  }
   596  
   597  // InteractionResponseData is response data for an interaction.
   598  type InteractionResponseData struct {
   599  	TTS             bool                    `json:"tts"`
   600  	Content         string                  `json:"content"`
   601  	Components      []MessageComponent      `json:"components"`
   602  	Embeds          []*MessageEmbed         `json:"embeds"`
   603  	AllowedMentions *MessageAllowedMentions `json:"allowed_mentions,omitempty"`
   604  	Files           []*File                 `json:"-"`
   605  	Attachments     *[]*MessageAttachment   `json:"attachments,omitempty"`
   606  	Poll            *Poll                   `json:"poll,omitempty"`
   607  
   608  	// NOTE: only MessageFlagsSuppressEmbeds and MessageFlagsEphemeral can be set.
   609  	Flags MessageFlags `json:"flags,omitempty"`
   610  
   611  	// NOTE: autocomplete interaction only.
   612  	Choices []*ApplicationCommandOptionChoice `json:"choices,omitempty"`
   613  
   614  	// NOTE: modal interaction only.
   615  
   616  	CustomID string `json:"custom_id,omitempty"`
   617  	Title    string `json:"title,omitempty"`
   618  }
   619  
   620  // VerifyInteraction implements message verification of the discord interactions api
   621  // signing algorithm, as documented here:
   622  // https://discord.com/developers/docs/interactions/receiving-and-responding#security-and-authorization
   623  func VerifyInteraction(r *http.Request, key ed25519.PublicKey) bool {
   624  	var msg bytes.Buffer
   625  
   626  	signature := r.Header.Get("X-Signature-Ed25519")
   627  	if signature == "" {
   628  		return false
   629  	}
   630  
   631  	sig, err := hex.DecodeString(signature)
   632  	if err != nil {
   633  		return false
   634  	}
   635  
   636  	if len(sig) != ed25519.SignatureSize {
   637  		return false
   638  	}
   639  
   640  	timestamp := r.Header.Get("X-Signature-Timestamp")
   641  	if timestamp == "" {
   642  		return false
   643  	}
   644  
   645  	msg.WriteString(timestamp)
   646  
   647  	defer r.Body.Close()
   648  	var body bytes.Buffer
   649  
   650  	// at the end of the function, copy the original body back into the request
   651  	defer func() {
   652  		r.Body = ioutil.NopCloser(&body)
   653  	}()
   654  
   655  	// copy body into buffers
   656  	_, err = io.Copy(&msg, io.TeeReader(r.Body, &body))
   657  	if err != nil {
   658  		return false
   659  	}
   660  
   661  	return ed25519.Verify(key, msg.Bytes(), sig)
   662  }