oauth2.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. // An Application struct stores values for a Discord OAuth2 Application
  12. type Application struct {
  13. ID string `json:"id,omitempty"`
  14. Name string `json:"name"`
  15. Description string `json:"description,omitempty"`
  16. Icon string `json:"icon,omitempty"`
  17. Secret string `json:"secret,omitempty"`
  18. RedirectURIs *[]string `json:"redirect_uris,omitempty"`
  19. }
  20. // Application returns an Application structure of a specific Application
  21. // appID : The ID of an Application
  22. func (s *Session) Application(appID string) (st *Application, err error) {
  23. body, err := s.Request("GET", APPLICATION(appID), nil)
  24. if err != nil {
  25. return
  26. }
  27. err = unmarshal(body, &st)
  28. return
  29. }
  30. // Applications returns all applications for the authenticated user
  31. func (s *Session) Applications() (st []*Application, err error) {
  32. body, err := s.Request("GET", APPLICATIONS, nil)
  33. if err != nil {
  34. return
  35. }
  36. err = unmarshal(body, &st)
  37. return
  38. }
  39. // ApplicationCreate creates a new Application
  40. // name : Name of Application / Bot
  41. // uris : Redirect URIs (Not required)
  42. func (s *Session) ApplicationCreate(ap *Application) (st *Application, err error) {
  43. data := struct {
  44. Name string `json:"name"`
  45. Description string `json:"description"`
  46. RedirectURIs *[]string `json:"redirect_uris,omitempty"`
  47. }{ap.Name, ap.Description, ap.RedirectURIs}
  48. body, err := s.Request("POST", APPLICATIONS, data)
  49. if err != nil {
  50. return
  51. }
  52. err = unmarshal(body, &st)
  53. return
  54. }
  55. // ApplicationUpdate updates an existing Application
  56. // var : desc
  57. func (s *Session) ApplicationUpdate(appID string, ap *Application) (st *Application, err error) {
  58. data := struct {
  59. Name string `json:"name"`
  60. Description string `json:"description"`
  61. RedirectURIs *[]string `json:"redirect_uris,omitempty"`
  62. }{ap.Name, ap.Description, ap.RedirectURIs}
  63. body, err := s.Request("PUT", APPLICATION(appID), data)
  64. if err != nil {
  65. return
  66. }
  67. err = unmarshal(body, &st)
  68. return
  69. }
  70. // ApplicationDelete deletes an existing Application
  71. // appID : The ID of an Application
  72. func (s *Session) ApplicationDelete(appID string) (err error) {
  73. _, err = s.Request("DELETE", APPLICATION(appID), nil)
  74. if err != nil {
  75. return
  76. }
  77. return
  78. }
  79. // ------------------------------------------------------------------------------------------------
  80. // Code specific to Discord OAuth2 Application Bots
  81. // ------------------------------------------------------------------------------------------------
  82. // ApplicationBotCreate creates an Application Bot Account
  83. //
  84. // appID : The ID of an Application
  85. // token : The authentication Token for a user account to convert into
  86. // a bot account. This is optional, if omited a new account
  87. // is created using the name of the application.
  88. //
  89. // NOTE: func name may change, if I can think up something better.
  90. func (s *Session) ApplicationBotCreate(appID, token string) (st *User, err error) {
  91. data := struct {
  92. Token string `json:"token,omitempty"`
  93. }{token}
  94. body, err := s.Request("POST", APPLICATIONS_BOT(appID), data)
  95. if err != nil {
  96. return
  97. }
  98. err = unmarshal(body, &st)
  99. return
  100. }