github.com/diamondburned/arikawa/v2@v2.1.0/discord/user.go (about) 1 package discord 2 3 import ( 4 "strconv" 5 "time" 6 ) 7 8 type User struct { 9 ID UserID `json:"id"` 10 Username string `json:"username"` 11 Discriminator string `json:"discriminator"` 12 Avatar Hash `json:"avatar"` 13 14 // These fields may be omitted 15 16 Bot bool `json:"bot,omitempty"` 17 MFA bool `json:"mfa_enabled,omitempty"` 18 19 Nitro UserNitro `json:"premium_type,omitempty"` 20 Flags UserFlags `json:"flags,omitempty"` 21 PublicFlags UserFlags `json:"public_flags,omitempty"` 22 23 DiscordSystem bool `json:"system,omitempty"` 24 EmailVerified bool `json:"verified,omitempty"` 25 26 Locale string `json:"locale,omitempty"` 27 Email string `json:"email,omitempty"` 28 } 29 30 // CreatedAt returns a time object representing when the user was created. 31 func (u User) CreatedAt() time.Time { 32 return u.ID.Time() 33 } 34 35 // Mention returns a mention of the user. 36 func (u User) Mention() string { 37 return u.ID.Mention() 38 } 39 40 // Tag returns a tag of the user. 41 func (u User) Tag() string { 42 return u.Username + "#" + u.Discriminator 43 } 44 45 // AvatarURL returns the URL of the Avatar Image. It automatically detects a 46 // suitable type. 47 func (u User) AvatarURL() string { 48 return u.AvatarURLWithType(AutoImage) 49 } 50 51 // AvatarURLWithType returns the URL of the Avatar Image using the passed type. 52 // If the user has no Avatar, his default avatar will be returned. This 53 // requires ImageType Auto or PNG 54 // 55 // Supported Image Types: PNG, JPEG, WebP, GIF (read above for caveat) 56 func (u User) AvatarURLWithType(t ImageType) string { 57 if u.Avatar == "" { 58 if t != PNGImage && t != AutoImage { 59 return "" 60 } 61 62 disc, err := strconv.Atoi(u.Discriminator) 63 if err != nil { // this should never happen 64 return "" 65 } 66 picNo := strconv.Itoa(disc % 5) 67 68 return "https://cdn.discordapp.com/embed/avatars/" + picNo + ".png" 69 } 70 71 return "https://cdn.discordapp.com/avatars/" + u.ID.String() + "/" + t.format(u.Avatar) 72 } 73 74 type UserFlags uint32 75 76 const NoFlag UserFlags = 0 77 78 const ( 79 Employee UserFlags = 1 << iota 80 Partner 81 HypeSquadEvents 82 BugHunterLvl1 83 _ 84 _ 85 HouseBravery 86 HouseBrilliance 87 HouseBalance 88 EarlySupporter 89 TeamUser 90 _ 91 System 92 _ 93 BugHunterLvl2 94 _ 95 VerifiedBot 96 VerifiedBotDeveloper 97 CertifiedModerator 98 ) 99 100 type UserNitro uint8 101 102 const ( 103 NoUserNitro UserNitro = iota 104 NitroClassic 105 NitroFull 106 ) 107 108 type Connection struct { 109 ID string `json:"id"` 110 Name string `json:"name"` 111 Type Service `json:"type"` 112 113 Revoked bool `json:"revoked"` 114 Verified bool `json:"verified"` 115 FriendSync bool `json:"friend_sync"` 116 ShowActivity bool `json:"show_activity"` 117 118 Visibility ConnectionVisibility `json:"visibility"` 119 120 // Only partial 121 Integrations []Integration `json:"integrations"` 122 } 123 124 type ConnectionVisibility uint8 125 126 const ( 127 ConnectionNotVisible ConnectionVisibility = iota 128 ConnectionVisibleEveryone 129 ) 130 131 type Activity struct { 132 Name string `json:"name"` 133 URL URL `json:"url,omitempty"` 134 135 Type ActivityType `json:"type"` 136 137 // User only 138 139 Instance bool `json:"instance,omitempty"` 140 Flags ActivityFlags `json:"flags,omitempty"` 141 142 CreatedAt UnixTimestamp `json:"created_at,omitempty"` 143 Timestamps *ActivityTimestamp `json:"timestamps,omitempty"` 144 145 AppID AppID `json:"application_id,omitempty"` 146 Details string `json:"details,omitempty"` 147 State string `json:"state,omitempty"` // party status 148 Emoji *Emoji `json:"emoji,omitempty"` 149 150 Party *ActivityParty `json:"party,omitempty"` 151 Assets *ActivityAssets `json:"assets,omitempty"` 152 Secrets *ActivitySecrets `json:"secrets,omitempty"` 153 154 // Undocumented fields 155 SyncID string `json:"sync_id,omitempty"` 156 SessionID string `json:"session_id,omitempty"` 157 } 158 159 type ActivityType uint8 160 161 const ( 162 // Playing $name 163 GameActivity ActivityType = iota 164 // Streaming $details 165 StreamingActivity 166 // Listening to $name 167 ListeningActivity 168 // Watching $name 169 WatchingActivity 170 // $emoji $state 171 CustomActivity 172 ) 173 174 type ActivityFlags uint32 175 176 const ( 177 InstanceActivity ActivityFlags = 1 << iota 178 JoinActivity 179 SpectateActivity 180 JoinRequestActivity 181 SyncActivity 182 PlayActivity 183 ) 184 185 type ActivityTimestamp struct { 186 Start UnixMsTimestamp `json:"start,omitempty"` 187 End UnixMsTimestamp `json:"end,omitempty"` 188 } 189 190 type ActivityParty struct { 191 ID string `json:"id,omitempty"` 192 Size [2]int `json:"size,omitempty"` // [ current, max ] 193 } 194 195 type ActivityAssets struct { 196 LargeImage string `json:"large_image,omitempty"` // id 197 LargeText string `json:"large_text,omitempty"` 198 SmallImage string `json:"small_image,omitempty"` // id 199 SmallText string `json:"small_text,omitempty"` 200 } 201 202 type ActivitySecrets struct { 203 Join string `json:"join,omitempty"` 204 Spectate string `json:"spectate,omitempty"` 205 Match string `json:"match,omitempty"` 206 } 207 208 // A Relationship between the logged in user and the user in the struct. This 209 // struct is undocumented. 210 type Relationship struct { 211 UserID UserID `json:"id"` 212 User User `json:"user"` 213 Type RelationshipType `json:"type"` 214 } 215 216 // RelationshipType is an enum for a relationship. 217 type RelationshipType uint8 218 219 const ( 220 _ RelationshipType = iota 221 FriendRelationship 222 BlockedRelationship 223 IncomingFriendRequest 224 SentFriendRequest 225 )