Interface IMessageChannel
Represents a generic channel that can send and receive messages.
Inherited Members
Namespace: Discord
Assembly: Discord.Net.Core.dll
Syntax
public interface IMessageChannel : IChannel, ISnowflakeEntity, IEntity<ulong>
Methods
| Improve this Doc View SourceDeleteMessageAsync(IMessage, RequestOptions)
Deletes a message based on the provided message in this channel.
Declaration
Task DeleteMessageAsync(IMessage message, RequestOptions options = null)
Parameters
| Type | Name | Description |
|---|---|---|
| IMessage | message | The message that would be removed. |
| RequestOptions | options | The options to be used when sending the request. |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task | A task that represents the asynchronous removal operation. |
DeleteMessageAsync(UInt64, RequestOptions)
Deletes a message.
Declaration
Task DeleteMessageAsync(ulong messageId, RequestOptions options = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.UInt64 | messageId | The snowflake identifier of the message that would be removed. |
| RequestOptions | options | The options to be used when sending the request. |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.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.
Declaration
IDisposable EnterTypingState(RequestOptions options = null)
Parameters
| Type | Name | Description |
|---|---|---|
| RequestOptions | options | The options to be used when sending the request. |
Returns
| Type | Description |
|---|---|
| System.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();
|
Improve this Doc
View Source
GetMessageAsync(UInt64, CacheMode, RequestOptions)
Gets a message from this message channel.
Declaration
Task<IMessage> GetMessageAsync(ulong id, CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.UInt64 | id | The snowflake identifier of the message. |
| CacheMode | mode | The CacheMode that determines whether the object should be fetched from cache. |
| RequestOptions | options | The options to be used when sending the request. |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task<IMessage> | A task that represents an asynchronous get operation for retrieving the message. The task result contains
the retrieved message; |
GetMessagesAsync(IMessage, Direction, Int32, CacheMode, RequestOptions)
Gets a collection of messages in this channel.
Declaration
IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = 100, CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null)
Parameters
| Type | Name | Description |
|---|---|---|
| IMessage | fromMessage | The starting message to get the messages from. |
| Direction | dir | The direction of the messages to be gotten from. |
| System.Int32 | limit | The numbers of message to be gotten from. |
| CacheMode | mode | The CacheMode that determines whether the object should be fetched from cache. |
| RequestOptions | options | The options to be used when sending the request. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IAsyncEnumerable<System.Collections.Generic.IReadOnlyCollection<IMessage>> | Paged collection of messages. |
Remarks
important
The returned collection is an asynchronous enumerable object; one must call FlattenAsync<T>(IAsyncEnumerable<IEnumerable<T>>) to access the individual messages as a collection.
warning
Do not fetch too many messages at once! This may cause unwanted preemptive rate limit or even actual rate limit, causing your bot to freeze!
limit around
the message fromMessage depending on the dir. The library will
attempt to split up the requests according to your limit and
MaxMessagesPerBatch. In other words, should the user request 500 messages,
and the MaxMessagesPerBatch constant is 100, the request will
be split into 5 individual requests; thus returning 5 individual asynchronous responses, hence the need
of flattening.
Examples
The following example gets 5 message prior to a specific message, oldMessage.
var oldMessage = await channel.SendMessageAsync("boi");
var messagesFromMsg = await channel.GetMessagesAsync(oldMessage, Direction.Before, 5).FlattenAsync();
|
Improve this Doc
View Source
GetMessagesAsync(Int32, CacheMode, RequestOptions)
Gets the last N messages from this message channel.
Declaration
IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(int limit = 100, CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | limit | The numbers of message to be gotten from. |
| CacheMode | mode | The CacheMode that determines whether the object should be fetched from cache. |
| RequestOptions | options | The options to be used when sending the request. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IAsyncEnumerable<System.Collections.Generic.IReadOnlyCollection<IMessage>> | Paged collection of messages. |
Remarks
important
The returned collection is an asynchronous enumerable object; one must call FlattenAsync<T>(IAsyncEnumerable<IEnumerable<T>>) to access the individual messages as a collection.
warning
Do not fetch too many messages at once! This may cause unwanted preemptive rate limit or even actual rate limit, causing your bot to freeze!
limit. The
library will attempt to split up the requests according to your limit and
MaxMessagesPerBatch. In other words, should the user request 500 messages,
and the MaxMessagesPerBatch constant is 100, the request will
be split into 5 individual requests; thus returning 5 individual asynchronous responses, hence the need
of flattening.
Examples
The following example downloads 300 messages and gets messages that belong to the user
53905483156684800.
var messages = await channel.GetMessagesAsync(300).FlattenAsync();
var userMessages = messages.Where(x => x.Author.Id == 53905483156684800);
|
Improve this Doc
View Source
GetMessagesAsync(UInt64, Direction, Int32, CacheMode, RequestOptions)
Gets a collection of messages in this channel.
Declaration
IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = 100, CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.UInt64 | fromMessageId | The ID of the starting message to get the messages from. |
| Direction | dir | The direction of the messages to be gotten from. |
| System.Int32 | limit | The numbers of message to be gotten from. |
| CacheMode | mode | The CacheMode that determines whether the object should be fetched from cache. |
| RequestOptions | options | The options to be used when sending the request. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IAsyncEnumerable<System.Collections.Generic.IReadOnlyCollection<IMessage>> | Paged collection of messages. |
Remarks
important
The returned collection is an asynchronous enumerable object; one must call FlattenAsync<T>(IAsyncEnumerable<IEnumerable<T>>) to access the individual messages as a collection.
warning
Do not fetch too many messages at once! This may cause unwanted preemptive rate limit or even actual rate limit, causing your bot to freeze!
limit around
the message fromMessageId depending on the dir. The library will
attempt to split up the requests according to your limit and
MaxMessagesPerBatch. In other words, should the user request 500 messages,
and the MaxMessagesPerBatch constant is 100, the request will
be split into 5 individual requests; thus returning 5 individual asynchronous responses, hence the need
of flattening.
Examples
The following example gets 5 message prior to the message identifier 442012544660537354.
await channel.GetMessagesAsync(442012544660537354, Direction.Before, 5).FlattenAsync();
The following example attempts to retrieve messageCount number of messages from the
beginning of the channel and prints them to the console.
public async Task PrintFirstMessages(IMessageChannel channel, int messageCount)
{
// Although the library does attempt to divide the messageCount by 100
// to comply to Discord's maximum message limit per request, sending
// too many could still cause the queue to clog up.
// The purpose of this exception is to discourage users from sending
// too many requests at once.
if (messageCount > 1000)
throw new InvalidOperationException("Too many messages requested.");
// Setting fromMessageId to 0 will make Discord
// default to the first message in channel.
var messages = await channel.GetMessagesAsync(
0, Direction.After, messageCount)
.FlattenAsync();
// Print message content
foreach (var message in messages)
Console.WriteLine($"{message.Author} posted '{message.Content}' at {message.CreatedAt}.");
}
| Improve this Doc View Source
GetPinnedMessagesAsync(RequestOptions)
Gets a collection of pinned messages in this channel.
Declaration
Task<IReadOnlyCollection<IMessage>> GetPinnedMessagesAsync(RequestOptions options = null)
Parameters
| Type | Name | Description |
|---|---|---|
| RequestOptions | options | The options to be used when sending the request. |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task<System.Collections.Generic.IReadOnlyCollection<IMessage>> | A task that represents the asynchronous get operation for retrieving pinned messages in this channel. The task result contains a collection of messages found in the pinned messages. |
SendFileAsync(Stream, String, String, Boolean, Embed, RequestOptions, Boolean)
Sends a file to this message channel with an optional caption.
Declaration
Task<IUserMessage> SendFileAsync(Stream stream, string filename, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false)
Parameters
| Type | Name | Description |
|---|---|---|
| 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 | |
| RequestOptions | options | The options to be used when sending the request. |
| System.Boolean | isSpoiler | Whether the message attachment should be hidden as a spoiler. |
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 sends a file as if you are uploading an attachment directly from your Discord client.
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());
|
Improve this Doc
View Source
SendFileAsync(String, String, Boolean, Embed, RequestOptions, Boolean)
Sends a file to this message channel with an optional caption.
Declaration
Task<IUserMessage> SendFileAsync(string filePath, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false)
Parameters
| Type | Name | Description |
|---|---|---|
| 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 | |
| RequestOptions | options | The options to be used when sending the request. |
| System.Boolean | isSpoiler | Whether the message attachment should be hidden as a spoiler. |
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 sends a file as if you are uploading an attachment directly from your Discord client.
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(String, Boolean, Embed, RequestOptions)
Sends a message to this message channel.
Declaration
Task<IUserMessage> SendMessageAsync(string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | text | The message to be sent. |
| System.Boolean | isTTS | Determines whether the message should be read aloud by Discord or not. |
| Embed | embed | |
| 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. |
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());
|
Improve this Doc
View Source
TriggerTypingAsync(RequestOptions)
Broadcasts the "user is typing" message to all users in this channel, lasting 10 seconds.
Declaration
Task TriggerTypingAsync(RequestOptions options = null)
Parameters
| Type | Name | Description |
|---|---|---|
| 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 that triggers the broadcast. |