oauth2.go 4.1 KB

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