github.com/diamondburned/arikawa@v1.3.14/discord/user.go (about)

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