demo.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 PrivateChannels function to get a list of PrivateChannels
  36. // for a given user.
  37. private, err := discord.PrivateChannels(&session, "@me")
  38. fmt.Println(private)
  39. // Use the Servers function to pull all available servers for a given user
  40. // This returns a Server structure
  41. servers, err := discord.Servers(&session, "@me")
  42. fmt.Println(servers)
  43. // Use the Members function to pull all members of a given server.
  44. members, err := discord.Members(&session, servers[0].Id)
  45. fmt.Println(members)
  46. // Use the Channels function to pull all available channels for a given
  47. // server. This returns a Channel structure.
  48. channels, err := discord.Channels(&session, servers[0].Id)
  49. fmt.Println(channels)
  50. // Use the Messages function to pull messages from the given channel
  51. // limit the result to 2 messages and don't provide beforeId or afterId
  52. messages, err := discord.Messages(&session, channels[0].Id, 2, 0, 0)
  53. fmt.Println(messages)
  54. // Use SendMessage to send a message to the given channel.
  55. responce, err := discord.SendMessage(&session, channels[0].Id, "Testing Discordgo")
  56. fmt.Println(responce)
  57. // Use the Logout function to Logout from the Discord server.
  58. discord.Logout(&session)
  59. return
  60. }