oauth2.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. import (
  9. "fmt"
  10. )
  11. // ------------------------------------------------------------------------------------------------
  12. // Code specific to Discord OAuth2 Applications
  13. // ------------------------------------------------------------------------------------------------
  14. // An Application struct stores values for a Discord OAuth2 Application
  15. type Application struct {
  16. ID string `json:"id,omitempty"`
  17. Name string `json:"name"`
  18. Description string `json:"description,omitempty"`
  19. Icon string `json:"icon,omitempty"`
  20. Secret string `json:"secret,omitempty"`
  21. RedirectURIs *[]string `json:"redirect_uris,omitempty"`
  22. // Concept.. almost guarenteed to be removed.
  23. // Imagine that it's just not even here at all.
  24. ses *Session
  25. }
  26. // Application returns an Application structure of a specific Application
  27. // appID : The ID of an Application
  28. func (s *Session) Application(appID string) (st *Application, err error) {
  29. body, err := s.Request("GET", APPLICATION(appID), nil)
  30. if err != nil {
  31. return
  32. }
  33. err = unmarshal(body, &st)
  34. st.ses = s
  35. return
  36. }
  37. // Applications returns all applications for the authenticated user
  38. func (s *Session) Applications() (st []*Application, err error) {
  39. body, err := s.Request("GET", APPLICATIONS, nil)
  40. if err != nil {
  41. return
  42. }
  43. err = unmarshal(body, &st)
  44. for k, _ := range st {
  45. st[k].ses = s
  46. }
  47. return
  48. // TODO ..
  49. }
  50. // ApplicationCreate creates a new Application
  51. // name : Name of Application / Bot
  52. // uris : Redirect URIs (Not required)
  53. func (s *Session) ApplicationCreate(ap *Application) (st *Application, err error) {
  54. data := struct {
  55. Name string `json:"name"`
  56. Description string `json:"description"`
  57. RedirectURIs *[]string `json:"redirect_uris,omitempty"`
  58. }{ap.Name, ap.Description, ap.RedirectURIs}
  59. body, err := s.Request("POST", APPLICATIONS, data)
  60. if err != nil {
  61. return
  62. }
  63. err = unmarshal(body, &st)
  64. st.ses = s
  65. return
  66. }
  67. // ApplicationEdit edits an existing Application
  68. // var : desc
  69. func (s *Session) ApplicationUpdate(appID string, ap *Application) (st *Application, err error) {
  70. data := struct {
  71. Name string `json:"name"`
  72. Description string `json:"description"`
  73. RedirectURIs *[]string `json:"redirect_uris,omitempty"`
  74. }{ap.Name, ap.Description, ap.RedirectURIs}
  75. body, err := s.Request("PUT", APPLICATION(appID), data)
  76. if err != nil {
  77. return
  78. }
  79. err = unmarshal(body, &st)
  80. st.ses = s
  81. return
  82. }
  83. // ApplicationDelete deletes an existing Application
  84. // appID : The ID of an Application
  85. func (s *Session) ApplicationDelete(appID string) (err error) {
  86. _, err = s.Request("DELETE", APPLICATION(appID), nil)
  87. if err != nil {
  88. return
  89. }
  90. return
  91. }
  92. //////////////////////////////////////////////////////////////////////////////
  93. // Below two functions are experimental ideas, they will absolutely change
  94. // one way or another and may be deleted entirely.
  95. // Delete is a concept helper function, may be removed.
  96. // this func depends on the Application.ses pointer
  97. // pointing to the Discord session that the application
  98. // came from. This "magic" makes some very very nice helper
  99. // functions possible.
  100. func (a *Application) Delete() (err error) {
  101. if a.ses == nil {
  102. return fmt.Errorf("ses is nil.")
  103. }
  104. return a.ses.ApplicationDelete(a.ID)
  105. }
  106. // Delete is a concept helper function, may be removed.
  107. // this one doesn't depend on the "magic" of adding the ses
  108. // pointer to each Application
  109. func (a *Application) DeleteB(s *Session) (err error) {
  110. return s.ApplicationDelete(a.ID)
  111. }
  112. // ------------------------------------------------------------------------------------------------
  113. // Code specific to Discord OAuth2 Application Bots
  114. // ------------------------------------------------------------------------------------------------
  115. // ApplicationBotCreate creates an Application Bot Account
  116. //
  117. // appID : The ID of an Application
  118. // token : The authentication Token for a user account to convert into
  119. // a bot account. This is optional, if omited a new account
  120. // is created using the name of the application.
  121. //
  122. // NOTE: func name may change, if I can think up something better.
  123. func (s *Session) ApplicationBotCreate(appID, token string) (st *User, err error) {
  124. data := struct {
  125. Token string `json:"token,omitempty"`
  126. }{token}
  127. body, err := s.Request("POST", APPLICATIONS_BOT(appID), data)
  128. if err != nil {
  129. return
  130. }
  131. err = unmarshal(body, &st)
  132. return
  133. }