oauth2.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Discordgo - Discord bindings for Go
  2. // Available at https://github.com/bwmarrin/discordgo
  3. // Copyright 2015-2016 Bruce Marriner <bruce@sqls.net>. All rights reserved.
  4. // Use of this source code is governed by a BSD-style
  5. // license that can be found in the LICENSE file.
  6. // This file contains functions related to Discord OAuth2 endpoints
  7. package discordgo
  8. // ------------------------------------------------------------------------------------------------
  9. // Code specific to Discord OAuth2 Applications
  10. // ------------------------------------------------------------------------------------------------
  11. // The MembershipState represents whether the user is in the team or has been invited into it
  12. type MembershipState int
  13. // Constants for the different stages of the MembershipState
  14. const (
  15. MembershipStateInvited MembershipState = iota + 1
  16. MembershipStateAccepted
  17. )
  18. // A TeamMember struct stores values for a single Team Member, extending the normal User data - note that the user field is partial
  19. type TeamMember struct {
  20. User *User `json:"user"`
  21. TeamID string `json:"team_id"`
  22. MembershipState MembershipState `json:"membership_state"`
  23. Permissions []string `json:"permissions"`
  24. }
  25. // A Team struct stores the members of a Discord Developer Team as well as some metadata about it
  26. type Team struct {
  27. ID string `json:"id"`
  28. Name string `json:"name"`
  29. Description string `json:"description"`
  30. Icon string `json:"icon"`
  31. OwnerID string `json:"owner_user_id"`
  32. Members []*TeamMember `json:"members"`
  33. }
  34. // An Application struct stores values for a Discord OAuth2 Application
  35. type Application struct {
  36. ID string `json:"id,omitempty"`
  37. Name string `json:"name"`
  38. Description string `json:"description,omitempty"`
  39. Icon string `json:"icon,omitempty"`
  40. Secret string `json:"secret,omitempty"`
  41. RedirectURIs *[]string `json:"redirect_uris,omitempty"`
  42. BotRequireCodeGrant bool `json:"bot_require_code_grant,omitempty"`
  43. BotPublic bool `json:"bot_public,omitempty"`
  44. RPCApplicationState int `json:"rpc_application_state,omitempty"`
  45. Flags int `json:"flags,omitempty"`
  46. Owner *User `json:"owner"`
  47. Bot *User `json:"bot"`
  48. Team *Team `json:"team"`
  49. }
  50. // Application returns an Application structure of a specific Application
  51. // appID : The ID of an Application
  52. func (s *Session) Application(appID string) (st *Application, err error) {
  53. body, err := s.RequestWithBucketID("GET", EndpointApplication(appID), nil, EndpointApplication(""))
  54. if err != nil {
  55. return
  56. }
  57. err = unmarshal(body, &st)
  58. return
  59. }
  60. // Applications returns all applications for the authenticated user
  61. func (s *Session) Applications() (st []*Application, err error) {
  62. body, err := s.RequestWithBucketID("GET", EndpointApplications, nil, EndpointApplications)
  63. if err != nil {
  64. return
  65. }
  66. err = unmarshal(body, &st)
  67. return
  68. }
  69. // ApplicationCreate creates a new Application
  70. // name : Name of Application / Bot
  71. // uris : Redirect URIs (Not required)
  72. func (s *Session) ApplicationCreate(ap *Application) (st *Application, err error) {
  73. data := struct {
  74. Name string `json:"name"`
  75. Description string `json:"description"`
  76. RedirectURIs *[]string `json:"redirect_uris,omitempty"`
  77. }{ap.Name, ap.Description, ap.RedirectURIs}
  78. body, err := s.RequestWithBucketID("POST", EndpointApplications, data, EndpointApplications)
  79. if err != nil {
  80. return
  81. }
  82. err = unmarshal(body, &st)
  83. return
  84. }
  85. // ApplicationUpdate updates an existing Application
  86. // var : desc
  87. func (s *Session) ApplicationUpdate(appID string, ap *Application) (st *Application, err error) {
  88. data := struct {
  89. Name string `json:"name"`
  90. Description string `json:"description"`
  91. RedirectURIs *[]string `json:"redirect_uris,omitempty"`
  92. }{ap.Name, ap.Description, ap.RedirectURIs}
  93. body, err := s.RequestWithBucketID("PUT", EndpointApplication(appID), data, EndpointApplication(""))
  94. if err != nil {
  95. return
  96. }
  97. err = unmarshal(body, &st)
  98. return
  99. }
  100. // ApplicationDelete deletes an existing Application
  101. // appID : The ID of an Application
  102. func (s *Session) ApplicationDelete(appID string) (err error) {
  103. _, err = s.RequestWithBucketID("DELETE", EndpointApplication(appID), nil, EndpointApplication(""))
  104. if err != nil {
  105. return
  106. }
  107. return
  108. }
  109. // Asset struct stores values for an asset of an application
  110. type Asset struct {
  111. Type int `json:"type"`
  112. ID string `json:"id"`
  113. Name string `json:"name"`
  114. }
  115. // ApplicationAssets returns an application's assets
  116. func (s *Session) ApplicationAssets(appID string) (ass []*Asset, err error) {
  117. body, err := s.RequestWithBucketID("GET", EndpointApplicationAssets(appID), nil, EndpointApplicationAssets(""))
  118. if err != nil {
  119. return
  120. }
  121. err = unmarshal(body, &ass)
  122. return
  123. }
  124. // ------------------------------------------------------------------------------------------------
  125. // Code specific to Discord OAuth2 Application Bots
  126. // ------------------------------------------------------------------------------------------------
  127. // ApplicationBotCreate creates an Application Bot Account
  128. //
  129. // appID : The ID of an Application
  130. //
  131. // NOTE: func name may change, if I can think up something better.
  132. func (s *Session) ApplicationBotCreate(appID string) (st *User, err error) {
  133. body, err := s.RequestWithBucketID("POST", EndpointApplicationsBot(appID), nil, EndpointApplicationsBot(""))
  134. if err != nil {
  135. return
  136. }
  137. err = unmarshal(body, &st)
  138. return
  139. }