Table of Contents

Class UserExtensions

Namespace
Discord
Assembly
Discord.Net.Core.dll

An extension class for various Discord user objects.

public static class UserExtensions
Inheritance
UserExtensions
Inherited Members

Methods

BanAsync(IGuildUser, int, string, RequestOptions)

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

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

Parameters

user IGuildUser

The user to ban.

pruneDays int

The number of days to remove messages from this user for - must be between [0, 7]

reason string

The reason of the ban to be written in the audit log.

options RequestOptions

The options to be used when sending the request.

Returns

Task

A task that represents the asynchronous operation for banning a user.

Exceptions

ArgumentException

pruneDays is not between 0 to 7.

SendFileAsync(IUser, FileAttachment, string, bool, Embed, RequestOptions, MessageComponent, Embed[])

Sends a file via DM with an optional caption.

public static Task<IUserMessage> SendFileAsync(this IUser user, FileAttachment attachment, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, MessageComponent components = null, Embed[] embeds = null)

Parameters

user IUser

The user to send the DM to.

attachment FileAttachment

The attachment containing the file and description.

text string

The message to be sent.

isTTS bool

Whether the message should be read aloud by Discord or not.

embed Embed

The RichEmbed to be sent.

options RequestOptions

The options to be used when sending the request.

components MessageComponent

The message component to be included with this message. Used for interactions.

embeds Embed[]

A array of Embeds to send with this response. Max 10.

Returns

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 DiscordCode50007 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.

SendFileAsync(IUser, Stream, string, string, bool, Embed, RequestOptions, MessageComponent, Embed[])

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

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

Parameters

user IUser

The user to send the DM to.

stream Stream

The Stream of the file to be sent.

filename string

The name of the attachment.

text string

The message to be sent.

isTTS bool

Whether the message should be read aloud by Discord or not.

embed Embed

The RichEmbed to be sent.

options RequestOptions

The options to be used when sending the request.

components MessageComponent

The message component to be included with this message. Used for interactions.

embeds Embed[]

A array of Embeds to send with this response. Max 10.

Returns

Task<IUserMessage>

A task that represents an asynchronous send operation for delivering the message. The task result contains the sent message.

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());

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 DiscordCode50007 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.

SendFileAsync(IUser, string, string, bool, Embed, RequestOptions, MessageComponent, Embed[])

Sends a file via DM with an optional caption.

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

Parameters

user IUser

The user to send the DM to.

filePath string

The file path of the file.

text string

The message to be sent.

isTTS bool

Whether the message should be read aloud by Discord or not.

embed Embed

The RichEmbed to be sent.

options RequestOptions

The options to be used when sending the request.

components MessageComponent

The message component to be included with this message. Used for interactions.

embeds Embed[]

A array of Embeds to send with this response. Max 10.

Returns

Task<IUserMessage>

A task that represents an asynchronous send operation for delivering the message. The task result contains the sent message.

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());

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 DiscordCode50007 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.

SendFilesAsync(IUser, IEnumerable<FileAttachment>, string, bool, Embed, RequestOptions, MessageComponent, Embed[])

Sends a collection of files via DM.

public static Task<IUserMessage> SendFilesAsync(this IUser user, IEnumerable<FileAttachment> attachments, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, MessageComponent components = null, Embed[] embeds = null)

Parameters

user IUser

The user to send the DM to.

attachments IEnumerable<FileAttachment>

A collection of attachments to upload.

text string

The message to be sent.

isTTS bool

Whether the message should be read aloud by Discord or not.

embed Embed

The RichEmbed to be sent.

options RequestOptions

The options to be used when sending the request.

components MessageComponent

The message component to be included with this message. Used for interactions.

embeds Embed[]

A array of Embeds to send with this response. Max 10.

Returns

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 attachments 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 DiscordCode50007 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.

SendMessageAsync(IUser, string, bool, Embed, RequestOptions, AllowedMentions, MessageComponent, Embed[])

Sends a message via DM.

public static Task<IUserMessage> SendMessageAsync(this IUser user, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, AllowedMentions allowedMentions = null, MessageComponent components = null, Embed[] embeds = null)

Parameters

user IUser

The user to send the DM to.

text string

The message to be sent.

isTTS bool

Whether the message should be read aloud by Discord or not.

embed Embed

The RichEmbed to be sent.

options RequestOptions

The options to be used when sending the request.

allowedMentions AllowedMentions

Specifies if notifications are sent for mentioned users and roles in the message text. If null, all mentioned roles and users will be notified.

components MessageComponent

The message components to be included with this message. Used for interactions.

embeds Embed[]

A array of Embeds to send with this response. Max 10.

Returns

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 DiscordCode50007 when using this method.