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.

SocketDMChannel.cs 9.0 kB

8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using Discord.Rest;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Immutable;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using Model = Discord.API.Channel;
  10. namespace Discord.WebSocket
  11. {
  12. [DebuggerDisplay(@"{DebuggerDisplay,nq}")]
  13. public class SocketDMChannel : SocketChannel, IDMChannel, ISocketPrivateChannel, ISocketMessageChannel
  14. {
  15. private readonly MessageCache _messages;
  16. public SocketUser Recipient { get; private set; }
  17. public IReadOnlyCollection<SocketMessage> CachedMessages => _messages?.Messages ?? ImmutableArray.Create<SocketMessage>();
  18. public new IReadOnlyCollection<SocketUser> Users => ImmutableArray.Create(Discord.CurrentUser, Recipient);
  19. internal SocketDMChannel(DiscordSocketClient discord, ulong id, SocketGlobalUser recipient)
  20. : base(discord, id)
  21. {
  22. Recipient = recipient;
  23. recipient.GlobalUser.AddRef();
  24. if (Discord.MessageCacheSize > 0)
  25. _messages = new MessageCache(Discord, this);
  26. }
  27. internal static SocketDMChannel Create(DiscordSocketClient discord, ClientState state, Model model)
  28. {
  29. var entity = new SocketDMChannel(discord, model.Id, discord.GetOrCreateUser(state, model.Recipients.Value[0]));
  30. entity.Update(state, model);
  31. return entity;
  32. }
  33. internal override void Update(ClientState state, Model model)
  34. {
  35. Recipient.Update(state, model.Recipients.Value[0]);
  36. }
  37. public Task CloseAsync(RequestOptions options = null)
  38. => ChannelHelper.DeleteAsync(this, Discord, options);
  39. //Messages
  40. public SocketMessage GetCachedMessage(ulong id)
  41. => _messages?.Get(id);
  42. public async Task<IMessage> GetMessageAsync(ulong id, RequestOptions options = null)
  43. {
  44. IMessage msg = _messages?.Get(id);
  45. if (msg == null)
  46. msg = await ChannelHelper.GetMessageAsync(this, Discord, id, options).ConfigureAwait(false);
  47. return msg;
  48. }
  49. public IAsyncEnumerable<IMessage> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  50. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit, CacheMode.AllowDownload, options);
  51. public IAsyncEnumerable<IMessage> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  52. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit, CacheMode.AllowDownload, options);
  53. public IAsyncEnumerable<IMessage> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  54. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessage.Id, dir, limit, CacheMode.AllowDownload, options);
  55. public IReadOnlyCollection<SocketMessage> GetCachedMessages(int limit = DiscordConfig.MaxMessagesPerBatch)
  56. => SocketChannelHelper.GetCachedMessages(this, Discord, _messages, null, Direction.Before, limit);
  57. public IReadOnlyCollection<SocketMessage> GetCachedMessages(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
  58. => SocketChannelHelper.GetCachedMessages(this, Discord, _messages, fromMessageId, dir, limit);
  59. public IReadOnlyCollection<SocketMessage> GetCachedMessages(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
  60. => SocketChannelHelper.GetCachedMessages(this, Discord, _messages, fromMessage.Id, dir, limit);
  61. public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(RequestOptions options = null)
  62. => ChannelHelper.GetPinnedMessagesAsync(this, Discord, options);
  63. public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS = false, Embed embed = null, RequestOptions options = null)
  64. => ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, embed, options);
  65. #if FILESYSTEM
  66. public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, RequestOptions options = null)
  67. => ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, options);
  68. #endif
  69. public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, RequestOptions options = null)
  70. => ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, options);
  71. public Task DeleteMessagesAsync(IEnumerable<IMessage> messages, RequestOptions options = null)
  72. => ChannelHelper.DeleteMessagesAsync(this, Discord, messages, options);
  73. public Task TriggerTypingAsync(RequestOptions options = null)
  74. => ChannelHelper.TriggerTypingAsync(this, Discord, options);
  75. public IDisposable EnterTypingState(RequestOptions options = null)
  76. => ChannelHelper.EnterTypingState(this, Discord, options);
  77. internal void AddMessage(SocketMessage msg)
  78. => _messages?.Add(msg);
  79. internal SocketMessage RemoveMessage(ulong id)
  80. => _messages?.Remove(id);
  81. //Users
  82. public new SocketUser GetUser(ulong id)
  83. {
  84. if (id == Recipient.Id)
  85. return Recipient;
  86. else if (id == Discord.CurrentUser.Id)
  87. return Discord.CurrentUser as SocketSelfUser;
  88. else
  89. return null;
  90. }
  91. public override string ToString() => $"@{Recipient}";
  92. private string DebuggerDisplay => $"@{Recipient} ({Id}, DM)";
  93. internal new SocketDMChannel Clone() => MemberwiseClone() as SocketDMChannel;
  94. //SocketChannel
  95. internal override IReadOnlyCollection<SocketUser> GetUsersInternal() => Users;
  96. internal override SocketUser GetUserInternal(ulong id) => GetUser(id);
  97. //IDMChannel
  98. IUser IDMChannel.Recipient => Recipient;
  99. //ISocketPrivateChannel
  100. IReadOnlyCollection<SocketUser> ISocketPrivateChannel.Recipients => ImmutableArray.Create(Recipient);
  101. //IPrivateChannel
  102. IReadOnlyCollection<IUser> IPrivateChannel.Recipients => ImmutableArray.Create<IUser>(Recipient);
  103. //IMessageChannel
  104. async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options)
  105. {
  106. if (mode == CacheMode.AllowDownload)
  107. return await GetMessageAsync(id, options).ConfigureAwait(false);
  108. else
  109. return GetCachedMessage(id);
  110. }
  111. IAsyncEnumerable<IMessage> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, RequestOptions options)
  112. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit, mode, options);
  113. IAsyncEnumerable<IMessage> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode, RequestOptions options)
  114. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit, mode, options);
  115. IAsyncEnumerable<IMessage> IMessageChannel.GetMessagesAsync(IMessage fromMessage, Direction dir, int limit, CacheMode mode, RequestOptions options)
  116. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessage.Id, dir, limit, mode, options);
  117. async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options)
  118. => await GetPinnedMessagesAsync(options).ConfigureAwait(false);
  119. #if FILESYSTEM
  120. async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, RequestOptions options)
  121. => await SendFileAsync(filePath, text, isTTS, options).ConfigureAwait(false);
  122. #endif
  123. async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, RequestOptions options)
  124. => await SendFileAsync(stream, filename, text, isTTS, options).ConfigureAwait(false);
  125. async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, Embed embed, RequestOptions options)
  126. => await SendMessageAsync(text, isTTS, embed, options).ConfigureAwait(false);
  127. IDisposable IMessageChannel.EnterTypingState(RequestOptions options)
  128. => EnterTypingState(options);
  129. //IChannel
  130. string IChannel.Name => $"@{Recipient}";
  131. Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  132. => Task.FromResult<IUser>(GetUser(id));
  133. IAsyncEnumerable<IUser> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
  134. => ImmutableArray.Create<IUser>(Users.ToArray()).ToAsyncEnumerable();
  135. }
  136. }