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

8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. [DebuggerDisplay(@"{DebuggerDisplay,nq}")]
  12. public class RestDMChannel : RestChannel, IDMChannel, IRestPrivateChannel, IRestMessageChannel, IUpdateable
  13. {
  14. public RestUser CurrentUser { get; private set; }
  15. public RestUser Recipient { get; private set; }
  16. public IReadOnlyCollection<RestUser> Users => ImmutableArray.Create(CurrentUser, Recipient);
  17. internal RestDMChannel(BaseDiscordClient discord, ulong id, ulong recipientId)
  18. : base(discord, id)
  19. {
  20. Recipient = new RestUser(Discord, recipientId);
  21. CurrentUser = new RestUser(Discord, discord.CurrentUser.Id);
  22. }
  23. internal new static RestDMChannel Create(BaseDiscordClient discord, Model model)
  24. {
  25. var entity = new RestDMChannel(discord, model.Id, model.Recipients.Value[0].Id);
  26. entity.Update(model);
  27. return entity;
  28. }
  29. internal override void Update(Model model)
  30. {
  31. Recipient.Update(model.Recipients.Value[0]);
  32. }
  33. public override async Task UpdateAsync(RequestOptions options = null)
  34. {
  35. var model = await Discord.ApiClient.GetChannelAsync(Id, options).ConfigureAwait(false);
  36. Update(model);
  37. }
  38. public Task CloseAsync(RequestOptions options = null)
  39. => ChannelHelper.DeleteAsync(this, Discord, options);
  40. public RestUser GetUser(ulong id)
  41. {
  42. if (id == Recipient.Id)
  43. return Recipient;
  44. else if (id == Discord.CurrentUser.Id)
  45. return CurrentUser;
  46. else
  47. return null;
  48. }
  49. public Task<RestMessage> GetMessageAsync(ulong id, RequestOptions options = null)
  50. => ChannelHelper.GetMessageAsync(this, Discord, id, options);
  51. public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  52. => ChannelHelper.GetMessagesAsync(this, Discord, null, Direction.Before, limit, options);
  53. public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  54. => ChannelHelper.GetMessagesAsync(this, Discord, fromMessageId, dir, limit, options);
  55. public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  56. => ChannelHelper.GetMessagesAsync(this, Discord, fromMessage.Id, dir, limit, options);
  57. public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(RequestOptions options = null)
  58. => ChannelHelper.GetPinnedMessagesAsync(this, Discord, options);
  59. public Task<RestUserMessage> SendMessageAsync(string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null)
  60. => ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, embed, options);
  61. #if FILESYSTEM
  62. public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null)
  63. => ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, embed, options);
  64. #endif
  65. public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null)
  66. => ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, embed, options);
  67. public Task DeleteMessageAsync(ulong messageId, RequestOptions options = null)
  68. => ChannelHelper.DeleteMessageAsync(this, messageId, Discord, options);
  69. public Task DeleteMessageAsync(IMessage message, RequestOptions options = null)
  70. => ChannelHelper.DeleteMessageAsync(this, message.Id, Discord, options);
  71. public Task TriggerTypingAsync(RequestOptions options = null)
  72. => ChannelHelper.TriggerTypingAsync(this, Discord, options);
  73. public IDisposable EnterTypingState(RequestOptions options = null)
  74. => ChannelHelper.EnterTypingState(this, Discord, options);
  75. public override string ToString() => $"@{Recipient}";
  76. private string DebuggerDisplay => $"@{Recipient} ({Id}, DM)";
  77. //IDMChannel
  78. IUser IDMChannel.Recipient => Recipient;
  79. //IRestPrivateChannel
  80. IReadOnlyCollection<RestUser> IRestPrivateChannel.Recipients => ImmutableArray.Create(Recipient);
  81. //IPrivateChannel
  82. IReadOnlyCollection<IUser> IPrivateChannel.Recipients => ImmutableArray.Create<IUser>(Recipient);
  83. //IMessageChannel
  84. async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options)
  85. {
  86. if (mode == CacheMode.AllowDownload)
  87. return await GetMessageAsync(id, options).ConfigureAwait(false);
  88. else
  89. return null;
  90. }
  91. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, RequestOptions options)
  92. {
  93. if (mode == CacheMode.AllowDownload)
  94. return GetMessagesAsync(limit, options);
  95. else
  96. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  97. }
  98. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode, RequestOptions options)
  99. {
  100. if (mode == CacheMode.AllowDownload)
  101. return GetMessagesAsync(fromMessageId, dir, limit, options);
  102. else
  103. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  104. }
  105. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(IMessage fromMessage, Direction dir, int limit, CacheMode mode, RequestOptions options)
  106. {
  107. if (mode == CacheMode.AllowDownload)
  108. return GetMessagesAsync(fromMessage, dir, limit, options);
  109. else
  110. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  111. }
  112. async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options)
  113. => await GetPinnedMessagesAsync(options).ConfigureAwait(false);
  114. #if FILESYSTEM
  115. async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, Embed embed, RequestOptions options)
  116. => await SendFileAsync(filePath, text, isTTS, embed, options).ConfigureAwait(false);
  117. #endif
  118. async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options)
  119. => await SendFileAsync(stream, filename, text, isTTS, embed, options).ConfigureAwait(false);
  120. async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, Embed embed, RequestOptions options)
  121. => await SendMessageAsync(text, isTTS, embed, options).ConfigureAwait(false);
  122. IDisposable IMessageChannel.EnterTypingState(RequestOptions options)
  123. => EnterTypingState(options);
  124. //IChannel
  125. string IChannel.Name => $"@{Recipient}";
  126. Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  127. => Task.FromResult<IUser>(GetUser(id));
  128. IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
  129. => ImmutableArray.Create<IReadOnlyCollection<IUser>>(Users).ToAsyncEnumerable();
  130. }
  131. }