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.

RestGroupChannel.cs 8.5 kB

8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using Discord.Audio;
  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.Rest
  11. {
  12. [DebuggerDisplay(@"{DebuggerDisplay,nq}")]
  13. public class RestGroupChannel : RestChannel, IGroupChannel, IRestPrivateChannel, IRestMessageChannel, IRestAudioChannel, IUpdateable
  14. {
  15. private string _iconId;
  16. private ImmutableDictionary<ulong, RestGroupUser> _users;
  17. public string Name { get; private set; }
  18. public IReadOnlyCollection<RestGroupUser> Users => _users.ToReadOnlyCollection();
  19. public IReadOnlyCollection<RestGroupUser> Recipients
  20. => _users.Select(x => x.Value).Where(x => x.Id != Discord.CurrentUser.Id).ToReadOnlyCollection(() => _users.Count - 1);
  21. internal RestGroupChannel(BaseDiscordClient discord, ulong id)
  22. : base(discord, id)
  23. {
  24. }
  25. internal new static RestGroupChannel Create(BaseDiscordClient discord, Model model)
  26. {
  27. var entity = new RestGroupChannel(discord, model.Id);
  28. entity.Update(model);
  29. return entity;
  30. }
  31. internal override void Update(Model model)
  32. {
  33. if (model.Name.IsSpecified)
  34. Name = model.Name.Value;
  35. if (model.Icon.IsSpecified)
  36. _iconId = model.Icon.Value;
  37. if (model.Recipients.IsSpecified)
  38. UpdateUsers(model.Recipients.Value);
  39. }
  40. internal void UpdateUsers(API.User[] models)
  41. {
  42. var users = ImmutableDictionary.CreateBuilder<ulong, RestGroupUser>();
  43. for (int i = 0; i < models.Length; i++)
  44. users[models[i].Id] = RestGroupUser.Create(Discord, models[i]);
  45. _users = users.ToImmutable();
  46. }
  47. public override async Task UpdateAsync(RequestOptions options = null)
  48. {
  49. var model = await Discord.ApiClient.GetChannelAsync(Id, options).ConfigureAwait(false);
  50. Update(model);
  51. }
  52. public Task LeaveAsync(RequestOptions options = null)
  53. => ChannelHelper.DeleteAsync(this, Discord, options);
  54. public RestUser GetUser(ulong id)
  55. {
  56. if (_users.TryGetValue(id, out RestGroupUser user))
  57. return user;
  58. return null;
  59. }
  60. public Task<RestMessage> GetMessageAsync(ulong id, RequestOptions options = null)
  61. => ChannelHelper.GetMessageAsync(this, Discord, id, options);
  62. public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  63. => ChannelHelper.GetMessagesAsync(this, Discord, null, Direction.Before, limit, options);
  64. public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  65. => ChannelHelper.GetMessagesAsync(this, Discord, fromMessageId, dir, limit, options);
  66. public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  67. => ChannelHelper.GetMessagesAsync(this, Discord, fromMessage.Id, dir, limit, options);
  68. public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(RequestOptions options = null)
  69. => ChannelHelper.GetPinnedMessagesAsync(this, Discord, options);
  70. public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS = false, Embed embed = null, RequestOptions options = null)
  71. => ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, embed, options);
  72. #if FILESYSTEM
  73. public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, RequestOptions options = null)
  74. => ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, options);
  75. #endif
  76. public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, RequestOptions options = null)
  77. => ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, options);
  78. public Task DeleteMessagesAsync(IEnumerable<IMessage> messages, RequestOptions options = null)
  79. => ChannelHelper.DeleteMessagesAsync(this, Discord, messages.Select(x => x.Id).ToArray(), options);
  80. public Task DeleteMessagesAsync(IEnumerable<ulong> messageIds, RequestOptions options = null)
  81. => ChannelHelper.DeleteMessagesAsync(this, Discord, messageIds, options);
  82. public Task TriggerTypingAsync(RequestOptions options = null)
  83. => ChannelHelper.TriggerTypingAsync(this, Discord, options);
  84. public IDisposable EnterTypingState(RequestOptions options = null)
  85. => ChannelHelper.EnterTypingState(this, Discord, options);
  86. public override string ToString() => Name;
  87. private string DebuggerDisplay => $"{Name} ({Id}, Group)";
  88. //ISocketPrivateChannel
  89. IReadOnlyCollection<RestUser> IRestPrivateChannel.Recipients => Recipients;
  90. //IPrivateChannel
  91. IReadOnlyCollection<IUser> IPrivateChannel.Recipients => Recipients;
  92. //IMessageChannel
  93. async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options)
  94. {
  95. if (mode == CacheMode.AllowDownload)
  96. return await GetMessageAsync(id, options).ConfigureAwait(false);
  97. else
  98. return null;
  99. }
  100. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, RequestOptions options)
  101. {
  102. if (mode == CacheMode.AllowDownload)
  103. return GetMessagesAsync(limit, options);
  104. else
  105. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  106. }
  107. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode, RequestOptions options)
  108. {
  109. if (mode == CacheMode.AllowDownload)
  110. return GetMessagesAsync(fromMessageId, dir, limit, options);
  111. else
  112. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  113. }
  114. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(IMessage fromMessage, Direction dir, int limit, CacheMode mode, RequestOptions options)
  115. {
  116. if (mode == CacheMode.AllowDownload)
  117. return GetMessagesAsync(fromMessage, dir, limit, options);
  118. else
  119. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  120. }
  121. async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options)
  122. => await GetPinnedMessagesAsync(options).ConfigureAwait(false);
  123. #if FILESYSTEM
  124. async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, RequestOptions options)
  125. => await SendFileAsync(filePath, text, isTTS, options).ConfigureAwait(false);
  126. #endif
  127. async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, RequestOptions options)
  128. => await SendFileAsync(stream, filename, text, isTTS, options).ConfigureAwait(false);
  129. async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, Embed embed, RequestOptions options)
  130. => await SendMessageAsync(text, isTTS, embed, options).ConfigureAwait(false);
  131. IDisposable IMessageChannel.EnterTypingState(RequestOptions options)
  132. => EnterTypingState(options);
  133. //IAudioChannel
  134. Task<IAudioClient> IAudioChannel.ConnectAsync(Action<IAudioClient> configAction) { throw new NotSupportedException(); }
  135. //IChannel
  136. Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  137. => Task.FromResult<IUser>(GetUser(id));
  138. IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
  139. => ImmutableArray.Create<IReadOnlyCollection<IUser>>(Users).ToAsyncEnumerable();
  140. }
  141. }