oauth2.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. Owner *User `json:"owner"`
  20. }
  21. // Application returns an Application structure of a specific Application
  22. // appID : The ID of an Application
  23. func (s *Session) Application(appID string) (st *Application, err error) {
  24. body, err := s.RequestWithBucketID("GET", EndpointApplication(appID), nil, EndpointApplication(""))
  25. if err != nil {
  26. return
  27. }
  28. err = unmarshal(body, &st)
  29. return
  30. }
  31. // Applications returns all applications for the authenticated user
  32. func (s *Session) Applications() (st []*Application, err error) {
  33. body, err := s.RequestWithBucketID("GET", EndpointApplications, nil, EndpointApplications)
  34. if err != nil {
  35. return
  36. }
  37. err = unmarshal(body, &st)
  38. return
  39. }
  40. // ApplicationCreate creates a new Application
  41. // name : Name of Application / Bot
  42. // uris : Redirect URIs (Not required)
  43. func (s *Session) ApplicationCreate(ap *Application) (st *Application, err error) {
  44. data := struct {
  45. Name string `json:"name"`
  46. Description string `json:"description"`
  47. RedirectURIs *[]string `json:"redirect_uris,omitempty"`
  48. }{ap.Name, ap.Description, ap.RedirectURIs}
  49. body, err := s.RequestWithBucketID("POST", EndpointApplications, data, EndpointApplications)
  50. if err != nil {
  51. return
  52. }
  53. err = unmarshal(body, &st)
  54. return
  55. }
  56. // ApplicationUpdate updates an existing Application
  57. // var : desc
  58. func (s *Session) ApplicationUpdate(appID string, ap *Application) (st *Application, err error) {
  59. data := struct {
  60. Name string `json:"name"`
  61. Description string `json:"description"`
  62. RedirectURIs *[]string `json:"redirect_uris,omitempty"`
  63. }{ap.Name, ap.Description, ap.RedirectURIs}
  64. body, err := s.RequestWithBucketID("PUT", EndpointApplication(appID), data, EndpointApplication(""))
  65. if err != nil {
  66. return
  67. }
  68. err = unmarshal(body, &st)
  69. return
  70. }
  71. // ApplicationDelete deletes an existing Application
  72. // appID : The ID of an Application
  73. func (s *Session) ApplicationDelete(appID string) (err error) {
  74. _, err = s.RequestWithBucketID("DELETE", EndpointApplication(appID), nil, EndpointApplication(""))
  75. if err != nil {
  76. return
  77. }
  78. return
  79. }
  80. // ------------------------------------------------------------------------------------------------
  81. // Code specific to Discord OAuth2 Application Bots
  82. // ------------------------------------------------------------------------------------------------
  83. // ApplicationBotCreate creates an Application Bot Account
  84. //
  85. // appID : The ID of an Application
  86. //
  87. // NOTE: func name may change, if I can think up something better.
  88. func (s *Session) ApplicationBotCreate(appID string) (st *User, err error) {
  89. body, err := s.RequestWithBucketID("POST", EndpointApplicationsBot(appID), nil, EndpointApplicationsBot(""))
  90. if err != nil {
  91. return
  92. }
  93. err = unmarshal(body, &st)
  94. return
  95. }