Class SocketDMChannel
Represents a WebSocket-based direct-message channel.
public class SocketDMChannel : SocketChannel, IDMChannel, ISocketPrivateChannel, IPrivateChannel, ISocketMessageChannel, IMessageChannel, IChannel, ISnowflakeEntity, IEntity<ulong>
- Inheritance
-
SocketDMChannel
- Implements
- Inherited Members
- Extension Methods
Properties
CachedMessages
Gets all messages in this channel's cache.
public IReadOnlyCollection<SocketMessage> CachedMessages { get; }
Property Value
- IReadOnlyCollection<SocketMessage>
A read-only collection of WebSocket-based messages.
Recipient
Gets the recipient of the channel.
public SocketUser Recipient { get; }
Property Value
Users
Gets a collection that is the current logged-in user and the recipient.
public IReadOnlyCollection<SocketUser> Users { get; }
Property Value
Methods
CloseAsync(RequestOptions)
Closes this private channel, removing it from your channel list.
public Task CloseAsync(RequestOptions options = null)
Parameters
options
RequestOptionsThe options to be used when sending the request.
Returns
- Task
A task that represents the asynchronous close operation.
DeleteMessageAsync(IMessage, RequestOptions)
Deletes a message based on the provided message in this channel.
public Task DeleteMessageAsync(IMessage message, RequestOptions options = null)
Parameters
message
IMessageThe message that would be removed.
options
RequestOptionsThe options to be used when sending the request.
Returns
- Task
A task that represents the asynchronous removal operation.
DeleteMessageAsync(ulong, RequestOptions)
Deletes a message.
public Task DeleteMessageAsync(ulong messageId, RequestOptions options = null)
Parameters
messageId
ulongThe snowflake identifier of the message that would be removed.
options
RequestOptionsThe options to be used when sending the request.
Returns
- Task
A task that represents the asynchronous removal operation.
EnterTypingState(RequestOptions)
Continuously broadcasts the "user is typing" message to all users in this channel until the returned object is disposed.
public IDisposable EnterTypingState(RequestOptions options = null)
Parameters
options
RequestOptionsThe options to be used when sending the request.
Returns
- IDisposable
A disposable object that, upon its disposal, will stop the client from broadcasting its typing state in this channel.
Examples
The following example keeps the client in the typing state until LongRunningAsync
has finished.
using (channel.EnterTypingState())
await LongRunningAsync();
GetCachedMessage(ulong)
Gets a cached message from this channel.
public SocketMessage GetCachedMessage(ulong id)
Parameters
id
ulongThe snowflake identifier of the message.
Returns
- SocketMessage
A WebSocket-based message object; null if it does not exist in the cache or if caching is not enabled.
Remarks
warning
This method requires the use of cache, which is not enabled by default; if caching is not enabled, this method will always return null. Please refer to MessageCacheSize for more details.
This method retrieves the message from the local WebSocket cache and does not send any additional request to Discord. This message may be a message that has been deleted.
GetCachedMessages(IMessage, Direction, int)
Gets the last N cached messages starting from a certain message in this message channel.
public IReadOnlyCollection<SocketMessage> GetCachedMessages(IMessage fromMessage, Direction dir, int limit = 100)
Parameters
fromMessage
IMessageThe message to start the fetching from.
dir
DirectionThe direction of which the message should be gotten from.
limit
intThe number of messages to get.
Returns
- IReadOnlyCollection<SocketMessage>
A read-only collection of WebSocket-based messages.
Remarks
warning
This method requires the use of cache, which is not enabled by default; if caching is not enabled, this method will always return an empty collection. Please refer to MessageCacheSize for more details.
This method retrieves the message(s) from the local WebSocket cache and does not send any additional request to Discord. This read-only collection may include messages that have been deleted. The maximum number of messages that can be retrieved from this method depends on the MessageCacheSize set.
GetCachedMessages(int)
Gets the last N cached messages from this message channel.
public IReadOnlyCollection<SocketMessage> GetCachedMessages(int limit = 100)
Parameters
limit
intThe number of messages to get.
Returns
- IReadOnlyCollection<SocketMessage>
A read-only collection of WebSocket-based messages.
Remarks
warning
This method requires the use of cache, which is not enabled by default; if caching is not enabled, this method will always return an empty collection. Please refer to MessageCacheSize for more details.
This method retrieves the message(s) from the local WebSocket cache and does not send any additional request to Discord. This read-only collection may include messages that have been deleted. The maximum number of messages that can be retrieved from this method depends on the MessageCacheSize set.
GetCachedMessages(ulong, Direction, int)
Gets the last N cached messages starting from a certain message in this message channel.
public IReadOnlyCollection<SocketMessage> GetCachedMessages(ulong fromMessageId, Direction dir, int limit = 100)
Parameters
fromMessageId
ulongThe message ID to start the fetching from.
dir
DirectionThe direction of which the message should be gotten from.
limit
intThe number of messages to get.
Returns
- IReadOnlyCollection<SocketMessage>
A read-only collection of WebSocket-based messages.
Remarks
warning
This method requires the use of cache, which is not enabled by default; if caching is not enabled, this method will always return an empty collection. Please refer to MessageCacheSize for more details.
This method retrieves the message(s) from the local WebSocket cache and does not send any additional request to Discord. This read-only collection may include messages that have been deleted. The maximum number of messages that can be retrieved from this method depends on the MessageCacheSize set.
GetMessageAsync(ulong, RequestOptions)
Gets the message associated with the given id
.
public Task<IMessage> GetMessageAsync(ulong id, RequestOptions options = null)
Parameters
id
ulongTThe ID of the message.
options
RequestOptionsThe options to be used when sending the request.
Returns
GetMessagesAsync(IMessage, Direction, int, RequestOptions)
Gets a collection of messages in this channel.
public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = 100, RequestOptions options = null)
Parameters
fromMessage
IMessageThe starting message to get the messages from.
dir
DirectionThe direction of the messages to be gotten from.
limit
intThe numbers of message to be gotten from.
options
RequestOptionsThe options to be used when sending the request.
Returns
- IAsyncEnumerable<IReadOnlyCollection<IMessage>>
Paged collection of messages.
Remarks
This method follows the same behavior as described in GetMessagesAsync(IMessage, Direction, int, CacheMode, RequestOptions). Please visit its documentation for more details on this method.
GetMessagesAsync(int, RequestOptions)
Gets the last N messages from this message channel.
public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(int limit = 100, RequestOptions options = null)
Parameters
limit
intThe numbers of message to be gotten from.
options
RequestOptionsThe options to be used when sending the request.
Returns
- IAsyncEnumerable<IReadOnlyCollection<IMessage>>
Paged collection of messages.
Remarks
This method follows the same behavior as described in GetMessagesAsync(int, CacheMode, RequestOptions). Please visit its documentation for more details on this method.
GetMessagesAsync(ulong, Direction, int, RequestOptions)
Gets a collection of messages in this channel.
public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = 100, RequestOptions options = null)
Parameters
fromMessageId
ulongThe ID of the starting message to get the messages from.
dir
DirectionThe direction of the messages to be gotten from.
limit
intThe numbers of message to be gotten from.
options
RequestOptionsThe options to be used when sending the request.
Returns
- IAsyncEnumerable<IReadOnlyCollection<IMessage>>
Paged collection of messages.
Remarks
This method follows the same behavior as described in GetMessagesAsync(ulong, Direction, int, CacheMode, RequestOptions). Please visit its documentation for more details on this method.
GetPinnedMessagesAsync(RequestOptions)
Gets a read-only collection of pinned messages in this channel.
public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(RequestOptions options = null)
Parameters
options
RequestOptionsThe options to be used when sending the request.
Returns
- Task<IReadOnlyCollection<RestMessage>>
A task that represents the asynchronous get operation for retrieving pinned messages in this channel. The task result contains a read-only collection of messages found in the pinned messages.
Remarks
This method follows the same behavior as described in GetPinnedMessagesAsync(RequestOptions). Please visit its documentation for more details on this method.
GetUser(ulong)
Gets a user in this channel from the provided id
.
public SocketUser GetUser(ulong id)
Parameters
id
ulongThe snowflake identifier of the user.
Returns
- SocketUser
A SocketUser object that is a recipient of this channel; otherwise null.
ModifyMessageAsync(ulong, Action<MessageProperties>, RequestOptions)
Modifies a message.
public Task<IUserMessage> ModifyMessageAsync(ulong messageId, Action<MessageProperties> func, RequestOptions options = null)
Parameters
messageId
ulongThe snowflake identifier of the message that would be changed.
func
Action<MessageProperties>A delegate containing the properties to modify the message with.
options
RequestOptionsThe options to be used when sending the request.
Returns
- Task<IUserMessage>
A task that represents the asynchronous modification operation.
Remarks
This method modifies this message with the specified properties. To see an example of this method and what properties are available, please refer to MessageProperties.
SendFileAsync(FileAttachment, string, bool, Embed, RequestOptions, AllowedMentions, MessageReference, MessageComponent, ISticker[], Embed[], MessageFlags, PollProperties)
Sends a file to this message channel with an optional caption.
public Task<RestUserMessage> SendFileAsync(FileAttachment attachment, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null, MessageFlags flags = MessageFlags.None, PollProperties poll = null)
Parameters
attachment
FileAttachmentThe attachment containing the file and description.
text
stringThe message to be sent.
isTTS
boolWhether the message should be read aloud by Discord or not.
embed
Embedoptions
RequestOptionsThe options to be used when sending the request.
allowedMentions
AllowedMentionsSpecifies if notifications are sent for mentioned users and roles in the message
text
. If null, all mentioned roles and users will be notified.messageReference
MessageReferenceThe message references to be included. Used to reply to specific messages.
components
MessageComponentThe message components to be included with this message. Used for interactions.
stickers
ISticker[]A collection of stickers to send with the file.
embeds
Embed[]A array of Embeds to send with this response. Max 10.
flags
MessageFlagsA message flag to be applied to the sent message, only SuppressEmbeds and SuppressNotification is permitted.
poll
PollPropertiesA poll to send with the message.
Returns
- Task<RestUserMessage>
A task that represents an asynchronous send operation for delivering the message. The task result contains the sent message.
Remarks
This method sends a file as if you are uploading an attachment directly from your Discord client.
Exceptions
- ArgumentOutOfRangeException
Message content is too long, length must be less or equal to MaxMessageSize.
- ArgumentException
The only valid MessageFlags are SuppressEmbeds and None.
SendFileAsync(Stream, string, string, bool, Embed, RequestOptions, bool, AllowedMentions, MessageReference, MessageComponent, ISticker[], Embed[], MessageFlags, PollProperties)
Sends a file to this message channel with an optional caption.
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null, MessageFlags flags = MessageFlags.None, PollProperties poll = null)
Parameters
stream
StreamThe Stream of the file to be sent.
filename
stringThe name of the attachment.
text
stringThe message to be sent.
isTTS
boolWhether the message should be read aloud by Discord or not.
embed
Embedoptions
RequestOptionsThe options to be used when sending the request.
isSpoiler
boolWhether the message attachment should be hidden as a spoiler.
allowedMentions
AllowedMentionsSpecifies if notifications are sent for mentioned users and roles in the message
text
. If null, all mentioned roles and users will be notified.messageReference
MessageReferenceThe message references to be included. Used to reply to specific messages.
components
MessageComponentThe message components to be included with this message. Used for interactions.
stickers
ISticker[]A collection of stickers to send with the file.
embeds
Embed[]A array of Embeds to send with this response. Max 10.
flags
MessageFlagsA message flag to be applied to the sent message, only SuppressEmbeds and SuppressNotification is permitted.
poll
PollPropertiesA poll to send with the message.
Returns
- Task<RestUserMessage>
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.
using (var b1nzyStream = await httpClient.GetStreamAsync("https://example.com/b1nzy"))
await channel.SendFileAsync(b1nzyStream, "b1nzy.jpg",
embed: new EmbedBuilder { ImageUrl = "attachment://b1nzy.jpg" }.Build());
Remarks
This method sends a file as if you are uploading an attachment directly from your Discord client.
Exceptions
- ArgumentOutOfRangeException
Message content is too long, length must be less or equal to MaxMessageSize.
- ArgumentException
The only valid MessageFlags are SuppressEmbeds and None.
SendFileAsync(string, string, bool, Embed, RequestOptions, bool, AllowedMentions, MessageReference, MessageComponent, ISticker[], Embed[], MessageFlags, PollProperties)
Sends a file to this message channel with an optional caption.
public Task<RestUserMessage> SendFileAsync(string filePath, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null, MessageFlags flags = MessageFlags.None, PollProperties poll = null)
Parameters
filePath
stringThe file path of the file.
text
stringThe message to be sent.
isTTS
boolWhether the message should be read aloud by Discord or not.
embed
Embedoptions
RequestOptionsThe options to be used when sending the request.
isSpoiler
boolWhether the message attachment should be hidden as a spoiler.
allowedMentions
AllowedMentionsSpecifies if notifications are sent for mentioned users and roles in the message
text
. If null, all mentioned roles and users will be notified.messageReference
MessageReferenceThe message references to be included. Used to reply to specific messages.
components
MessageComponentThe message components to be included with this message. Used for interactions.
stickers
ISticker[]A collection of stickers to send with the file.
embeds
Embed[]A array of Embeds to send with this response. Max 10.
flags
MessageFlagsA message flag to be applied to the sent message, only SuppressEmbeds and SuppressNotification is permitted.
poll
PollPropertiesA poll to send with the message.
Returns
- Task<RestUserMessage>
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 sends a file as if you are uploading an attachment directly from your Discord client.
Exceptions
- ArgumentException
The only valid MessageFlags are SuppressEmbeds and None.
SendFilesAsync(IEnumerable<FileAttachment>, string, bool, Embed, RequestOptions, AllowedMentions, MessageReference, MessageComponent, ISticker[], Embed[], MessageFlags, PollProperties)
Sends a collection of files to this message channel.
public Task<RestUserMessage> SendFilesAsync(IEnumerable<FileAttachment> attachments, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null, MessageFlags flags = MessageFlags.None, PollProperties poll = null)
Parameters
attachments
IEnumerable<FileAttachment>A collection of attachments to upload.
text
stringThe message to be sent.
isTTS
boolWhether the message should be read aloud by Discord or not.
embed
Embedoptions
RequestOptionsThe options to be used when sending the request.
allowedMentions
AllowedMentionsSpecifies if notifications are sent for mentioned users and roles in the message
text
. If null, all mentioned roles and users will be notified.messageReference
MessageReferenceThe message references to be included. Used to reply to specific messages.
components
MessageComponentThe message components to be included with this message. Used for interactions.
stickers
ISticker[]A collection of stickers to send with the file.
embeds
Embed[]A array of Embeds to send with this response. Max 10.
flags
MessageFlagsA message flag to be applied to the sent message, only SuppressEmbeds and SuppressNotification is permitted.
poll
PollPropertiesA poll to send with the message.
Returns
- Task<RestUserMessage>
A task that represents an asynchronous send operation for delivering the message. The task result contains the sent message.
Remarks
This method sends files as if you are uploading attachments directly from your Discord client.
Exceptions
- ArgumentOutOfRangeException
Message content is too long, length must be less or equal to MaxMessageSize.
- ArgumentException
The only valid MessageFlags are SuppressEmbeds and None.
SendMessageAsync(string, bool, Embed, RequestOptions, AllowedMentions, MessageReference, MessageComponent, ISticker[], Embed[], MessageFlags, PollProperties)
Sends a message to this message channel.
public Task<RestUserMessage> SendMessageAsync(string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null, MessageFlags flags = MessageFlags.None, PollProperties poll = null)
Parameters
text
stringThe message to be sent.
isTTS
boolDetermines whether the message should be read aloud by Discord or not.
embed
Embedoptions
RequestOptionsThe options to be used when sending the request.
allowedMentions
AllowedMentionsSpecifies if notifications are sent for mentioned users and roles in the message
text
. If null, all mentioned roles and users will be notified.messageReference
MessageReferenceThe message references to be included. Used to reply to specific messages.
components
MessageComponentThe message components to be included with this message. Used for interactions.
stickers
ISticker[]A collection of stickers to send with the message.
embeds
Embed[]A array of Embeds to send with this response. Max 10.
flags
MessageFlagsA message flag to be applied to the sent message, only SuppressEmbeds and SuppressNotification is permitted.
poll
PollPropertiesA poll to send with the message.
Returns
- Task<RestUserMessage>
A task that represents an asynchronous send operation for delivering the message. The task result contains the sent message.
Examples
The following example sends a message with the current system time in RFC 1123 format to the channel and deletes itself after 5 seconds.
var message = await channel.SendMessageAsync(DateTimeOffset.UtcNow.ToString("R"));
await Task.Delay(TimeSpan.FromSeconds(5))
.ContinueWith(x => message.DeleteAsync());
Exceptions
- ArgumentOutOfRangeException
Message content is too long, length must be less or equal to MaxMessageSize.
- ArgumentException
The only valid MessageFlags are SuppressEmbeds and None.
ToString()
Returns the recipient user.
public override string ToString()
Returns
TriggerTypingAsync(RequestOptions)
Broadcasts the "user is typing" message to all users in this channel, lasting 10 seconds.
public Task TriggerTypingAsync(RequestOptions options = null)
Parameters
options
RequestOptionsThe options to be used when sending the request.
Returns
- Task
A task that represents the asynchronous operation that triggers the broadcast.