user.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // The type of Nitro subscription on a user's account.
  52. // Only available when the request is authorized via a Bearer token.
  53. PremiumType int `json:"premium_type"`
  54. // Whether the user is an Official Discord System user (part of the urgent message system).
  55. System bool `json:"system"`
  56. // The flags on a user's account.
  57. // Only available when the request is authorized via a Bearer token.
  58. Flags int `json:"flags"`
  59. }
  60. // String returns a unique identifier of the form username#discriminator
  61. func (u *User) String() string {
  62. return u.Username + "#" + u.Discriminator
  63. }
  64. // Mention return a string which mentions the user
  65. func (u *User) Mention() string {
  66. return "<@" + u.ID + ">"
  67. }
  68. // AvatarURL returns a URL to the user's avatar.
  69. // size: The size of the user's avatar as a power of two
  70. // if size is an empty string, no size parameter will
  71. // be added to the URL.
  72. func (u *User) AvatarURL(size string) string {
  73. var URL string
  74. if u.Avatar == "" {
  75. URL = EndpointDefaultUserAvatar(u.Discriminator)
  76. } else if strings.HasPrefix(u.Avatar, "a_") {
  77. URL = EndpointUserAvatarAnimated(u.ID, u.Avatar)
  78. } else {
  79. URL = EndpointUserAvatar(u.ID, u.Avatar)
  80. }
  81. if size != "" {
  82. return URL + "?size=" + size
  83. }
  84. return URL
  85. }