github.com/meowstercatel/discordgo@v0.0.0-20250615204139-29b43d676735/user.go (about)

     1  package discordgo
     2  
     3  import (
     4  	"strconv"
     5  )
     6  
     7  // UserFlags is the flags of "user" (see UserFlags* consts)
     8  // https://discord.com/developers/docs/resources/user#user-object-user-flags
     9  type UserFlags int
    10  
    11  // Valid UserFlags values
    12  const (
    13  	UserFlagDiscordEmployee           UserFlags = 1 << 0
    14  	UserFlagDiscordPartner            UserFlags = 1 << 1
    15  	UserFlagHypeSquadEvents           UserFlags = 1 << 2
    16  	UserFlagBugHunterLevel1           UserFlags = 1 << 3
    17  	UserFlagHouseBravery              UserFlags = 1 << 6
    18  	UserFlagHouseBrilliance           UserFlags = 1 << 7
    19  	UserFlagHouseBalance              UserFlags = 1 << 8
    20  	UserFlagEarlySupporter            UserFlags = 1 << 9
    21  	UserFlagTeamUser                  UserFlags = 1 << 10
    22  	UserFlagSystem                    UserFlags = 1 << 12
    23  	UserFlagBugHunterLevel2           UserFlags = 1 << 14
    24  	UserFlagVerifiedBot               UserFlags = 1 << 16
    25  	UserFlagVerifiedBotDeveloper      UserFlags = 1 << 17
    26  	UserFlagDiscordCertifiedModerator UserFlags = 1 << 18
    27  	UserFlagBotHTTPInteractions       UserFlags = 1 << 19
    28  	UserFlagActiveBotDeveloper        UserFlags = 1 << 22
    29  )
    30  
    31  // UserPremiumType is the type of premium (nitro) subscription a user has (see UserPremiumType* consts).
    32  // https://discord.com/developers/docs/resources/user#user-object-premium-types
    33  type UserPremiumType int
    34  
    35  // Valid UserPremiumType values.
    36  const (
    37  	UserPremiumTypeNone         UserPremiumType = 0
    38  	UserPremiumTypeNitroClassic UserPremiumType = 1
    39  	UserPremiumTypeNitro        UserPremiumType = 2
    40  	UserPremiumTypeNitroBasic   UserPremiumType = 3
    41  )
    42  
    43  // A User stores all data for an individual Discord user.
    44  type User struct {
    45  	// The ID of the user.
    46  	ID string `json:"id"`
    47  
    48  	// The email of the user. This is only present when
    49  	// the application possesses the email scope for the user.
    50  	Email string `json:"email"`
    51  
    52  	// The user's username.
    53  	Username string `json:"username"`
    54  
    55  	// The hash of the user's avatar. Use Session.UserAvatar
    56  	// to retrieve the avatar itself.
    57  	Avatar string `json:"avatar"`
    58  
    59  	// The user's chosen language option.
    60  	Locale string `json:"locale"`
    61  
    62  	// The discriminator of the user (4 numbers after name).
    63  	Discriminator string `json:"discriminator"`
    64  
    65  	// The user's display name, if it is set.
    66  	// For bots, this is the application name.
    67  	GlobalName string `json:"global_name"`
    68  
    69  	// The token of the user. This is only present for
    70  	// the user represented by the current session.
    71  	Token string `json:"token"`
    72  
    73  	// Whether the user's email is verified.
    74  	Verified bool `json:"verified"`
    75  
    76  	// Whether the user has multi-factor authentication enabled.
    77  	MFAEnabled bool `json:"mfa_enabled"`
    78  
    79  	// The hash of the user's banner image.
    80  	Banner string `json:"banner"`
    81  
    82  	// User's banner color, encoded as an integer representation of hexadecimal color code
    83  	AccentColor int `json:"accent_color"`
    84  
    85  	// Whether the user is a bot.
    86  	Bot bool `json:"bot"`
    87  
    88  	// The public flags on a user's account.
    89  	// This is a combination of bit masks; the presence of a certain flag can
    90  	// be checked by performing a bitwise AND between this int and the flag.
    91  	PublicFlags UserFlags `json:"public_flags"`
    92  
    93  	// The type of Nitro subscription on a user's account.
    94  	// Only available when the request is authorized via a Bearer token.
    95  	PremiumType UserPremiumType `json:"premium_type"`
    96  
    97  	// Whether the user is an Official Discord System user (part of the urgent message system).
    98  	System bool `json:"system"`
    99  
   100  	// The flags on a user's account.
   101  	// Only available when the request is authorized via a Bearer token.
   102  	Flags int `json:"flags"`
   103  }
   104  
   105  // String returns a unique identifier of the form username#discriminator
   106  // or just username, if the discriminator is set to "0".
   107  func (u *User) String() string {
   108  	// If the user has been migrated from the legacy username system, their discriminator is "0".
   109  	// See https://support-dev.discord.com/hc/en-us/articles/13667755828631
   110  	if u.Discriminator == "0" {
   111  		return u.Username
   112  	}
   113  
   114  	return u.Username + "#" + u.Discriminator
   115  }
   116  
   117  // Mention return a string which mentions the user
   118  func (u *User) Mention() string {
   119  	return "<@" + u.ID + ">"
   120  }
   121  
   122  // AvatarURL returns a URL to the user's avatar.
   123  //
   124  //	size:    The size of the user's avatar as a power of two
   125  //	         if size is an empty string, no size parameter will
   126  //	         be added to the URL.
   127  func (u *User) AvatarURL(size string) string {
   128  	return avatarURL(
   129  		u.Avatar,
   130  		EndpointDefaultUserAvatar(u.DefaultAvatarIndex()),
   131  		EndpointUserAvatar(u.ID, u.Avatar),
   132  		EndpointUserAvatarAnimated(u.ID, u.Avatar),
   133  		size,
   134  	)
   135  }
   136  
   137  // BannerURL returns the URL of the users's banner image.
   138  //
   139  //	size:    The size of the desired banner image as a power of two
   140  //	         Image size can be any power of two between 16 and 4096.
   141  func (u *User) BannerURL(size string) string {
   142  	return bannerURL(u.Banner, EndpointUserBanner(u.ID, u.Banner), EndpointUserBannerAnimated(u.ID, u.Banner), size)
   143  }
   144  
   145  // DefaultAvatarIndex returns the index of the user's default avatar.
   146  func (u *User) DefaultAvatarIndex() int {
   147  	if u.Discriminator == "0" {
   148  		id, _ := strconv.ParseUint(u.ID, 10, 64)
   149  		return int((id >> 22) % 6)
   150  	}
   151  
   152  	id, _ := strconv.Atoi(u.Discriminator)
   153  	return id % 5
   154  }
   155  
   156  // DisplayName returns the user's global name if they have one, otherwise it returns their username.
   157  func (u *User) DisplayName() string {
   158  	if u.GlobalName != "" {
   159  		return u.GlobalName
   160  	}
   161  	return u.Username
   162  }