Show / Hide Table of Contents

# Class UserExtensions

An extension class for various Discord user objects.

##### Inheritance

System.Object
UserExtensions

##### Inherited Members

System.Object.ToString()
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()

###### Namespace: Discord

###### Assembly: Discord.Net.Core.dll

##### Syntax

    public static class UserExtensions

### Methods

| Improve this Doc View Source

#### BanAsync(IGuildUser, Int32, String, RequestOptions)

Bans the user from the guild and optionally prunes their recent messages.

##### Declaration

    public static Task BanAsync(this IGuildUser user, int pruneDays = 0, string reason = null, RequestOptions options = null)

##### Parameters

Type | Name | Description
--- | --- | ---
IGuildUser | user | The user to ban.
System.Int32 | pruneDays | The number of days to remove messages from this `user` for - must be between [0, 7]
System.String | reason | The reason of the ban to be written in the audit log.
RequestOptions | options | The options to be used when sending the request.

##### Returns

Type | Description
--- | ---
System.Threading.Tasks.Task | A task that represents the asynchronous operation for banning a user.

##### Exceptions

Type | Condition
--- | ---
System.ArgumentException | `pruneDays` is not between 0 to 7.

| Improve this Doc View Source

#### SendFileAsync(IUser, Stream, String, String, Boolean, Embed, RequestOptions)

Sends a file to this message channel with an optional caption.

##### Declaration

    public static Task<IUserMessage> SendFileAsync(this IUser user, Stream stream, string filename, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null)

##### Parameters

Type | Name | Description
--- | --- | ---
IUser | user | The user to send the DM to.
System.IO.Stream | stream | The System.IO.Stream of the file to be sent.
System.String | filename | The name of the attachment.
System.String | text | The message to be sent.
System.Boolean | isTTS | Whether the message should be read aloud by Discord or not.
Embed | embed | The Rich Embed to be sent.
RequestOptions | options | The options to be used when sending the request.

##### Returns

Type | Description
--- | ---
System.Threading.Tasks.Task<IUserMessage> | A task that represents an asynchronous send operation for delivering the message. The task result contains the sent message.

##### Remarks

This method attempts to send an attachment as a direct-message to the user.

##### warning

Please note that this method will throw an HttpException if the user cannot receive DMs due to privacy reasons or if the user has the sender blocked.

You may want to consider catching for DiscordCode `50007` when using this method.

##### note

If you wish to upload an image and have it embedded in a Rich embed, you may upload the file and refer to the file with "attachment://filename.ext" in the ImageUrl. See the example section for its usage.

##### Examples

The following example uploads a streamed image that will be called `b1nzy.jpg` embedded inside a rich embed to the channel.

    await channel.SendFileAsync(b1nzyStream, "b1nzy.jpg",
        embed: new EmbedBuilder {ImageUrl = "attachment://b1nzy.jpg"}.Build());

| Improve this Doc View Source

#### SendFileAsync(IUser, String, String, Boolean, Embed, RequestOptions)

Sends a file via DM with an optional caption.

##### Declaration

    public static Task<IUserMessage> SendFileAsync(this IUser user, string filePath, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null)

##### Parameters

Type | Name | Description
--- | --- | ---
IUser | user | The user to send the DM to.
System.String | filePath | The file path of the file.
System.String | text | The message to be sent.
System.Boolean | isTTS | Whether the message should be read aloud by Discord or not.
Embed | embed | The Rich Embed to be sent.
RequestOptions | options | The options to be used when sending the request.

##### Returns

Type | Description
--- | ---
System.Threading.Tasks.Task<IUserMessage> | A task that represents an asynchronous send operation for delivering the message. The task result contains the sent message.

##### Remarks

This method attempts to send an attachment as a direct-message to the user.

##### warning

Please note that this method will throw an HttpException if the user cannot receive DMs due to privacy reasons or if the user has the sender blocked.

You may want to consider catching for DiscordCode `50007` when using this method.

##### note

If you wish to upload an image and have it embedded in a Rich embed, you may upload the file and refer to the file with "attachment://filename.ext" in the ImageUrl. See the example section for its usage.

##### Examples

The following example uploads a local file called `wumpus.txt` along with the text `good discord boi` to the channel.

    await channel.SendFileAsync("wumpus.txt", "good discord boi");

The following example uploads a local image called `b1nzy.jpg` embedded inside a rich embed to the channel.

    await channel.SendFileAsync("b1nzy.jpg",
        embed: new EmbedBuilder {ImageUrl = "attachment://b1nzy.jpg"}.Build());

| Improve this Doc View Source

#### SendMessageAsync(IUser, String, Boolean, Embed, RequestOptions)

Sends a message via DM.

##### Declaration

    public static Task<IUserMessage> SendMessageAsync(this IUser user, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null)

##### Parameters

Type | Name | Description
--- | --- | ---
IUser | user | The user to send the DM to.
System.String | text | The message to be sent.
System.Boolean | isTTS | Whether the message should be read aloud by Discord or not.
Embed | embed | The Rich Embed to be sent.
RequestOptions | options | The options to be used when sending the request.

##### Returns

Type | Description
--- | ---
System.Threading.Tasks.Task<IUserMessage> | A task that represents the asynchronous send operation. The task result contains the sent message.

##### Remarks

This method attempts to send a direct-message to the user.

##### warning

Please note that this method will throw an HttpException if the user cannot receive DMs due to privacy reasons or if the user has the sender blocked.

You may want to consider catching for DiscordCode `50007` when using this method.