-
-
Notifications
You must be signed in to change notification settings - Fork 742
/
RestUser.cs
189 lines (168 loc) · 7.41 KB
/
RestUser.cs
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
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.Threading.Tasks;
using EventUserModel = Discord.API.GuildScheduledEventUser;
using Model = Discord.API.User;
namespace Discord.Rest
{
/// <summary>
/// Represents a REST-based user.
/// </summary>
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class RestUser : RestEntity<ulong>, IUser, IUpdateable
{
#region RestUser
/// <inheritdoc />
public bool IsBot { get; private set; }
/// <inheritdoc />
public string Username { get; private set; }
/// <inheritdoc />
public ushort DiscriminatorValue { get; private set; }
/// <inheritdoc />
public string AvatarId { get; private set; }
/// <summary>
/// Gets the hash of the banner.
/// </summary>
/// <remarks>
/// <see langword="null"/> if the user has no banner set.
/// </remarks>
public string BannerId { get; private set; }
/// <summary>
/// Gets the color of the banner.
/// </summary>
/// <remarks>
/// <see langword="null"/> if the user has no banner set.
/// </remarks>
public Color? BannerColor { get; private set; }
/// <inheritdoc />
public Color? AccentColor { get; private set; }
/// <inheritdoc />
public UserProperties? PublicFlags { get; private set; }
/// <inheritdoc />
public string GlobalName { get; internal set; }
/// <inheritdoc />
public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
/// <inheritdoc />
public string Discriminator => DiscriminatorValue.ToString("D4");
/// <inheritdoc />
public string Mention => MentionUtils.MentionUser(Id);
/// <inheritdoc />
public virtual IActivity Activity => null;
/// <inheritdoc />
public virtual UserStatus Status => UserStatus.Offline;
/// <inheritdoc />
public virtual IReadOnlyCollection<ClientType> ActiveClients => ImmutableHashSet<ClientType>.Empty;
/// <inheritdoc />
public virtual IReadOnlyCollection<IActivity> Activities => ImmutableList<IActivity>.Empty;
/// <inheritdoc />
public virtual bool IsWebhook => false;
/// <inheritdoc />
public string AvatarDecorationHash { get; private set; }
/// <inheritdoc />
public ulong? AvatarDecorationSkuId { get; private set; }
internal RestUser(BaseDiscordClient discord, ulong id)
: base(discord, id)
{
}
internal static RestUser Create(BaseDiscordClient discord, Model model)
=> Create(discord, null, model, null);
internal static RestUser Create(BaseDiscordClient discord, IGuild guild, Model model, ulong? webhookId)
{
RestUser entity;
if (webhookId.HasValue)
entity = new RestWebhookUser(discord, guild, model.Id, webhookId.Value);
else
entity = new RestUser(discord, model.Id);
entity.Update(model);
return entity;
}
internal static RestUser Create(BaseDiscordClient discord, IGuild guild, EventUserModel model)
{
if (model.Member.IsSpecified)
{
var member = model.Member.Value;
member.User = model.User;
return RestGuildUser.Create(discord, guild, member);
}
else
return RestUser.Create(discord, model.User);
}
internal virtual void Update(Model model)
{
if (model.Avatar.IsSpecified)
AvatarId = model.Avatar.Value;
if (model.Banner.IsSpecified)
BannerId = model.Banner.Value;
if (model.BannerColor.IsSpecified)
BannerColor = model.BannerColor.Value;
if (model.AccentColor.IsSpecified)
AccentColor = model.AccentColor.Value;
if (model.Discriminator.IsSpecified)
DiscriminatorValue = ushort.Parse(model.Discriminator.GetValueOrDefault(null) ?? "0", NumberStyles.None, CultureInfo.InvariantCulture);
if (model.Bot.IsSpecified)
IsBot = model.Bot.Value;
if (model.Username.IsSpecified)
Username = model.Username.Value;
if (model.PublicFlags.IsSpecified)
PublicFlags = model.PublicFlags.Value;
if (model.GlobalName.IsSpecified)
GlobalName = model.GlobalName.Value;
if (model.AvatarDecoration is { IsSpecified: true, Value: not null })
{
AvatarDecorationHash = model.AvatarDecoration.Value?.Asset;
AvatarDecorationSkuId = model.AvatarDecoration.Value?.SkuId;
}
}
/// <inheritdoc />
public virtual async Task UpdateAsync(RequestOptions options = null)
{
var model = await Discord.ApiClient.GetUserAsync(Id, options).ConfigureAwait(false);
Update(model);
}
/// <summary>
/// Creates a direct message channel to this user.
/// </summary>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous get operation. The task result contains a rest DM channel where the user is the recipient.
/// </returns>
public Task<RestDMChannel> CreateDMChannelAsync(RequestOptions options = null)
=> UserHelper.CreateDMChannelAsync(this, Discord, options);
/// <inheritdoc />
public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
=> CDN.GetUserAvatarUrl(Id, AvatarId, size, format);
/// <inheritdoc />
public string GetBannerUrl(ImageFormat format = ImageFormat.Auto, ushort size = 256)
=> CDN.GetUserBannerUrl(Id, BannerId, size, format);
/// <inheritdoc />
public string GetDefaultAvatarUrl()
=> DiscriminatorValue != 0
? CDN.GetDefaultUserAvatarUrl(DiscriminatorValue)
: CDN.GetDefaultUserAvatarUrl(Id);
public virtual string GetDisplayAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
=> GetAvatarUrl(format, size) ?? GetDefaultAvatarUrl();
/// <inheritdoc />
public string GetAvatarDecorationUrl()
=> AvatarDecorationHash is not null
? CDN.GetAvatarDecorationUrl(AvatarDecorationHash)
: null;
/// <summary>
/// Gets the Username#Discriminator of the user.
/// </summary>
/// <returns>
/// A string that resolves to Username#Discriminator of the user.
/// </returns>
public override string ToString()
=> Format.UsernameAndDiscriminator(this, Discord.FormatUsersInBidirectionalUnicode);
private string DebuggerDisplay => $"{Format.UsernameAndDiscriminator(this, Discord.FormatUsersInBidirectionalUnicode)} ({Id}{(IsBot ? ", Bot" : "")})";
#endregion
#region IUser
/// <inheritdoc />
async Task<IDMChannel> IUser.CreateDMChannelAsync(RequestOptions options)
=> await CreateDMChannelAsync(options).ConfigureAwait(false);
#endregion
}
}