oauth2_test.go 2.3 KB

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