demo.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /******************************************************************************
  2. * Discordgo demo program.
  3. *
  4. * Please run this with command line arguments of email and password.
  5. */
  6. package main
  7. import (
  8. "fmt"
  9. "os"
  10. discord "github.com/bwmarrin/discordgo"
  11. )
  12. func main() {
  13. var err error
  14. var email string = os.Args[1]
  15. var password string = os.Args[2]
  16. // Create new session object and enable debugging.
  17. session := discord.Session{Debug: true}
  18. // Login to the Discord server with the provided email and password
  19. // from the command line arguments
  20. session.Token, err = session.Login(email, password)
  21. if err != nil {
  22. fmt.Println("Unable to login to Discord.")
  23. fmt.Println(err)
  24. return
  25. }
  26. // Example using Request function to query a specific URL
  27. // This pulls authenticated user's information.
  28. // Request returns the actual request body not JSON
  29. body, err := discord.Request(&session, "http://discordapp.com/api/users/@me")
  30. fmt.Println(body)
  31. // Use the User function to do the same as above. This function
  32. // returns a User structure
  33. user, err := discord.Users(&session, "@me")
  34. fmt.Println(user)
  35. // Use the Servers function to pull all available servers for a given user
  36. // This returns a Server structure
  37. servers, err := discord.Servers(&session, "@me")
  38. fmt.Println(servers)
  39. // Use the Channels function to pull all available channels for a given
  40. // server. This returns a Channel structure.
  41. channels, err := discord.Channels(&session, servers[0].Id)
  42. fmt.Println(channels)
  43. // Use the Logout function to Logout from the Discord server.
  44. discord.Logout(&session)
  45. return
  46. }