123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- package discordgo
- import (
- "bytes"
- "crypto/ed25519"
- "encoding/hex"
- "io"
- "io/ioutil"
- "net/http"
- "time"
- )
- const InteractionDeadline = time.Second * 3
- type ApplicationCommand struct {
- ID string `json:"id"`
- ApplicationID string `json:"application_id,omitempty"`
- Name string `json:"name"`
- Description string `json:"description,omitempty"`
- Version string `json:"version,omitempty"`
- Options []*ApplicationCommandOption `json:"options"`
- }
- type ApplicationCommandOptionType uint8
- const (
- ApplicationCommandOptionSubCommand = ApplicationCommandOptionType(iota + 1)
- ApplicationCommandOptionSubCommandGroup
- ApplicationCommandOptionString
- ApplicationCommandOptionInteger
- ApplicationCommandOptionBoolean
- ApplicationCommandOptionUser
- ApplicationCommandOptionChannel
- ApplicationCommandOptionRole
- )
- type ApplicationCommandOption struct {
- Type ApplicationCommandOptionType `json:"type"`
- Name string `json:"name"`
- Description string `json:"description,omitempty"`
-
-
-
- Required bool `json:"required"`
- Choices []*ApplicationCommandOptionChoice `json:"choices"`
- Options []*ApplicationCommandOption `json:"options"`
- }
- type ApplicationCommandOptionChoice struct {
- Name string `json:"name"`
- Value interface{} `json:"value"`
- }
- type InteractionType uint8
- const (
- InteractionPing = InteractionType(iota + 1)
- InteractionApplicationCommand
- )
- type Interaction struct {
- ID string `json:"id"`
- Type InteractionType `json:"type"`
- Data ApplicationCommandInteractionData `json:"data"`
- GuildID string `json:"guild_id"`
- ChannelID string `json:"channel_id"`
-
-
-
-
- Member *Member `json:"member"`
-
-
-
-
- User *User `json:"user"`
- Token string `json:"token"`
- Version int `json:"version"`
- }
- type ApplicationCommandInteractionData struct {
- ID string `json:"id"`
- Name string `json:"name"`
- Options []*ApplicationCommandInteractionDataOption `json:"options"`
- }
- type ApplicationCommandInteractionDataOption struct {
- Name string `json:"name"`
-
- Value interface{} `json:"value,omitempty"`
- Options []*ApplicationCommandInteractionDataOption `json:"options,omitempty"`
- }
- func (o ApplicationCommandInteractionDataOption) IntValue() int64 {
- if v, ok := o.Value.(float64); ok {
- return int64(v)
- }
- return 0
- }
- func (o ApplicationCommandInteractionDataOption) UintValue() uint64 {
- if v, ok := o.Value.(float64); ok {
- return uint64(v)
- }
- return 0
- }
- func (o ApplicationCommandInteractionDataOption) FloatValue() float64 {
- if v, ok := o.Value.(float64); ok {
- return v
- }
- return 0.0
- }
- func (o ApplicationCommandInteractionDataOption) StringValue() string {
- if v, ok := o.Value.(string); ok {
- return v
- }
- return ""
- }
- func (o ApplicationCommandInteractionDataOption) BoolValue() bool {
- if v, ok := o.Value.(bool); ok {
- return v
- }
- return false
- }
- func (o ApplicationCommandInteractionDataOption) ChannelValue(s *Session) *Channel {
- chanID := o.StringValue()
- if chanID == "" {
- return nil
- }
- if s == nil {
- return &Channel{ID: chanID}
- }
- ch, err := s.State.Channel(chanID)
- if err != nil {
- ch, err = s.Channel(chanID)
- if err != nil {
- return &Channel{ID: chanID}
- }
- }
- return ch
- }
- func (o ApplicationCommandInteractionDataOption) RoleValue(s *Session, gID string) *Role {
- roleID := o.StringValue()
- if roleID == "" {
- return nil
- }
- if s == nil || gID == "" {
- return &Role{ID: roleID}
- }
- r, err := s.State.Role(roleID, gID)
- if err != nil {
- roles, err := s.GuildRoles(gID)
- if err == nil {
- for _, r = range roles {
- if r.ID == roleID {
- return r
- }
- }
- }
- return &Role{ID: roleID}
- }
- return r
- }
- func (o ApplicationCommandInteractionDataOption) UserValue(s *Session) *User {
- userID := o.StringValue()
- if userID == "" {
- return nil
- }
- if s == nil {
- return &User{ID: userID}
- }
- u, err := s.User(userID)
- if err != nil {
- return &User{ID: userID}
- }
- return u
- }
- type InteractionResponseType uint8
- const (
-
- InteractionResponsePong = InteractionResponseType(iota + 1)
-
-
- InteractionResponseAcknowledge
-
-
- InteractionResponseChannelMessage
-
- InteractionResponseChannelMessageWithSource
-
-
- InteractionResponseDeferredChannelMessageWithSource
- )
- type InteractionResponse struct {
- Type InteractionResponseType `json:"type,omitempty"`
- Data *InteractionApplicationCommandResponseData `json:"data,omitempty"`
- }
- type InteractionApplicationCommandResponseData struct {
- TTS bool `json:"tts,omitempty"`
- Content string `json:"content,omitempty"`
- Embeds []*MessageEmbed `json:"embeds,omitempty"`
- AllowedMentions *MessageAllowedMentions `json:"allowed_mentions,omitempty"`
-
- Flags uint64 `json:"flags,omitempty"`
- }
- func VerifyInteraction(r *http.Request, key ed25519.PublicKey) bool {
- var msg bytes.Buffer
- signature := r.Header.Get("X-Signature-Ed25519")
- if signature == "" {
- return false
- }
- sig, err := hex.DecodeString(signature)
- if err != nil {
- return false
- }
- if len(sig) != ed25519.SignatureSize {
- return false
- }
- timestamp := r.Header.Get("X-Signature-Timestamp")
- if timestamp == "" {
- return false
- }
- msg.WriteString(timestamp)
- defer r.Body.Close()
- var body bytes.Buffer
-
- defer func() {
- r.Body = ioutil.NopCloser(&body)
- }()
-
- _, err = io.Copy(&msg, io.TeeReader(r.Body, &body))
- if err != nil {
- return false
- }
- return ed25519.Verify(key, msg.Bytes(), sig)
- }
|