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.

RestDMChannel.cs 11 kB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Immutable;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using Model = Discord.API.Channel;
  9. namespace Discord.Rest
  10. {
  11. /// <summary>
  12. /// Represents a REST-based direct-message channel.
  13. /// </summary>
  14. [DebuggerDisplay(@"{DebuggerDisplay,nq}")]
  15. public class RestDMChannel : RestChannel, IDMChannel, IRestPrivateChannel, IRestMessageChannel
  16. {
  17. public RestUser CurrentUser { get; }
  18. public RestUser Recipient { get; }
  19. public IReadOnlyCollection<RestUser> Users => ImmutableArray.Create(CurrentUser, Recipient);
  20. internal RestDMChannel(BaseDiscordClient discord, ulong id, ulong recipientId)
  21. : base(discord, id)
  22. {
  23. Recipient = new RestUser(Discord, recipientId);
  24. CurrentUser = new RestUser(Discord, discord.CurrentUser.Id);
  25. }
  26. internal new static RestDMChannel Create(BaseDiscordClient discord, Model model)
  27. {
  28. var entity = new RestDMChannel(discord, model.Id, model.Recipients.Value[0].Id);
  29. entity.Update(model);
  30. return entity;
  31. }
  32. internal override void Update(Model model)
  33. {
  34. Recipient.Update(model.Recipients.Value[0]);
  35. }
  36. /// <inheritdoc />
  37. public override async Task UpdateAsync(RequestOptions options = null)
  38. {
  39. var model = await Discord.ApiClient.GetChannelAsync(Id, options).ConfigureAwait(false);
  40. Update(model);
  41. }
  42. /// <inheritdoc />
  43. public Task CloseAsync(RequestOptions options = null)
  44. => ChannelHelper.DeleteAsync(this, Discord, options);
  45. /// <summary>
  46. /// Gets a user in this channel from the provided <paramref name="id"/>.
  47. /// </summary>
  48. /// <param name="id">The snowflake identifier of the user.</param>
  49. /// <returns>
  50. /// A <see cref="RestUser"/> object that is a recipient of this channel; otherwise <c>null</c>.
  51. /// </returns>
  52. public RestUser GetUser(ulong id)
  53. {
  54. if (id == Recipient.Id)
  55. return Recipient;
  56. else if (id == Discord.CurrentUser.Id)
  57. return CurrentUser;
  58. else
  59. return null;
  60. }
  61. /// <inheritdoc />
  62. public Task<RestMessage> GetMessageAsync(ulong id, RequestOptions options = null)
  63. => ChannelHelper.GetMessageAsync(this, Discord, id, options);
  64. /// <inheritdoc />
  65. public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  66. => ChannelHelper.GetMessagesAsync(this, Discord, null, Direction.Before, limit, options);
  67. /// <inheritdoc />
  68. public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  69. => ChannelHelper.GetMessagesAsync(this, Discord, fromMessageId, dir, limit, options);
  70. /// <inheritdoc />
  71. public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  72. => ChannelHelper.GetMessagesAsync(this, Discord, fromMessage.Id, dir, limit, options);
  73. /// <inheritdoc />
  74. public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(RequestOptions options = null)
  75. => ChannelHelper.GetPinnedMessagesAsync(this, Discord, options);
  76. /// <inheritdoc />
  77. /// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
  78. public Task<RestUserMessage> SendMessageAsync(string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null)
  79. => ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, embed, options);
  80. /// <inheritdoc />
  81. /// <exception cref="ArgumentException">
  82. /// <paramref name="filePath" /> is a zero-length string, contains only white space, or contains one or more
  83. /// invalid characters as defined by <see cref="System.IO.Path.GetInvalidPathChars"/>.
  84. /// </exception>
  85. /// <exception cref="ArgumentNullException">
  86. /// <paramref name="filePath" /> is <c>null</c>.
  87. /// </exception>
  88. /// <exception cref="PathTooLongException">
  89. /// The specified path, file name, or both exceed the system-defined maximum length. For example, on
  90. /// Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260
  91. /// characters.
  92. /// </exception>
  93. /// <exception cref="DirectoryNotFoundException">
  94. /// The specified path is invalid, (for example, it is on an unmapped drive).
  95. /// </exception>
  96. /// <exception cref="UnauthorizedAccessException">
  97. /// <paramref name="filePath" /> specified a directory.-or- The caller does not have the required permission.
  98. /// </exception>
  99. /// <exception cref="FileNotFoundException">
  100. /// The file specified in <paramref name="filePath" /> was not found.
  101. /// </exception>
  102. /// <exception cref="NotSupportedException"><paramref name="filePath" /> is in an invalid format.</exception>
  103. /// <exception cref="IOException">An I/O error occurred while opening the file.</exception>
  104. /// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
  105. public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null)
  106. => ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, embed, options);
  107. /// <inheritdoc />
  108. /// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
  109. public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null)
  110. => ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, embed, options);
  111. /// <inheritdoc />
  112. public Task DeleteMessageAsync(ulong messageId, RequestOptions options = null)
  113. => ChannelHelper.DeleteMessageAsync(this, messageId, Discord, options);
  114. /// <inheritdoc />
  115. public Task DeleteMessageAsync(IMessage message, RequestOptions options = null)
  116. => ChannelHelper.DeleteMessageAsync(this, message.Id, Discord, options);
  117. /// <inheritdoc />
  118. public Task TriggerTypingAsync(RequestOptions options = null)
  119. => ChannelHelper.TriggerTypingAsync(this, Discord, options);
  120. /// <inheritdoc />
  121. public IDisposable EnterTypingState(RequestOptions options = null)
  122. => ChannelHelper.EnterTypingState(this, Discord, options);
  123. public override string ToString() => $"@{Recipient}";
  124. private string DebuggerDisplay => $"@{Recipient} ({Id}, DM)";
  125. //IDMChannel
  126. /// <inheritdoc />
  127. IUser IDMChannel.Recipient => Recipient;
  128. //IRestPrivateChannel
  129. /// <inheritdoc />
  130. IReadOnlyCollection<RestUser> IRestPrivateChannel.Recipients => ImmutableArray.Create(Recipient);
  131. //IPrivateChannel
  132. /// <inheritdoc />
  133. IReadOnlyCollection<IUser> IPrivateChannel.Recipients => ImmutableArray.Create<IUser>(Recipient);
  134. //IMessageChannel
  135. /// <inheritdoc />
  136. async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options)
  137. {
  138. if (mode == CacheMode.AllowDownload)
  139. return await GetMessageAsync(id, options).ConfigureAwait(false);
  140. else
  141. return null;
  142. }
  143. /// <inheritdoc />
  144. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, RequestOptions options)
  145. {
  146. if (mode == CacheMode.AllowDownload)
  147. return GetMessagesAsync(limit, options);
  148. else
  149. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  150. }
  151. /// <inheritdoc />
  152. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode, RequestOptions options)
  153. {
  154. if (mode == CacheMode.AllowDownload)
  155. return GetMessagesAsync(fromMessageId, dir, limit, options);
  156. else
  157. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  158. }
  159. /// <inheritdoc />
  160. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(IMessage fromMessage, Direction dir, int limit, CacheMode mode, RequestOptions options)
  161. {
  162. if (mode == CacheMode.AllowDownload)
  163. return GetMessagesAsync(fromMessage, dir, limit, options);
  164. else
  165. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  166. }
  167. /// <inheritdoc />
  168. async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options)
  169. => await GetPinnedMessagesAsync(options).ConfigureAwait(false);
  170. /// <inheritdoc />
  171. async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, Embed embed, RequestOptions options)
  172. => await SendFileAsync(filePath, text, isTTS, embed, options).ConfigureAwait(false);
  173. /// <inheritdoc />
  174. async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options)
  175. => await SendFileAsync(stream, filename, text, isTTS, embed, options).ConfigureAwait(false);
  176. /// <inheritdoc />
  177. async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, Embed embed, RequestOptions options)
  178. => await SendMessageAsync(text, isTTS, embed, options).ConfigureAwait(false);
  179. //IChannel
  180. /// <inheritdoc />
  181. string IChannel.Name => $"@{Recipient}";
  182. /// <inheritdoc />
  183. Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  184. => Task.FromResult<IUser>(GetUser(id));
  185. /// <inheritdoc />
  186. IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
  187. => ImmutableArray.Create<IReadOnlyCollection<IUser>>(Users).ToAsyncEnumerable();
  188. }
  189. }