You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

RestTextChannel.cs 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using Model = Discord.API.Channel;
  8. namespace Discord.Rest
  9. {
  10. [DebuggerDisplay(@"{DebuggerDisplay,nq}")]
  11. public class RestTextChannel : RestGuildChannel, IRestMessageChannel, ITextChannel
  12. {
  13. public string Topic { get; private set; }
  14. public int SlowModeInterval { get; private set; }
  15. public ulong? CategoryId { get; private set; }
  16. public string Mention => MentionUtils.MentionChannel(Id);
  17. private bool _nsfw;
  18. public bool IsNsfw => _nsfw;
  19. internal RestTextChannel(BaseDiscordClient discord, IGuild guild, ulong id)
  20. : base(discord, guild, id)
  21. {
  22. }
  23. internal new static RestTextChannel Create(BaseDiscordClient discord, IGuild guild, Model model)
  24. {
  25. var entity = new RestTextChannel(discord, guild, model.Id);
  26. entity.Update(model);
  27. return entity;
  28. }
  29. internal override void Update(Model model)
  30. {
  31. base.Update(model);
  32. CategoryId = model.CategoryId;
  33. Topic = model.Topic.Value;
  34. SlowModeInterval = model.SlowMode.Value;
  35. _nsfw = model.Nsfw.GetValueOrDefault();
  36. }
  37. public async Task ModifyAsync(Action<TextChannelProperties> func, RequestOptions options = null)
  38. {
  39. var model = await ChannelHelper.ModifyAsync(this, Discord, func, options).ConfigureAwait(false);
  40. Update(model);
  41. }
  42. public Task<RestGuildUser> GetUserAsync(ulong id, RequestOptions options = null)
  43. => ChannelHelper.GetUserAsync(this, Guild, Discord, id, options);
  44. public IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync(RequestOptions options = null)
  45. => ChannelHelper.GetUsersAsync(this, Guild, Discord, null, null, options);
  46. public Task<RestMessage> GetMessageAsync(ulong id, RequestOptions options = null)
  47. => ChannelHelper.GetMessageAsync(this, Discord, id, options);
  48. public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  49. => ChannelHelper.GetMessagesAsync(this, Discord, null, Direction.Before, limit, options);
  50. public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  51. => ChannelHelper.GetMessagesAsync(this, Discord, fromMessageId, dir, limit, options);
  52. public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  53. => ChannelHelper.GetMessagesAsync(this, Discord, fromMessage.Id, dir, limit, options);
  54. public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(RequestOptions options = null)
  55. => ChannelHelper.GetPinnedMessagesAsync(this, Discord, options);
  56. public Task<RestUserMessage> SendMessageAsync(string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null)
  57. => ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, embed, options);
  58. public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null)
  59. => ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, embed, options);
  60. public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null)
  61. => ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, embed, options);
  62. public Task DeleteMessageAsync(ulong messageId, RequestOptions options = null)
  63. => ChannelHelper.DeleteMessageAsync(this, messageId, Discord, options);
  64. public Task DeleteMessageAsync(IMessage message, RequestOptions options = null)
  65. => ChannelHelper.DeleteMessageAsync(this, message.Id, Discord, options);
  66. public Task DeleteMessagesAsync(IEnumerable<IMessage> messages, RequestOptions options = null)
  67. => ChannelHelper.DeleteMessagesAsync(this, Discord, messages.Select(x => x.Id), options);
  68. public Task DeleteMessagesAsync(IEnumerable<ulong> messageIds, RequestOptions options = null)
  69. => ChannelHelper.DeleteMessagesAsync(this, Discord, messageIds, options);
  70. public Task TriggerTypingAsync(RequestOptions options = null)
  71. => ChannelHelper.TriggerTypingAsync(this, Discord, options);
  72. public IDisposable EnterTypingState(RequestOptions options = null)
  73. => ChannelHelper.EnterTypingState(this, Discord, options);
  74. public Task<RestWebhook> CreateWebhookAsync(string name, Stream avatar = null, RequestOptions options = null)
  75. => ChannelHelper.CreateWebhookAsync(this, Discord, name, avatar, options);
  76. public Task<RestWebhook> GetWebhookAsync(ulong id, RequestOptions options = null)
  77. => ChannelHelper.GetWebhookAsync(this, Discord, id, options);
  78. public Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(RequestOptions options = null)
  79. => ChannelHelper.GetWebhooksAsync(this, Discord, options);
  80. public Task<ICategoryChannel> GetCategoryAsync(RequestOptions options = null)
  81. => ChannelHelper.GetCategoryAsync(this, Discord, options);
  82. private string DebuggerDisplay => $"{Name} ({Id}, Text)";
  83. //ITextChannel
  84. async Task<IWebhook> ITextChannel.CreateWebhookAsync(string name, Stream avatar, RequestOptions options)
  85. => await CreateWebhookAsync(name, avatar, options).ConfigureAwait(false);
  86. async Task<IWebhook> ITextChannel.GetWebhookAsync(ulong id, RequestOptions options)
  87. => await GetWebhookAsync(id, options).ConfigureAwait(false);
  88. async Task<IReadOnlyCollection<IWebhook>> ITextChannel.GetWebhooksAsync(RequestOptions options)
  89. => await GetWebhooksAsync(options).ConfigureAwait(false);
  90. //IMessageChannel
  91. async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options)
  92. {
  93. if (mode == CacheMode.AllowDownload)
  94. return await GetMessageAsync(id, options).ConfigureAwait(false);
  95. else
  96. return null;
  97. }
  98. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, RequestOptions options)
  99. {
  100. if (mode == CacheMode.AllowDownload)
  101. return GetMessagesAsync(limit, options);
  102. else
  103. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  104. }
  105. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode, RequestOptions options)
  106. {
  107. if (mode == CacheMode.AllowDownload)
  108. return GetMessagesAsync(fromMessageId, dir, limit, options);
  109. else
  110. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  111. }
  112. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(IMessage fromMessage, Direction dir, int limit, CacheMode mode, RequestOptions options)
  113. {
  114. if (mode == CacheMode.AllowDownload)
  115. return GetMessagesAsync(fromMessage, dir, limit, options);
  116. else
  117. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  118. }
  119. async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options)
  120. => await GetPinnedMessagesAsync(options).ConfigureAwait(false);
  121. async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, Embed embed, RequestOptions options)
  122. => await SendFileAsync(filePath, text, isTTS, embed, options).ConfigureAwait(false);
  123. async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options)
  124. => await SendFileAsync(stream, filename, text, isTTS, embed, options).ConfigureAwait(false);
  125. async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, Embed embed, RequestOptions options)
  126. => await SendMessageAsync(text, isTTS, embed, options).ConfigureAwait(false);
  127. IDisposable IMessageChannel.EnterTypingState(RequestOptions options)
  128. => EnterTypingState(options);
  129. //IGuildChannel
  130. async Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  131. {
  132. if (mode == CacheMode.AllowDownload)
  133. return await GetUserAsync(id, options).ConfigureAwait(false);
  134. else
  135. return null;
  136. }
  137. IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
  138. {
  139. if (mode == CacheMode.AllowDownload)
  140. return GetUsersAsync(options);
  141. else
  142. return AsyncEnumerable.Empty<IReadOnlyCollection<IGuildUser>>();
  143. }
  144. //IChannel
  145. async Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  146. {
  147. if (mode == CacheMode.AllowDownload)
  148. return await GetUserAsync(id, options).ConfigureAwait(false);
  149. else
  150. return null;
  151. }
  152. IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
  153. {
  154. if (mode == CacheMode.AllowDownload)
  155. return GetUsersAsync(options);
  156. else
  157. return AsyncEnumerable.Empty<IReadOnlyCollection<IGuildUser>>();
  158. }
  159. // INestedChannel
  160. async Task<ICategoryChannel> INestedChannel.GetCategoryAsync(CacheMode mode, RequestOptions options)
  161. {
  162. if (CategoryId.HasValue && mode == CacheMode.AllowDownload)
  163. return (await Guild.GetChannelAsync(CategoryId.Value, mode, options).ConfigureAwait(false)) as ICategoryChannel;
  164. return null;
  165. }
  166. }
  167. }