oauth2.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 = 1
  16. MembershipStateAccepted MembershipState = 2
  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. // Application returns an Application structure of a specific Application
  35. // appID : The ID of an Application
  36. func (s *Session) Application(appID string) (st *Application, err error) {
  37. body, err := s.RequestWithBucketID("GET", EndpointOAuth2Application(appID), nil, EndpointOAuth2Application(""))
  38. if err != nil {
  39. return
  40. }
  41. err = unmarshal(body, &st)
  42. return
  43. }
  44. // Applications returns all applications for the authenticated user
  45. func (s *Session) Applications() (st []*Application, err error) {
  46. body, err := s.RequestWithBucketID("GET", EndpointOAuth2Applications, nil, EndpointOAuth2Applications)
  47. if err != nil {
  48. return
  49. }
  50. err = unmarshal(body, &st)
  51. return
  52. }
  53. // ApplicationCreate creates a new Application
  54. // name : Name of Application / Bot
  55. // uris : Redirect URIs (Not required)
  56. func (s *Session) ApplicationCreate(ap *Application) (st *Application, err error) {
  57. data := struct {
  58. Name string `json:"name"`
  59. Description string `json:"description"`
  60. }{ap.Name, ap.Description}
  61. body, err := s.RequestWithBucketID("POST", EndpointOAuth2Applications, data, EndpointOAuth2Applications)
  62. if err != nil {
  63. return
  64. }
  65. err = unmarshal(body, &st)
  66. return
  67. }
  68. // ApplicationUpdate updates an existing Application
  69. // var : desc
  70. func (s *Session) ApplicationUpdate(appID string, ap *Application) (st *Application, err error) {
  71. data := struct {
  72. Name string `json:"name"`
  73. Description string `json:"description"`
  74. }{ap.Name, ap.Description}
  75. body, err := s.RequestWithBucketID("PUT", EndpointOAuth2Application(appID), data, EndpointOAuth2Application(""))
  76. if err != nil {
  77. return
  78. }
  79. err = unmarshal(body, &st)
  80. return
  81. }
  82. // ApplicationDelete deletes an existing Application
  83. // appID : The ID of an Application
  84. func (s *Session) ApplicationDelete(appID string) (err error) {
  85. _, err = s.RequestWithBucketID("DELETE", EndpointOAuth2Application(appID), nil, EndpointOAuth2Application(""))
  86. if err != nil {
  87. return
  88. }
  89. return
  90. }
  91. // Asset struct stores values for an asset of an application
  92. type Asset struct {
  93. Type int `json:"type"`
  94. ID string `json:"id"`
  95. Name string `json:"name"`
  96. }
  97. // ApplicationAssets returns an application's assets
  98. func (s *Session) ApplicationAssets(appID string) (ass []*Asset, err error) {
  99. body, err := s.RequestWithBucketID("GET", EndpointOAuth2ApplicationAssets(appID), nil, EndpointOAuth2ApplicationAssets(""))
  100. if err != nil {
  101. return
  102. }
  103. err = unmarshal(body, &ass)
  104. return
  105. }
  106. // ------------------------------------------------------------------------------------------------
  107. // Code specific to Discord OAuth2 Application Bots
  108. // ------------------------------------------------------------------------------------------------
  109. // ApplicationBotCreate creates an Application Bot Account
  110. //
  111. // appID : The ID of an Application
  112. //
  113. // NOTE: func name may change, if I can think up something better.
  114. func (s *Session) ApplicationBotCreate(appID string) (st *User, err error) {
  115. body, err := s.RequestWithBucketID("POST", EndpointOAuth2ApplicationsBot(appID), nil, EndpointOAuth2ApplicationsBot(""))
  116. if err != nil {
  117. return
  118. }
  119. err = unmarshal(body, &st)
  120. return
  121. }