using System; using System.Collections.Immutable; using System.Diagnostics; using System.Globalization; using System.Threading.Tasks; using Model = Discord.API.User; namespace Discord.Rest { /// /// Represents a REST-based user. /// [DebuggerDisplay(@"{DebuggerDisplay,nq}")] public class RestUser : RestEntity, IUser, IUpdateable { /// public bool IsBot { get; private set; } /// public string Username { get; private set; } /// public ushort DiscriminatorValue { get; private set; } /// public string AvatarId { get; private set; } /// public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id); /// public string Discriminator => DiscriminatorValue.ToString("D4"); /// public string Mention => MentionUtils.MentionUser(Id); /// public virtual IActivity Activity => null; /// public virtual UserStatus Status => UserStatus.Offline; /// public virtual IImmutableSet ActiveClients => ImmutableHashSet.Empty; /// public virtual bool IsWebhook => false; 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 virtual void Update(Model model) { if (model.Avatar.IsSpecified) AvatarId = model.Avatar.Value; if (model.Discriminator.IsSpecified) DiscriminatorValue = ushort.Parse(model.Discriminator.Value, NumberStyles.None, CultureInfo.InvariantCulture); if (model.Bot.IsSpecified) IsBot = model.Bot.Value; if (model.Username.IsSpecified) Username = model.Username.Value; } /// public virtual async Task UpdateAsync(RequestOptions options = null) { var model = await Discord.ApiClient.GetUserAsync(Id, options).ConfigureAwait(false); Update(model); } /// /// Returns a direct message channel to this user, or create one if it does not already exist. /// /// The options to be used when sending the request. /// /// A task that represents the asynchronous get operation. The task result contains a rest DM channel where the user is the recipient. /// public Task GetOrCreateDMChannelAsync(RequestOptions options = null) => UserHelper.CreateDMChannelAsync(this, Discord, options); /// public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) => CDN.GetUserAvatarUrl(Id, AvatarId, size, format); /// public string GetDefaultAvatarUrl() => CDN.GetDefaultUserAvatarUrl(DiscriminatorValue); /// /// Gets the Username#Discriminator of the user. /// /// /// A string that resolves to Username#Discriminator of the user. /// public override string ToString() => $"{Username}#{Discriminator}"; private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")})"; //IUser /// async Task IUser.GetOrCreateDMChannelAsync(RequestOptions options) => await GetOrCreateDMChannelAsync(options).ConfigureAwait(false); } }