user.go 1.8 KB

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