-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUser.cs
More file actions
233 lines (192 loc) · 8.01 KB
/
User.cs
File metadata and controls
233 lines (192 loc) · 8.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using Newtonsoft.Json;
using System;
namespace DiscordRPC
{
/// <summary>
/// Object representing a Discord user. This is used for join requests.
/// </summary>
public class User
{
/// <summary>
/// Possible formats for avatars
/// </summary>
public enum AvatarFormat
{
/// <summary>
/// Portable Network Graphics format (.png)
/// <para>Losses format that supports transparent avatars. Most recommended for stationary formats with wide support from many libraries.</para>
/// </summary>
PNG,
/// <summary>
/// Joint Photographic Experts Group format (.jpeg)
/// <para>The format most cameras use. Lossy and does not support transparent avatars.</para>
/// </summary>
JPEG,
/// <summary>
/// WebP format (.webp)
/// <para>Picture only version of WebM. Pronounced "weeb p".</para>
/// </summary>
WebP,
/// <summary>
/// Graphics Interchange Format (.gif)
/// <para>Animated avatars that Discord Nitro users are able to use. If the user doesn't have an animated avatar, then it will just be a single frame gif.</para>
/// </summary>
GIF //Gif, as in gift.
}
/// <summary>
/// Possible square sizes of avatars.
/// </summary>
public enum AvatarSize
{
/// <summary> 16 x 16 pixels.</summary>
x16 = 16,
/// <summary> 32 x 32 pixels.</summary>
x32 = 32,
/// <summary> 64 x 64 pixels.</summary>
x64 = 64,
/// <summary> 128 x 128 pixels.</summary>
x128 = 128,
/// <summary> 256 x 256 pixels.</summary>
x256 = 256,
/// <summary> 512 x 512 pixels.</summary>
x512 = 512,
/// <summary> 1024 x 1024 pixels.</summary>
x1024 = 1024,
/// <summary> 2048 x 2048 pixels.</summary>
x2048 = 2048
}
/// <summary>
/// The snowflake ID of the user.
/// </summary>
[JsonProperty("id")]
public ulong ID { get; private set; }
/// <summary>
/// The username of the player.
/// </summary>
[JsonProperty("username")]
public string Username { get; private set; }
/// <summary>
/// The discriminator of the user.
/// </summary>
[JsonProperty("discriminator")]
public int Discriminator { get; private set; }
/// <summary>
/// The avatar hash of the user. Too get a URL for the avatar, use the <see cref="GetAvatarURL(AvatarFormat, AvatarSize)"/>. This can be null if the user has no avatar. The <see cref="GetAvatarURL(AvatarFormat, AvatarSize)"/> will account for this and return the discord default.
/// </summary>
[JsonProperty("avatar")]
public string Avatar { get; private set; }
/// <summary>
/// The flags on a users account, often represented as a badge.
/// </summary>
[JsonProperty("flags")]
public Flag Flags { get; private set; }
/// <summary>
/// A flag on the user account
/// </summary>
[Flags]
public enum Flag
{
/// <summary>No flag</summary>
None = 0,
/// <summary>Staff of Discord.</summary>
Employee = 1 << 0,
/// <summary>Partners of Discord.</summary>
Partner = 1 << 1,
/// <summary>Original HypeSquad which organise events.</summary>
HypeSquad = 1 << 2,
/// <summary>Bug Hunters that found and reported bugs in Discord.</summary>
BugHunter = 1 << 3,
//These 2 are mistery types
//A = 1 << 4,
//B = 1 << 5,
/// <summary>The HypeSquad House of Bravery.</summary>
HouseBravery = 1 << 6,
/// <summary>The HypeSquad House of Brilliance.</summary>
HouseBrilliance = 1 << 7,
/// <summary>The HypeSquad House of Balance (the best one).</summary>
HouseBalance = 1 << 8,
/// <summary>Early Supporter of Discord and had Nitro before the store was released.</summary>
EarlySupporter = 1 << 9,
/// <summary>Apart of a team.
/// <para>Unclear if it is reserved for members that share a team with the current application.</para>
/// </summary>
TeamUser = 1 << 10
}
/// <summary>
/// The premium type of the user.
/// </summary>
[JsonProperty("premium_type")]
public PremiumType Premium { get; private set; }
/// <summary>
/// Type of premium
/// </summary>
public enum PremiumType
{
/// <summary>No subscription to any forms of Nitro.</summary>
None = 0,
/// <summary>Nitro Classic subscription. Has chat perks and animated avatars.</summary>
NitroClassic = 1,
/// <summary>Nitro subscription. Has chat perks, animated avatars, server boosting, and access to free Nitro Games.</summary>
Nitro = 2
}
/// <summary>
/// The endpoint for the CDN. Normally cdn.discordapp.com.
/// </summary>
public string CdnEndpoint { get; private set; }
/// <summary>
/// Creates a new User instance.
/// </summary>
internal User()
{
CdnEndpoint = "cdn.discordapp.com";
}
/// <summary>
/// Updates the URL paths to the appropriate configuration
/// </summary>
/// <param name="configuration">The configuration received by the OnReady event.</param>
internal void SetConfiguration(Configuration configuration)
{
this.CdnEndpoint = configuration.CdnHost;
}
/// <summary>
/// Gets a URL that can be used to download the user's avatar. If the user has not yet set their avatar, it will return the default one that discord is using. The default avatar only supports the <see cref="AvatarFormat.PNG"/> format.
/// </summary>
/// <param name="format">The format of the target avatar</param>
/// <param name="size">The optional size of the avatar you wish for. Defaults to x128.</param>
/// <returns></returns>
public string GetAvatarURL(AvatarFormat format, AvatarSize size = AvatarSize.x128)
{
//Prepare the endpoint
string endpoint = "/avatars/" + ID + "/" + Avatar;
//The user has no avatar, so we better replace it with the default
if (string.IsNullOrEmpty(Avatar))
{
//Make sure we are only using PNG
if (format != AvatarFormat.PNG)
throw new BadImageFormatException("The user has no avatar and the requested format " + format.ToString() + " is not supported. (Only supports PNG).");
//Get the endpoint
int descrim = Discriminator % 5;
endpoint = "/embed/avatars/" + descrim;
}
//Finish of the endpoint
return string.Format("https://{0}{1}{2}?size={3}", this.CdnEndpoint, endpoint, GetAvatarExtension(format), (int)size);
}
/// <summary>
/// Returns the file extension of the specified format.
/// </summary>
/// <param name="format">The format to get the extention off</param>
/// <returns>Returns a period prefixed file extension.</returns>
public string GetAvatarExtension(AvatarFormat format)
{
return "." + format.ToString().ToLowerInvariant();
}
/// <summary>
/// Formats the user into username#discriminator
/// </summary>
/// <returns></returns>
public override string ToString()
{
return Username + "#" + Discriminator.ToString("D4");
}
}
}