user.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package discordgo
  2. import "strings"
  3. // UserFlags is the flags of "user" (see UserFlags* consts)
  4. // https://discord.com/developers/docs/resources/user#user-object-user-flags
  5. type UserFlags int
  6. // Valid UserFlags values
  7. const (
  8. UserFlagDiscordEmployee UserFlags = 1 << 0
  9. UserFlagDiscordPartner = 1 << 1
  10. UserFlagHypeSquadEvents = 1 << 2
  11. UserFlagBugHunterLevel1 = 1 << 3
  12. UserFlagHouseBravery = 1 << 6
  13. UserFlagHouseBrilliance = 1 << 7
  14. UserFlagHouseBalance = 1 << 8
  15. UserFlagEarlySupporter = 1 << 9
  16. UserFlagTeamUser = 1 << 10
  17. UserFlagSystem = 1 << 12
  18. UserFlagBugHunterLevel2 = 1 << 14
  19. UserFlagVerifiedBot = 1 << 16
  20. UserFlagVerifiedBotDeveloper = 1 << 17
  21. )
  22. // A User stores all data for an individual Discord user.
  23. type User struct {
  24. // The ID of the user.
  25. ID string `json:"id"`
  26. // The email of the user. This is only present when
  27. // the application possesses the email scope for the user.
  28. Email string `json:"email"`
  29. // The user's username.
  30. Username string `json:"username"`
  31. // The hash of the user's avatar. Use Session.UserAvatar
  32. // to retrieve the avatar itself.
  33. Avatar string `json:"avatar"`
  34. // The user's chosen language option.
  35. Locale string `json:"locale"`
  36. // The discriminator of the user (4 numbers after name).
  37. Discriminator string `json:"discriminator"`
  38. // The token of the user. This is only present for
  39. // the user represented by the current session.
  40. Token string `json:"token"`
  41. // Whether the user's email is verified.
  42. Verified bool `json:"verified"`
  43. // Whether the user has multi-factor authentication enabled.
  44. MFAEnabled bool `json:"mfa_enabled"`
  45. // Whether the user is a bot.
  46. Bot bool `json:"bot"`
  47. // The public flags on a user's account.
  48. // This is a combination of bit masks; the presence of a certain flag can
  49. // be checked by performing a bitwise AND between this int and the flag.
  50. PublicFlags UserFlags `json:"public_flags"`
  51. }
  52. // String returns a unique identifier of the form username#discriminator
  53. func (u *User) String() string {
  54. return u.Username + "#" + u.Discriminator
  55. }
  56. // Mention return a string which mentions the user
  57. func (u *User) Mention() string {
  58. return "<@" + u.ID + ">"
  59. }
  60. // AvatarURL returns a URL to the user's avatar.
  61. // size: The size of the user's avatar as a power of two
  62. // if size is an empty string, no size parameter will
  63. // be added to the URL.
  64. func (u *User) AvatarURL(size string) string {
  65. var URL string
  66. if u.Avatar == "" {
  67. URL = EndpointDefaultUserAvatar(u.Discriminator)
  68. } else if strings.HasPrefix(u.Avatar, "a_") {
  69. URL = EndpointUserAvatarAnimated(u.ID, u.Avatar)
  70. } else {
  71. URL = EndpointUserAvatar(u.ID, u.Avatar)
  72. }
  73. if size != "" {
  74. return URL + "?size=" + size
  75. }
  76. return URL
  77. }