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

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