oauth2.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 applications
  7. package discordgo
  8. import (
  9. "fmt"
  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. // Concept.. almost guarenteed to be removed.
  20. // Imagine that it's just not even here at all.
  21. ses *Session
  22. }
  23. // Application returns an Application structure of a specific Application
  24. // appID : The ID of an Application
  25. func (s *Session) Application(appID string) (st *Application, err error) {
  26. body, err := s.Request("GET", APPLICATION(appID), nil)
  27. if err != nil {
  28. return
  29. }
  30. err = unmarshal(body, &st)
  31. st.ses = s
  32. return
  33. }
  34. // Applications returns all applications for the authenticated user
  35. func (s *Session) Applications() (st []*Application, err error) {
  36. body, err := s.Request("GET", APPLICATIONS, nil)
  37. if err != nil {
  38. return
  39. }
  40. err = unmarshal(body, &st)
  41. for k, _ := range st {
  42. st[k].ses = s
  43. }
  44. return
  45. // TODO ..
  46. }
  47. // ApplicationCreate creates a new Application
  48. // name : Name of Application / Bot
  49. // uris : Redirect URIs (Not required)
  50. func (s *Session) ApplicationCreate(ap *Application) (st *Application, err error) {
  51. data := struct {
  52. Name string `json:"name"`
  53. Description string `json:"description"`
  54. RedirectURIs *[]string `json:"redirect_uris,omitempty"`
  55. }{ap.Name, ap.Description, ap.RedirectURIs}
  56. body, err := s.Request("POST", APPLICATIONS, data)
  57. if err != nil {
  58. return
  59. }
  60. err = unmarshal(body, &st)
  61. st.ses = s
  62. return
  63. }
  64. // ApplicationEdit edits an existing Application
  65. // var : desc
  66. func (s *Session) ApplicationUpdate(appID string, ap *Application) (st *Application, err error) {
  67. data := struct {
  68. Name string `json:"name"`
  69. Description string `json:"description"`
  70. RedirectURIs *[]string `json:"redirect_uris,omitempty"`
  71. }{ap.Name, ap.Description, ap.RedirectURIs}
  72. body, err := s.Request("PUT", APPLICATION(appID), data)
  73. if err != nil {
  74. return
  75. }
  76. err = unmarshal(body, &st)
  77. st.ses = s
  78. return
  79. }
  80. // ApplicationDelete deletes an existing Application
  81. // appID : The ID of an Application
  82. func (s *Session) ApplicationDelete(appID string) (err error) {
  83. _, err = s.Request("DELETE", APPLICATION(appID), nil)
  84. if err != nil {
  85. return
  86. }
  87. return
  88. }
  89. //////////////////////////////////////////////////////////////////////////////
  90. // Below two functions are experimental ideas, they will absolutely change
  91. // one way or another and may be deleted entirely.
  92. // Delete is a concept helper function, may be removed.
  93. // this func depends on the Application.ses pointer
  94. // pointing to the Discord session that the application
  95. // came from. This "magic" makes some very very nice helper
  96. // functions possible.
  97. func (a *Application) Delete() (err error) {
  98. if a.ses == nil {
  99. return fmt.Errorf("ses is nil.")
  100. }
  101. return a.ses.ApplicationDelete(a.ID)
  102. }
  103. // Delete is a concept helper function, may be removed.
  104. // this one doesn't depend on the "magic" of adding the ses
  105. // pointer to each Application
  106. func (a *Application) DeleteB(s *Session) (err error) {
  107. return s.ApplicationDelete(a.ID)
  108. }