user.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 user's chosen language option.
  19. Locale string `json:"locale"`
  20. // The discriminator of the user (4 numbers after name).
  21. Discriminator string `json:"discriminator"`
  22. // The token of the user. This is only present for
  23. // the user represented by the current session.
  24. Token string `json:"token"`
  25. // Whether the user's email is verified.
  26. Verified bool `json:"verified"`
  27. // Whether the user has multi-factor authentication enabled.
  28. MFAEnabled bool `json:"mfa_enabled"`
  29. // Whether the user is a bot.
  30. Bot bool `json:"bot"`
  31. }
  32. // String returns a unique identifier of the form username#discriminator
  33. func (u *User) String() string {
  34. return fmt.Sprintf("%s#%s", u.Username, u.Discriminator)
  35. }
  36. // Mention return a string which mentions the user
  37. func (u *User) Mention() string {
  38. return fmt.Sprintf("<@%s>", u.ID)
  39. }
  40. // AvatarURL returns a URL to the user's avatar.
  41. // size: The size of the user's avatar as a power of two
  42. // if size is an empty string, no size parameter will
  43. // be added to the URL.
  44. func (u *User) AvatarURL(size string) string {
  45. var URL string
  46. if u.Avatar == "" {
  47. URL = EndpointDefaultUserAvatar(u.Discriminator)
  48. } else if strings.HasPrefix(u.Avatar, "a_") {
  49. URL = EndpointUserAvatarAnimated(u.ID, u.Avatar)
  50. } else {
  51. URL = EndpointUserAvatar(u.ID, u.Avatar)
  52. }
  53. if size != "" {
  54. return URL + "?size=" + size
  55. }
  56. return URL
  57. }