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 9.4 kB

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