user.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package discordgo
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. // A User stores all data for an individual Discord user.
  7. type User struct {
  8. // The ID of the user.
  9. ID string `json:"id"`
  10. // The email of the user. This is only present when
  11. // the application possesses the email scope for the user.
  12. Email string `json:"email"`
  13. // The user's username.
  14. Username string `json:"username"`
  15. // The hash of the user's avatar. Use Session.UserAvatar
  16. // to retrieve the avatar itself.
  17. Avatar string `json:"avatar"`
  18. // The discriminator of the user (4 numbers after name).
  19. Discriminator string `json:"discriminator"`
  20. // The token of the user. This is only present for
  21. // the user represented by the current session.
  22. Token string `json:"token"`
  23. // Whether the user's email is verified.
  24. Verified bool `json:"verified"`
  25. // Whether the user has multi-factor authentication enabled.
  26. MFAEnabled bool `json:"mfa_enabled"`
  27. // Whether the user is a bot.
  28. Bot bool `json:"bot"`
  29. }
  30. // String returns a unique identifier of the form username#discriminator
  31. func (u *User) String() string {
  32. return fmt.Sprintf("%s#%s", u.Username, u.Discriminator)
  33. }
  34. // Mention return a string which mentions the user
  35. func (u *User) Mention() string {
  36. return fmt.Sprintf("<@%s>", u.ID)
  37. }
  38. // AvatarURL returns a URL to the user's avatar.
  39. // size: The size of the user's avatar as a power of two
  40. // if size is an empty string, no size parameter will
  41. // be added to the URL.
  42. func (u *User) AvatarURL(size string) string {
  43. var URL string
  44. if u.Avatar == "" {
  45. URL = EndpointDefaultUserAvatar(u.Discriminator)
  46. } else if strings.HasPrefix(u.Avatar, "a_") {
  47. URL = EndpointUserAvatarAnimated(u.ID, u.Avatar)
  48. } else {
  49. URL = EndpointUserAvatar(u.ID, u.Avatar)
  50. }
  51. if size != "" {
  52. return URL + "?size=" + size
  53. }
  54. return URL
  55. }