Table of Contents

Interface ISocketMessageChannel

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

Represents a generic WebSocket-based channel that can send and receive messages.

public interface ISocketMessageChannel : IMessageChannel, IChannel, ISnowflakeEntity, IEntity<ulong>
Inherited Members
Extension Methods

Properties

CachedMessages

Gets all messages in this channel's cache.

IReadOnlyCollection<SocketMessage> CachedMessages { get; }

Property Value

IReadOnlyCollection<SocketMessage>

A read-only collection of WebSocket-based messages.

Methods

GetCachedMessage(ulong)

Gets a cached message from this channel.

SocketMessage GetCachedMessage(ulong id)

Parameters

id ulong

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

IReadOnlyCollection<SocketMessage> GetCachedMessages(IMessage fromMessage, Direction dir, int limit = 100)

Parameters

fromMessage IMessage

The message to start the fetching from.

dir Direction

The direction of which the message should be gotten from.

limit int

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

IReadOnlyCollection<SocketMessage> GetCachedMessages(int limit = 100)

Parameters

limit int

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

IReadOnlyCollection<SocketMessage> GetCachedMessages(ulong fromMessageId, Direction dir, int limit = 100)

Parameters

fromMessageId ulong

The message ID to start the fetching from.

dir Direction

The direction of which the message should be gotten from.

limit int

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

GetPinnedMessagesAsync(RequestOptions)

Gets a read-only collection of pinned messages in this channel.

Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(RequestOptions options = null)

Parameters

options RequestOptions

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

SendFileAsync(FileAttachment, string, bool, Embed, RequestOptions, AllowedMentions, MessageReference, MessageComponent, ISticker[], Embed[], MessageFlags, PollProperties)

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

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

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.

messageReference MessageReference

The message references to be included. Used to reply to specific messages.

components MessageComponent

The 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 MessageFlags

A message flag to be applied to the sent message, only SuppressEmbeds and SuppressNotification is permitted.

poll PollProperties

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

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

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

isSpoiler bool

Whether the message attachment should be hidden as a spoiler.

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.

messageReference MessageReference

The message references to be included. Used to reply to specific messages.

components MessageComponent

The 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 MessageFlags

A message flag to be applied to the sent message, only SuppressEmbeds and SuppressNotification is permitted.

poll PollProperties

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

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(string, string, bool, Embed, RequestOptions, bool, AllowedMentions, MessageReference, MessageComponent, ISticker[], Embed[], MessageFlags, PollProperties)

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

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

isSpoiler bool

Whether the message attachment should be hidden as a spoiler.

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.

messageReference MessageReference

The message references to be included. Used to reply to specific messages.

components MessageComponent

The 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 MessageFlags

A message flag to be applied to the sent message, only SuppressEmbeds and SuppressNotification is permitted.

poll PollProperties

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

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(IEnumerable<FileAttachment>, string, bool, Embed, RequestOptions, AllowedMentions, MessageReference, MessageComponent, ISticker[], Embed[], MessageFlags, PollProperties)

Sends a collection of files to this message channel.

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

messageReference MessageReference

The message references to be included. Used to reply to specific messages.

components MessageComponent

The 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 MessageFlags

A message flag to be applied to the sent message, only SuppressEmbeds and SuppressNotification is permitted.

poll PollProperties

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

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(string, bool, Embed, RequestOptions, AllowedMentions, MessageReference, MessageComponent, ISticker[], Embed[], MessageFlags, PollProperties)

Sends a message to this message channel.

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 string

The message to be sent.

isTTS bool

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

messageReference MessageReference

The message references to be included. Used to reply to specific messages.

components MessageComponent

The 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 MessageFlags

A message flag to be applied to the sent message, only SuppressEmbeds and SuppressNotification is permitted.

poll PollProperties

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