123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- package discordgo
- import (
- "bytes"
- "crypto/ed25519"
- "encoding/hex"
- "encoding/json"
- "fmt"
- "io"
- "io/ioutil"
- "net/http"
- "time"
- )
- const InteractionDeadline = time.Second * 3
- type ApplicationCommandType uint8
- const (
-
- ChatApplicationCommand ApplicationCommandType = 1
-
- UserApplicationCommand ApplicationCommandType = 2
-
- MessageApplicationCommand ApplicationCommandType = 3
- )
- type ApplicationCommand struct {
- ID string `json:"id,omitempty"`
- ApplicationID string `json:"application_id,omitempty"`
- Type ApplicationCommandType `json:"type,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 = 1
- ApplicationCommandOptionSubCommandGroup ApplicationCommandOptionType = 2
- ApplicationCommandOptionString ApplicationCommandOptionType = 3
- ApplicationCommandOptionInteger ApplicationCommandOptionType = 4
- ApplicationCommandOptionBoolean ApplicationCommandOptionType = 5
- ApplicationCommandOptionUser ApplicationCommandOptionType = 6
- ApplicationCommandOptionChannel ApplicationCommandOptionType = 7
- ApplicationCommandOptionRole ApplicationCommandOptionType = 8
- ApplicationCommandOptionMentionable ApplicationCommandOptionType = 9
- )
- func (t ApplicationCommandOptionType) String() string {
- switch t {
- case ApplicationCommandOptionSubCommand:
- return "SubCommand"
- case ApplicationCommandOptionSubCommandGroup:
- return "SubCommandGroup"
- case ApplicationCommandOptionString:
- return "String"
- case ApplicationCommandOptionInteger:
- return "Integer"
- case ApplicationCommandOptionBoolean:
- return "Boolean"
- case ApplicationCommandOptionUser:
- return "User"
- case ApplicationCommandOptionChannel:
- return "Channel"
- case ApplicationCommandOptionRole:
- return "Role"
- case ApplicationCommandOptionMentionable:
- return "Mentionable"
- }
- return fmt.Sprintf("ApplicationCommandOptionType(%d)", t)
- }
- 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"`
- ChannelTypes []ChannelType `json:"channel_types"`
- }
- type ApplicationCommandOptionChoice struct {
- Name string `json:"name"`
- Value interface{} `json:"value"`
- }
- type InteractionType uint8
- const (
- InteractionPing InteractionType = 1
- InteractionApplicationCommand InteractionType = 2
- InteractionMessageComponent InteractionType = 3
- )
- func (t InteractionType) String() string {
- switch t {
- case InteractionPing:
- return "Ping"
- case InteractionApplicationCommand:
- return "ApplicationCommand"
- case InteractionMessageComponent:
- return "MessageComponent"
- }
- return fmt.Sprintf("InteractionType(%d)", t)
- }
- type Interaction struct {
- ID string `json:"id"`
- Type InteractionType `json:"type"`
- Data InteractionData `json:"-"`
- GuildID string `json:"guild_id"`
- ChannelID string `json:"channel_id"`
-
-
- Message *Message `json:"message"`
-
-
-
-
- Member *Member `json:"member"`
-
-
-
-
- User *User `json:"user"`
- Token string `json:"token"`
- Version int `json:"version"`
- }
- type interaction Interaction
- type rawInteraction struct {
- interaction
- Data json.RawMessage `json:"data"`
- }
- func (i *Interaction) UnmarshalJSON(raw []byte) error {
- var tmp rawInteraction
- err := json.Unmarshal(raw, &tmp)
- if err != nil {
- return err
- }
- *i = Interaction(tmp.interaction)
- switch tmp.Type {
- case InteractionApplicationCommand:
- v := ApplicationCommandInteractionData{}
- err = json.Unmarshal(tmp.Data, &v)
- if err != nil {
- return err
- }
- i.Data = v
- case InteractionMessageComponent:
- v := MessageComponentInteractionData{}
- err = json.Unmarshal(tmp.Data, &v)
- if err != nil {
- return err
- }
- i.Data = v
- }
- return nil
- }
- func (i Interaction) MessageComponentData() (data MessageComponentInteractionData) {
- if i.Type != InteractionMessageComponent {
- panic("MessageComponentData called on interaction of type " + i.Type.String())
- }
- return i.Data.(MessageComponentInteractionData)
- }
- func (i Interaction) ApplicationCommandData() (data ApplicationCommandInteractionData) {
- if i.Type != InteractionApplicationCommand {
- panic("ApplicationCommandData called on interaction of type " + i.Type.String())
- }
- return i.Data.(ApplicationCommandInteractionData)
- }
- type InteractionData interface {
- Type() InteractionType
- }
- type ApplicationCommandInteractionData struct {
- ID string `json:"id"`
- Name string `json:"name"`
- Resolved *ApplicationCommandInteractionDataResolved `json:"resolved"`
-
- Options []*ApplicationCommandInteractionDataOption `json:"options"`
-
-
- TargetID string `json:"target_id"`
- }
- type ApplicationCommandInteractionDataResolved struct {
- Users map[string]*User `json:"users"`
- Members map[string]*Member `json:"members"`
- Roles map[string]*Role `json:"roles"`
- Channels map[string]*Channel `json:"channels"`
- Messages map[string]*Message `json:"messages"`
- }
- func (ApplicationCommandInteractionData) Type() InteractionType {
- return InteractionApplicationCommand
- }
- type MessageComponentInteractionData struct {
- CustomID string `json:"custom_id"`
- ComponentType ComponentType `json:"component_type"`
-
- Values []string `json:"values"`
- }
- func (MessageComponentInteractionData) Type() InteractionType {
- return InteractionMessageComponent
- }
- type ApplicationCommandInteractionDataOption struct {
- Name string `json:"name"`
- Type ApplicationCommandOptionType `json:"type"`
-
- Value interface{} `json:"value,omitempty"`
- Options []*ApplicationCommandInteractionDataOption `json:"options,omitempty"`
- }
- func (o ApplicationCommandInteractionDataOption) IntValue() int64 {
- if o.Type != ApplicationCommandOptionInteger {
- panic("IntValue called on data option of type " + o.Type.String())
- }
- return int64(o.Value.(float64))
- }
- func (o ApplicationCommandInteractionDataOption) UintValue() uint64 {
- if o.Type != ApplicationCommandOptionInteger {
- panic("UintValue called on data option of type " + o.Type.String())
- }
- return uint64(o.Value.(float64))
- }
- func (o ApplicationCommandInteractionDataOption) FloatValue() float64 {
-
- if v, ok := o.Value.(float64); ok {
- return v
- }
- return 0.0
- }
- func (o ApplicationCommandInteractionDataOption) StringValue() string {
- if o.Type != ApplicationCommandOptionString {
- panic("StringValue called on data option of type " + o.Type.String())
- }
- return o.Value.(string)
- }
- func (o ApplicationCommandInteractionDataOption) BoolValue() bool {
- if o.Type != ApplicationCommandOptionBoolean {
- panic("BoolValue called on data option of type " + o.Type.String())
- }
- return o.Value.(bool)
- }
- func (o ApplicationCommandInteractionDataOption) ChannelValue(s *Session) *Channel {
- if o.Type != ApplicationCommandOptionChannel {
- panic("ChannelValue called on data option of type " + o.Type.String())
- }
- chanID := o.Value.(string)
- 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 {
- if o.Type != ApplicationCommandOptionRole && o.Type != ApplicationCommandOptionMentionable {
- panic("RoleValue called on data option of type " + o.Type.String())
- }
- roleID := o.Value.(string)
- 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 {
- if o.Type != ApplicationCommandOptionUser && o.Type != ApplicationCommandOptionMentionable {
- panic("UserValue called on data option of type " + o.Type.String())
- }
- userID := o.Value.(string)
- 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 = 1
-
- InteractionResponseChannelMessageWithSource InteractionResponseType = 4
-
- InteractionResponseDeferredChannelMessageWithSource InteractionResponseType = 5
-
- InteractionResponseDeferredMessageUpdate InteractionResponseType = 6
-
- InteractionResponseUpdateMessage InteractionResponseType = 7
- )
- type InteractionResponse struct {
- Type InteractionResponseType `json:"type,omitempty"`
- Data *InteractionResponseData `json:"data,omitempty"`
- }
- type InteractionResponseData struct {
- TTS bool `json:"tts"`
- Content string `json:"content"`
- Components []MessageComponent `json:"components"`
- Embeds []*MessageEmbed `json:"embeds,omitempty"`
- AllowedMentions *MessageAllowedMentions `json:"allowed_mentions,omitempty"`
- Flags uint64 `json:"flags,omitempty"`
- Files []*File `json:"-"`
- }
- 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)
- }
|