|
|
@@ -0,0 +1,348 @@ |
|
|
|
using Discord.Rest; |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Collections.Immutable; |
|
|
|
using System.IO; |
|
|
|
using System.Linq; |
|
|
|
using System.Text; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Model = Discord.API.Channel; |
|
|
|
|
|
|
|
namespace Discord.WebSocket |
|
|
|
{ |
|
|
|
/// <summary> |
|
|
|
/// Represents a thread channel inside of a guild. |
|
|
|
/// </summary> |
|
|
|
public class SocketThreadChannel : SocketGuildChannel, IThreadChannel, ISocketMessageChannel |
|
|
|
{ |
|
|
|
/// <summary> |
|
|
|
/// <see langword="true"/> if this thread is private, otherwise <see langword="false"/> |
|
|
|
/// </summary> |
|
|
|
public bool IsPrivateThread { get; private set; } |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// Gets the parent channel this thread resides in. |
|
|
|
/// </summary> |
|
|
|
public SocketTextChannel ParentChannel { get; private set; } |
|
|
|
|
|
|
|
/// <inheritdoc/> |
|
|
|
public int MessageCount { get; private set; } |
|
|
|
|
|
|
|
/// <inheritdoc/> |
|
|
|
public int MemberCount { get; private set; } |
|
|
|
|
|
|
|
/// <inheritdoc/> |
|
|
|
public bool Archived { get; private set; } |
|
|
|
|
|
|
|
/// <inheritdoc/> |
|
|
|
public DateTimeOffset ArchiveTimestamp { get; private set; } |
|
|
|
|
|
|
|
/// <inheritdoc/> |
|
|
|
public ThreadArchiveDuration AutoArchiveDuration { get; private set; } |
|
|
|
|
|
|
|
/// <inheritdoc/> |
|
|
|
public bool Locked { get; private set; } |
|
|
|
|
|
|
|
/// <inheritdoc/> |
|
|
|
public bool IsNsfw { get; private set; } |
|
|
|
|
|
|
|
/// <inheritdoc/> |
|
|
|
public string Topic { get; private set; } |
|
|
|
|
|
|
|
/// <inheritdoc/> |
|
|
|
public int SlowModeInterval { get; private set; } |
|
|
|
|
|
|
|
/// <inheritdoc/> |
|
|
|
public string Mention { get; private set; } |
|
|
|
|
|
|
|
/// <inheritdoc/> |
|
|
|
public ulong? CategoryId { get; private set; } |
|
|
|
|
|
|
|
/// <inheritdoc /> |
|
|
|
public IReadOnlyCollection<SocketMessage> CachedMessages => _messages?.Messages ?? ImmutableArray.Create<SocketMessage>(); |
|
|
|
|
|
|
|
public new IReadOnlyCollection<SocketThreadUser> Users = ImmutableArray.Create<SocketThreadUser>(); |
|
|
|
|
|
|
|
private readonly MessageCache _messages; |
|
|
|
|
|
|
|
internal SocketThreadChannel(DiscordSocketClient discord, SocketGuild guild, ulong id, SocketTextChannel parent) |
|
|
|
: base(discord, id, guild) |
|
|
|
{ |
|
|
|
this.ParentChannel = parent; |
|
|
|
} |
|
|
|
|
|
|
|
internal static SocketThreadChannel Create(SocketGuild guild, ClientState state, Model model) |
|
|
|
{ |
|
|
|
var parent = (SocketTextChannel)guild.GetChannel(model.CategoryId.Value); |
|
|
|
var entity = new SocketThreadChannel(guild.Discord, guild, model.Id, parent); |
|
|
|
entity.Update(state, model); |
|
|
|
return entity; |
|
|
|
} |
|
|
|
|
|
|
|
internal override void Update(ClientState state, Model model) |
|
|
|
{ |
|
|
|
base.Update(state, model); |
|
|
|
|
|
|
|
this.MessageCount = model.MessageCount.GetValueOrDefault(-1); |
|
|
|
this.MemberCount = model.MemberCount.GetValueOrDefault(-1); |
|
|
|
|
|
|
|
this.IsPrivateThread = model.Type == ChannelType.PrivateThread; |
|
|
|
|
|
|
|
if (model.ThreadMetadata.IsSpecified) |
|
|
|
{ |
|
|
|
this.Archived = model.ThreadMetadata.Value.Archived; |
|
|
|
this.ArchiveTimestamp = model.ThreadMetadata.Value.ArchiveTimestamp; |
|
|
|
this.AutoArchiveDuration = (ThreadArchiveDuration)model.ThreadMetadata.Value.AutoArchiveDuration; |
|
|
|
this.Locked = model.ThreadMetadata.Value.Locked.GetValueOrDefault(false); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <inheritdoc /> |
|
|
|
public virtual Task SyncPermissionsAsync(RequestOptions options = null) |
|
|
|
=> ChannelHelper.SyncPermissionsAsync(this, Discord, options); |
|
|
|
|
|
|
|
/// <inheritdoc /> |
|
|
|
public Task ModifyAsync(Action<TextChannelProperties> func, RequestOptions options = null) |
|
|
|
=> ChannelHelper.ModifyAsync(this, Discord, func, options); |
|
|
|
|
|
|
|
//Messages |
|
|
|
/// <inheritdoc /> |
|
|
|
public SocketMessage GetCachedMessage(ulong id) |
|
|
|
=> _messages?.Get(id); |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// Gets a message from this message channel. |
|
|
|
/// </summary> |
|
|
|
/// <remarks> |
|
|
|
/// This method follows the same behavior as described in <see cref="IMessageChannel.GetMessageAsync"/>. |
|
|
|
/// Please visit its documentation for more details on this method. |
|
|
|
/// </remarks> |
|
|
|
/// <param name="id">The snowflake identifier of the message.</param> |
|
|
|
/// <param name="options">The options to be used when sending the request.</param> |
|
|
|
/// <returns> |
|
|
|
/// A task that represents an asynchronous get operation for retrieving the message. The task result contains |
|
|
|
/// the retrieved message; <c>null</c> if no message is found with the specified identifier. |
|
|
|
/// </returns> |
|
|
|
public async Task<IMessage> GetMessageAsync(ulong id, RequestOptions options = null) |
|
|
|
{ |
|
|
|
IMessage msg = _messages?.Get(id); |
|
|
|
if (msg == null) |
|
|
|
msg = await ChannelHelper.GetMessageAsync(this, Discord, id, options).ConfigureAwait(false); |
|
|
|
return msg; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// Gets the last N messages from this message channel. |
|
|
|
/// </summary> |
|
|
|
/// <remarks> |
|
|
|
/// This method follows the same behavior as described in <see cref="IMessageChannel.GetMessagesAsync(int, CacheMode, RequestOptions)"/>. |
|
|
|
/// Please visit its documentation for more details on this method. |
|
|
|
/// </remarks> |
|
|
|
/// <param name="limit">The numbers of message to be gotten from.</param> |
|
|
|
/// <param name="options">The options to be used when sending the request.</param> |
|
|
|
/// <returns> |
|
|
|
/// Paged collection of messages. |
|
|
|
/// </returns> |
|
|
|
public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null) |
|
|
|
=> SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit, CacheMode.AllowDownload, options); |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// Gets a collection of messages in this channel. |
|
|
|
/// </summary> |
|
|
|
/// <remarks> |
|
|
|
/// This method follows the same behavior as described in <see cref="IMessageChannel.GetMessagesAsync(ulong, Direction, int, CacheMode, RequestOptions)"/>. |
|
|
|
/// Please visit its documentation for more details on this method. |
|
|
|
/// </remarks> |
|
|
|
/// <param name="fromMessageId">The ID of the starting message to get the messages from.</param> |
|
|
|
/// <param name="dir">The direction of the messages to be gotten from.</param> |
|
|
|
/// <param name="limit">The numbers of message to be gotten from.</param> |
|
|
|
/// <param name="options">The options to be used when sending the request.</param> |
|
|
|
/// <returns> |
|
|
|
/// Paged collection of messages. |
|
|
|
/// </returns> |
|
|
|
public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null) |
|
|
|
=> SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit, CacheMode.AllowDownload, options); |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// Gets a collection of messages in this channel. |
|
|
|
/// </summary> |
|
|
|
/// <remarks> |
|
|
|
/// This method follows the same behavior as described in <see cref="IMessageChannel.GetMessagesAsync(IMessage, Direction, int, CacheMode, RequestOptions)"/>. |
|
|
|
/// Please visit its documentation for more details on this method. |
|
|
|
/// </remarks> |
|
|
|
/// <param name="fromMessage">The starting message to get the messages from.</param> |
|
|
|
/// <param name="dir">The direction of the messages to be gotten from.</param> |
|
|
|
/// <param name="limit">The numbers of message to be gotten from.</param> |
|
|
|
/// <param name="options">The options to be used when sending the request.</param> |
|
|
|
/// <returns> |
|
|
|
/// Paged collection of messages. |
|
|
|
/// </returns> |
|
|
|
public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null) |
|
|
|
=> SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessage.Id, dir, limit, CacheMode.AllowDownload, options); |
|
|
|
|
|
|
|
/// <inheritdoc /> |
|
|
|
public IReadOnlyCollection<SocketMessage> GetCachedMessages(int limit = DiscordConfig.MaxMessagesPerBatch) |
|
|
|
=> SocketChannelHelper.GetCachedMessages(this, Discord, _messages, null, Direction.Before, limit); |
|
|
|
|
|
|
|
/// <inheritdoc /> |
|
|
|
public IReadOnlyCollection<SocketMessage> GetCachedMessages(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch) |
|
|
|
=> SocketChannelHelper.GetCachedMessages(this, Discord, _messages, fromMessageId, dir, limit); |
|
|
|
|
|
|
|
/// <inheritdoc /> |
|
|
|
public IReadOnlyCollection<SocketMessage> GetCachedMessages(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch) |
|
|
|
=> SocketChannelHelper.GetCachedMessages(this, Discord, _messages, fromMessage.Id, dir, limit); |
|
|
|
|
|
|
|
/// <inheritdoc /> |
|
|
|
public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(RequestOptions options = null) |
|
|
|
=> ChannelHelper.GetPinnedMessagesAsync(this, Discord, options); |
|
|
|
|
|
|
|
/// <inheritdoc /> |
|
|
|
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception> |
|
|
|
public Task<RestUserMessage> SendMessageAsync(string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent component = null) |
|
|
|
=> ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, embed, allowedMentions, messageReference, component, options); |
|
|
|
|
|
|
|
/// <inheritdoc /> |
|
|
|
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent component = null) |
|
|
|
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, embed, allowedMentions, messageReference, component, options, isSpoiler); |
|
|
|
|
|
|
|
/// <inheritdoc /> |
|
|
|
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception> |
|
|
|
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent component = null) |
|
|
|
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, embed, allowedMentions, messageReference, component, options, isSpoiler); |
|
|
|
|
|
|
|
/// <inheritdoc /> |
|
|
|
public Task DeleteMessagesAsync(IEnumerable<IMessage> messages, RequestOptions options = null) |
|
|
|
=> ChannelHelper.DeleteMessagesAsync(this, Discord, messages.Select(x => x.Id), options); |
|
|
|
/// <inheritdoc /> |
|
|
|
public Task DeleteMessagesAsync(IEnumerable<ulong> messageIds, RequestOptions options = null) |
|
|
|
=> ChannelHelper.DeleteMessagesAsync(this, Discord, messageIds, options); |
|
|
|
|
|
|
|
/// <inheritdoc /> |
|
|
|
public async Task<IUserMessage> ModifyMessageAsync(ulong messageId, Action<MessageProperties> func, RequestOptions options = null) |
|
|
|
=> await ChannelHelper.ModifyMessageAsync(this, messageId, func, Discord, options).ConfigureAwait(false); |
|
|
|
|
|
|
|
/// <inheritdoc /> |
|
|
|
public Task DeleteMessageAsync(ulong messageId, RequestOptions options = null) |
|
|
|
=> ChannelHelper.DeleteMessageAsync(this, messageId, Discord, options); |
|
|
|
|
|
|
|
/// <inheritdoc /> |
|
|
|
public Task DeleteMessageAsync(IMessage message, RequestOptions options = null) |
|
|
|
=> ChannelHelper.DeleteMessageAsync(this, message.Id, Discord, options); |
|
|
|
|
|
|
|
/// <inheritdoc /> |
|
|
|
public Task TriggerTypingAsync(RequestOptions options = null) |
|
|
|
=> ChannelHelper.TriggerTypingAsync(this, Discord, options); |
|
|
|
|
|
|
|
/// <inheritdoc /> |
|
|
|
public IDisposable EnterTypingState(RequestOptions options = null) |
|
|
|
=> ChannelHelper.EnterTypingState(this, Discord, options); |
|
|
|
|
|
|
|
internal void AddMessage(SocketMessage msg) |
|
|
|
=> _messages?.Add(msg); |
|
|
|
|
|
|
|
internal SocketMessage RemoveMessage(ulong id) |
|
|
|
=> _messages?.Remove(id); |
|
|
|
|
|
|
|
//Users |
|
|
|
/// <inheritdoc /> |
|
|
|
public new SocketThreadUser GetUser(ulong id) |
|
|
|
{ |
|
|
|
var user = Users.FirstOrDefault(x => x.Id == id); |
|
|
|
return user; |
|
|
|
} |
|
|
|
|
|
|
|
public Task GetUsersAsync() |
|
|
|
{ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private string DebuggerDisplay => $"{Name} ({Id}, Thread)"; |
|
|
|
internal new SocketThreadChannel Clone() => MemberwiseClone() as SocketThreadChannel; |
|
|
|
|
|
|
|
//ITextChannel |
|
|
|
/// <inheritdoc /> |
|
|
|
Task<IWebhook> ITextChannel.CreateWebhookAsync(string name, Stream avatar, RequestOptions options) |
|
|
|
=> throw new NotSupportedException("Thread channels don't support webhooks"); |
|
|
|
/// <inheritdoc /> |
|
|
|
Task<IWebhook> ITextChannel.GetWebhookAsync(ulong id, RequestOptions options) |
|
|
|
=> throw new NotSupportedException("Thread channels don't support webhooks"); |
|
|
|
/// <inheritdoc /> |
|
|
|
Task<IReadOnlyCollection<IWebhook>> ITextChannel.GetWebhooksAsync(RequestOptions options) |
|
|
|
=> throw new NotSupportedException("Thread channels don't support webhooks"); |
|
|
|
|
|
|
|
//IGuildChannel |
|
|
|
/// <inheritdoc /> |
|
|
|
Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) |
|
|
|
=> Task.FromResult<IGuildUser>(GetUser(id)); |
|
|
|
/// <inheritdoc /> |
|
|
|
IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options) |
|
|
|
=> ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>(Users).ToAsyncEnumerable(); |
|
|
|
|
|
|
|
//IMessageChannel |
|
|
|
/// <inheritdoc /> |
|
|
|
async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options) |
|
|
|
{ |
|
|
|
if (mode == CacheMode.AllowDownload) |
|
|
|
return await GetMessageAsync(id, options).ConfigureAwait(false); |
|
|
|
else |
|
|
|
return GetCachedMessage(id); |
|
|
|
} |
|
|
|
/// <inheritdoc /> |
|
|
|
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, RequestOptions options) |
|
|
|
=> SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit, mode, options); |
|
|
|
/// <inheritdoc /> |
|
|
|
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode, RequestOptions options) |
|
|
|
=> SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit, mode, options); |
|
|
|
/// <inheritdoc /> |
|
|
|
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(IMessage fromMessage, Direction dir, int limit, CacheMode mode, RequestOptions options) |
|
|
|
=> SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessage.Id, dir, limit, mode, options); |
|
|
|
/// <inheritdoc /> |
|
|
|
async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options) |
|
|
|
=> await GetPinnedMessagesAsync(options).ConfigureAwait(false); |
|
|
|
|
|
|
|
/// <inheritdoc /> |
|
|
|
async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler, AllowedMentions allowedMentions, MessageReference messageReference, MessageComponent component) |
|
|
|
=> await SendFileAsync(filePath, text, isTTS, embed, options, isSpoiler, allowedMentions, messageReference, component).ConfigureAwait(false); |
|
|
|
/// <inheritdoc /> |
|
|
|
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler, AllowedMentions allowedMentions, MessageReference messageReference, MessageComponent component) |
|
|
|
=> await SendFileAsync(stream, filename, text, isTTS, embed, options, isSpoiler, allowedMentions, messageReference, component).ConfigureAwait(false); |
|
|
|
/// <inheritdoc /> |
|
|
|
async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, Embed embed, RequestOptions options, AllowedMentions allowedMentions, MessageReference messageReference, MessageComponent component) |
|
|
|
=> await SendMessageAsync(text, isTTS, embed, options, allowedMentions, messageReference, component).ConfigureAwait(false); |
|
|
|
|
|
|
|
// INestedChannel |
|
|
|
/// <inheritdoc /> |
|
|
|
Task<ICategoryChannel> INestedChannel.GetCategoryAsync(CacheMode mode, RequestOptions options) |
|
|
|
=> Task.FromResult(this.ParentChannel.Category); |
|
|
|
Task<IInviteMetadata> INestedChannel.CreateInviteAsync(int? maxAge, int? maxUses, bool isTemporary, bool isUnique, RequestOptions options) |
|
|
|
=> throw new NotSupportedException("Thread channels don't support invites"); |
|
|
|
Task<IInviteMetadata> INestedChannel.CreateInviteToApplicationAsync(ulong applicationId, int? maxAge, int? maxUses, bool isTemporary, bool isUnique, RequestOptions options) |
|
|
|
=> throw new NotSupportedException("Thread channels don't support invites"); |
|
|
|
Task<IInviteMetadata> INestedChannel.CreateInviteToStreamAsync(IUser user, int? maxAge, int? maxUses, bool isTemporary, bool isUnique, RequestOptions options) |
|
|
|
=> throw new NotSupportedException("Thread channels don't support invites"); |
|
|
|
Task<IReadOnlyCollection<IInviteMetadata>> INestedChannel.GetInvitesAsync(RequestOptions options) |
|
|
|
=> throw new NotSupportedException("Thread channels don't support invites"); |
|
|
|
|
|
|
|
public Task JoinAsync() |
|
|
|
{ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public Task LeaveAsync() |
|
|
|
{ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public Task AddThreadMember(IGuildUser user) |
|
|
|
{ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public Task RemoveThreadMember(IGuildUser user) |
|
|
|
{ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |