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

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