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.2 kB

8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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, Embed embed = null, RequestOptions options = null)
  74. => ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, embed, options);
  75. #endif
  76. public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null)
  77. => ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, embed, options);
  78. public Task TriggerTypingAsync(RequestOptions options = null)
  79. => ChannelHelper.TriggerTypingAsync(this, Discord, options);
  80. public IDisposable EnterTypingState(RequestOptions options = null)
  81. => ChannelHelper.EnterTypingState(this, Discord, options);
  82. public override string ToString() => Name;
  83. private string DebuggerDisplay => $"{Name} ({Id}, Group)";
  84. //ISocketPrivateChannel
  85. IReadOnlyCollection<RestUser> IRestPrivateChannel.Recipients => Recipients;
  86. //IPrivateChannel
  87. IReadOnlyCollection<IUser> IPrivateChannel.Recipients => Recipients;
  88. //IMessageChannel
  89. async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options)
  90. {
  91. if (mode == CacheMode.AllowDownload)
  92. return await GetMessageAsync(id, options).ConfigureAwait(false);
  93. else
  94. return null;
  95. }
  96. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, RequestOptions options)
  97. {
  98. if (mode == CacheMode.AllowDownload)
  99. return GetMessagesAsync(limit, options);
  100. else
  101. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  102. }
  103. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode, RequestOptions options)
  104. {
  105. if (mode == CacheMode.AllowDownload)
  106. return GetMessagesAsync(fromMessageId, dir, limit, options);
  107. else
  108. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  109. }
  110. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(IMessage fromMessage, Direction dir, int limit, CacheMode mode, RequestOptions options)
  111. {
  112. if (mode == CacheMode.AllowDownload)
  113. return GetMessagesAsync(fromMessage, dir, limit, options);
  114. else
  115. return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
  116. }
  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, Embed embed, RequestOptions options)
  121. => await SendFileAsync(filePath, text, isTTS, embed, options).ConfigureAwait(false);
  122. #endif
  123. async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options)
  124. => await SendFileAsync(stream, filename, text, isTTS, embed, 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. //IAudioChannel
  130. Task<IAudioClient> IAudioChannel.ConnectAsync(Action<IAudioClient> configAction) { throw new NotSupportedException(); }
  131. //IChannel
  132. Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  133. => Task.FromResult<IUser>(GetUser(id));
  134. IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
  135. => ImmutableArray.Create<IReadOnlyCollection<IUser>>(Users).ToAsyncEnumerable();
  136. }
  137. }