Table of Contents

Class SocketUser

Namespace
Discord.WebSocket
Assembly
Discord.Net.WebSocket.dll

Represents a WebSocket-based user.

public abstract class SocketUser : SocketEntity<ulong>, IUser, ISnowflakeEntity, IEntity<ulong>, IMentionable, IPresence
Inheritance
SocketUser
Implements
Derived
Inherited Members
Extension Methods

Properties

ActiveClients

Gets the set of clients where this user is currently active.

public IReadOnlyCollection<ClientType> ActiveClients { get; }

Property Value

IReadOnlyCollection<ClientType>

Activities

Gets the list of activities that this user currently has available.

public IReadOnlyCollection<IActivity> Activities { get; }

Property Value

IReadOnlyCollection<IActivity>

AvatarDecorationHash

Gets the hash of the avatar decoration.

public string AvatarDecorationHash { get; }

Property Value

string

Remarks

null if the user has no avatar decoration set.

AvatarDecorationSkuId

Gets the id of the avatar decoration's SKU.

public ulong? AvatarDecorationSkuId { get; }

Property Value

ulong?

Remarks

null if the user has no avatar decoration set.

AvatarId

Gets the identifier of this user's avatar.

public abstract string AvatarId { get; }

Property Value

string

CreatedAt

Gets when the snowflake was created.

public DateTimeOffset CreatedAt { get; }

Property Value

DateTimeOffset

A DateTimeOffset representing when the entity was first created.

Discriminator

Gets the per-username unique ID for this user. This will return "0000" for users who have migrated to new username system.

public string Discriminator { get; }

Property Value

string

DiscriminatorValue

Gets the per-username unique ID for this user. This will return 0 for users who have migrated to new username system.

public abstract ushort DiscriminatorValue { get; }

Property Value

ushort

GlobalName

Gets the user's display name, if it is set. For bots, this will get the application name.

public abstract string GlobalName { get; }

Property Value

string

Remarks

This property will be null if user has no display name set.

IsBot

Gets a value that indicates whether this user is identified as a bot.

public abstract bool IsBot { get; }

Property Value

bool

true if the user is a bot application; otherwise false.

Remarks

This property retrieves a value that indicates whether this user is a registered bot application (indicated by the blue BOT tag within the official chat client).

IsWebhook

Gets a value that indicates whether this user is a webhook user.

public abstract bool IsWebhook { get; }

Property Value

bool

true if the user is a webhook; otherwise false.

Mention

Returns a special string used to mention this object.

public string Mention { get; }

Property Value

string

A string that is recognized by Discord as a mention (e.g. <@168693960628371456>).

MutualGuilds

Gets mutual guilds shared with this user.

public IReadOnlyCollection<SocketGuild> MutualGuilds { get; }

Property Value

IReadOnlyCollection<SocketGuild>

Remarks

This property will only include guilds in the same DiscordSocketClient.

PublicFlags

Gets the public flags that are applied to this user's account.

public UserProperties? PublicFlags { get; }

Property Value

UserProperties?

The value of public flags for this user.

Remarks

This value is determined by bitwise OR-ing UserProperties values together.

Status

Gets the current status of this user.

public UserStatus Status { get; }

Property Value

UserStatus

Username

Gets the username for this user.

public abstract string Username { get; }

Property Value

string

Methods

CreateDMChannelAsync(RequestOptions)

Creates the direct message channel of this user.

public Task<IDMChannel> CreateDMChannelAsync(RequestOptions options = null)

Parameters

options RequestOptions

The options to be used when sending the request.

Returns

Task<IDMChannel>

A task that represents the asynchronous operation for getting or creating a DM channel. The task result contains the DM channel associated with this user.

Examples

The following example attempts to send a direct message to the target user and logs the incident should it fail.

  public async Task MessageUserAsync(IUser user)
                    {
                        var channel = await user.CreateDMChannelAsync();
                        try
                        {
                            await channel.SendMessageAsync("Awesome stuff!");
                        }
                        catch (Discord.Net.HttpException ex) when (ex.HttpCode == HttpStatusCode.Forbidden)
                        {
                            Console.WriteLine($"Boo, I cannot message {user}.");
                        }
                    }

Remarks

This method is used to obtain or create a channel used to send a direct message.

warning

In event that the current user cannot send a message to the target user, a channel can and will still be created by Discord. However, attempting to send a message will yield a HttpException with a 403 as its HttpCode. There are currently no official workarounds by Discord.

GetAvatarDecorationUrl()

Gets the URL for user's avatar decoration.

public string GetAvatarDecorationUrl()

Returns

string

Remarks

null if the user has no avatar decoration set.

GetAvatarUrl(ImageFormat, ushort)

Gets the avatar URL for this user, if it is set.

public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)

Parameters

format ImageFormat

The format of the image.

size ushort

The size of the image that matches any power of two, ranging from 16 to 2048.

Returns

string

A string representing the user's avatar URL; null if the user has no avatar set.

Examples

The following example attempts to retrieve the user's current avatar and send it to a channel; if one is not set, a default avatar for this user will be returned instead.

  

Remarks

tip

If you wish to retrieve the display avatar for this user, consider using GetDisplayAvatarUrl(ImageFormat, ushort).

GetDefaultAvatarUrl()

Gets the default avatar URL for this user.

public string GetDefaultAvatarUrl()

Returns

string

A string representing the user's default avatar URL.

Remarks

This avatar is auto-generated by Discord and consists of their logo combined with a random background color.

note

The calculation is always done by taking the remainder of this user's DiscriminatorValue divided by 5.

GetDisplayAvatarUrl(ImageFormat, ushort)

Gets the display avatar URL for this user.

public virtual string GetDisplayAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)

Parameters

format ImageFormat

The format of the image.

size ushort

The size of the image that matches any power of two, ranging from 16 to 2048.

Returns

string

A string representing the user's display avatar URL.

Remarks

This method will return GetDefaultAvatarUrl() if the user has no avatar set.

ToString()

Gets the full name of the user (e.g. Example#0001).

public override string ToString()

Returns

string

The full name of the user.