using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Discord.Rest;
using Model = Discord.API.User;
namespace Discord.WebSocket
{
///
/// Represents a WebSocket-based user.
///
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public abstract class SocketUser : SocketEntity, IUser
{
///
public abstract bool IsBot { get; internal set; }
///
public abstract string Username { get; internal set; }
///
public abstract ushort DiscriminatorValue { get; internal set; }
///
public abstract string AvatarId { get; internal set; }
///
public abstract bool IsWebhook { get; }
internal abstract SocketGlobalUser GlobalUser { get; }
internal abstract SocketPresence Presence { get; set; }
///
public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
///
public string Discriminator => DiscriminatorValue.ToString("D4");
///
public string Mention => MentionUtils.MentionUser(Id);
///
public IActivity Activity => Presence.Activity;
///
public UserStatus Status => Presence.Status;
///
public IImmutableSet ActiveClients => Presence.ActiveClients ?? ImmutableHashSet.Empty;
///
/// Gets mutual guilds shared with this user.
///
public IReadOnlyCollection MutualGuilds
=> Discord.Guilds.Where(g => g.Users.Any(u => u.Id == Id)).ToImmutableArray();
internal SocketUser(DiscordSocketClient discord, ulong id)
: base(discord, id)
{
}
internal virtual bool Update(ClientState state, Model model)
{
bool hasChanges = false;
if (model.Avatar.IsSpecified && model.Avatar.Value != AvatarId)
{
AvatarId = model.Avatar.Value;
hasChanges = true;
}
if (model.Discriminator.IsSpecified)
{
var newVal = ushort.Parse(model.Discriminator.Value, NumberStyles.None, CultureInfo.InvariantCulture);
if (newVal != DiscriminatorValue)
{
DiscriminatorValue = ushort.Parse(model.Discriminator.Value, NumberStyles.None, CultureInfo.InvariantCulture);
hasChanges = true;
}
}
if (model.Bot.IsSpecified && model.Bot.Value != IsBot)
{
IsBot = model.Bot.Value;
hasChanges = true;
}
if (model.Username.IsSpecified && model.Username.Value != Username)
{
Username = model.Username.Value;
hasChanges = true;
}
return hasChanges;
}
///
public async Task GetOrCreateDMChannelAsync(RequestOptions options = null)
=> GlobalUser.DMChannel ?? await UserHelper.CreateDMChannelAsync(this, Discord, options).ConfigureAwait(false) as IDMChannel;
///
public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
=> CDN.GetUserAvatarUrl(Id, AvatarId, size, format);
///
public string GetDefaultAvatarUrl()
=> CDN.GetDefaultUserAvatarUrl(DiscriminatorValue);
///
/// Gets the full name of the user (e.g. Example#0001).
///
///
/// The full name of the user.
///
public override string ToString() => $"{Username}#{Discriminator}";
private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")})";
internal SocketUser Clone() => MemberwiseClone() as SocketUser;
}
}