oauth2_test.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package discordgo_test
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "github.com/bwmarrin/discordgo"
  7. )
  8. func ExampleApplication() {
  9. // Authentication Token pulled from environment variable DG_TOKEN
  10. Token := os.Getenv("DG_TOKEN")
  11. if Token == "" {
  12. return
  13. }
  14. // Create a new Discordgo session
  15. dg, err := discordgo.New(Token)
  16. if err != nil {
  17. log.Println(err)
  18. return
  19. }
  20. // Create an new Application
  21. ap := &discordgo.Application{}
  22. ap.Name = "TestApp"
  23. ap.Description = "TestDesc"
  24. ap, err = dg.ApplicationCreate(ap)
  25. log.Printf("ApplicationCreate: err: %+v, app: %+v\n", err, ap)
  26. // Get a specific Application by it's ID
  27. ap, err = dg.Application(ap.ID)
  28. log.Printf("Application: err: %+v, app: %+v\n", err, ap)
  29. // Update an existing Application with new values
  30. ap.Description = "Whooooa"
  31. ap, err = dg.ApplicationUpdate(ap.ID, ap)
  32. log.Printf("ApplicationUpdate: err: %+v, app: %+v\n", err, ap)
  33. // create a new bot account for this application
  34. bot, err := dg.ApplicationBotCreate(ap.ID, "")
  35. log.Printf("BotCreate: err: %+v, bot: %+v\n", err, bot)
  36. // Get a list of all applications for the authenticated user
  37. apps, err := dg.Applications()
  38. log.Printf("Applications: err: %+v, apps : %+v\n", err, apps)
  39. for k, v := range apps {
  40. log.Printf("Applications: %d : %+v\n", k, v)
  41. }
  42. // Delete the application we created.
  43. err = ap.Delete()
  44. log.Printf("Delete: err: %+v\n", err)
  45. return
  46. }
  47. // This provides an example on converting an existing normal user account
  48. // into a bot account. You must authentication to Discord using your personal
  49. // username and password then provide the authentication token of the account
  50. // you want converted.
  51. func ExampleApplicationConvertBot() {
  52. dg, err := discordgo.New("myemail", "mypassword")
  53. if err != nil {
  54. log.Println(err)
  55. return
  56. }
  57. // create an application
  58. ap := &discordgo.Application{}
  59. ap.Name = "Application Name"
  60. ap.Description = "Application Description"
  61. ap, err = dg.ApplicationCreate(ap)
  62. log.Printf("ApplicationCreate: err: %+v, app: %+v\n", err, ap)
  63. // create a bot account
  64. bot, err := dg.ApplicationBotCreate(ap.ID, "existing bot user account token")
  65. log.Printf("BotCreate: err: %+v, bot: %+v\n", err, bot)
  66. if err != nil {
  67. log.Printf("You can not login with your converted bot user using the below token\n%s\n", bot.Token)
  68. }
  69. return
  70. }