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.

SocketGroupChannel.cs 12 kB

8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using Discord.Audio;
  2. using Discord.Rest;
  3. using System;
  4. using System.Collections.Concurrent;
  5. using System.Collections.Generic;
  6. using System.Collections.Immutable;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. using Model = Discord.API.Channel;
  12. using UserModel = Discord.API.User;
  13. using VoiceStateModel = Discord.API.VoiceState;
  14. namespace Discord.WebSocket
  15. {
  16. [DebuggerDisplay(@"{DebuggerDisplay,nq}")]
  17. public class SocketGroupChannel : SocketChannel, IGroupChannel, ISocketPrivateChannel, ISocketMessageChannel, ISocketAudioChannel
  18. {
  19. private readonly MessageCache _messages;
  20. private string _iconId;
  21. private ConcurrentDictionary<ulong, SocketGroupUser> _users;
  22. private ConcurrentDictionary<ulong, SocketVoiceState> _voiceStates;
  23. public string Name { get; private set; }
  24. public IReadOnlyCollection<SocketMessage> CachedMessages => _messages?.Messages ?? ImmutableArray.Create<SocketMessage>();
  25. public new IReadOnlyCollection<SocketGroupUser> Users => _users.ToReadOnlyCollection();
  26. public IReadOnlyCollection<SocketGroupUser> Recipients
  27. => _users.Select(x => x.Value).Where(x => x.Id != Discord.CurrentUser.Id).ToReadOnlyCollection(() => _users.Count - 1);
  28. internal SocketGroupChannel(DiscordSocketClient discord, ulong id)
  29. : base(discord, id)
  30. {
  31. if (Discord.MessageCacheSize > 0)
  32. _messages = new MessageCache(Discord, this);
  33. _voiceStates = new ConcurrentDictionary<ulong, SocketVoiceState>(ConcurrentHashSet.DefaultConcurrencyLevel, 5);
  34. _users = new ConcurrentDictionary<ulong, SocketGroupUser>(ConcurrentHashSet.DefaultConcurrencyLevel, 5);
  35. }
  36. internal static SocketGroupChannel Create(DiscordSocketClient discord, ClientState state, Model model)
  37. {
  38. var entity = new SocketGroupChannel(discord, model.Id);
  39. entity.Update(state, model);
  40. return entity;
  41. }
  42. internal override void Update(ClientState state, Model model)
  43. {
  44. if (model.Name.IsSpecified)
  45. Name = model.Name.Value;
  46. if (model.Icon.IsSpecified)
  47. _iconId = model.Icon.Value;
  48. if (model.Recipients.IsSpecified)
  49. UpdateUsers(state, model.Recipients.Value);
  50. }
  51. private void UpdateUsers(ClientState state, UserModel[] models)
  52. {
  53. var users = new ConcurrentDictionary<ulong, SocketGroupUser>(ConcurrentHashSet.DefaultConcurrencyLevel, (int)(models.Length * 1.05));
  54. for (int i = 0; i < models.Length; i++)
  55. users[models[i].Id] = SocketGroupUser.Create(this, state, models[i]);
  56. _users = users;
  57. }
  58. public Task LeaveAsync(RequestOptions options = null)
  59. => ChannelHelper.DeleteAsync(this, Discord, options);
  60. public Task<IAudioClient> ConnectAsync()
  61. {
  62. throw new NotSupportedException("Voice is not yet supported for group channels.");
  63. }
  64. //Messages
  65. public SocketMessage GetCachedMessage(ulong id)
  66. => _messages?.Get(id);
  67. public async Task<IMessage> GetMessageAsync(ulong id, RequestOptions options = null)
  68. {
  69. IMessage msg = _messages?.Get(id);
  70. if (msg == null)
  71. msg = await ChannelHelper.GetMessageAsync(this, Discord, id, options).ConfigureAwait(false);
  72. return msg;
  73. }
  74. public IAsyncEnumerable<IMessage> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  75. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit, CacheMode.AllowDownload, options);
  76. public IAsyncEnumerable<IMessage> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  77. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit, CacheMode.AllowDownload, options);
  78. public IAsyncEnumerable<IMessage> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
  79. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessage.Id, dir, limit, CacheMode.AllowDownload, options);
  80. public IReadOnlyCollection<SocketMessage> GetCachedMessages(int limit = DiscordConfig.MaxMessagesPerBatch)
  81. => SocketChannelHelper.GetCachedMessages(this, Discord, _messages, null, Direction.Before, limit);
  82. public IReadOnlyCollection<SocketMessage> GetCachedMessages(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
  83. => SocketChannelHelper.GetCachedMessages(this, Discord, _messages, fromMessageId, dir, limit);
  84. public IReadOnlyCollection<SocketMessage> GetCachedMessages(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
  85. => SocketChannelHelper.GetCachedMessages(this, Discord, _messages, fromMessage.Id, dir, limit);
  86. public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(RequestOptions options = null)
  87. => ChannelHelper.GetPinnedMessagesAsync(this, Discord, options);
  88. public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS = false, Embed embed = null, RequestOptions options = null)
  89. => ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, embed, options);
  90. #if FILESYSTEM
  91. public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, RequestOptions options = null)
  92. => ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, options);
  93. #endif
  94. public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, RequestOptions options = null)
  95. => ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, options);
  96. public Task DeleteMessagesAsync(IEnumerable<IMessage> messages, RequestOptions options = null)
  97. => ChannelHelper.DeleteMessagesAsync(this, Discord, messages, options);
  98. public Task TriggerTypingAsync(RequestOptions options = null)
  99. => ChannelHelper.TriggerTypingAsync(this, Discord, options);
  100. public IDisposable EnterTypingState(RequestOptions options = null)
  101. => ChannelHelper.EnterTypingState(this, Discord, options);
  102. internal void AddMessage(SocketMessage msg)
  103. => _messages?.Add(msg);
  104. internal SocketMessage RemoveMessage(ulong id)
  105. => _messages?.Remove(id);
  106. //Users
  107. public new SocketGroupUser GetUser(ulong id)
  108. {
  109. if (_users.TryGetValue(id, out SocketGroupUser user))
  110. return user;
  111. return null;
  112. }
  113. internal SocketGroupUser GetOrAddUser(UserModel model)
  114. {
  115. if (_users.TryGetValue(model.Id, out SocketGroupUser user))
  116. return user as SocketGroupUser;
  117. else
  118. {
  119. var privateUser = SocketGroupUser.Create(this, Discord.State, model);
  120. privateUser.GlobalUser.AddRef();
  121. _users[privateUser.Id] = privateUser;
  122. return privateUser;
  123. }
  124. }
  125. internal SocketGroupUser RemoveUser(ulong id)
  126. {
  127. if (_users.TryRemove(id, out SocketGroupUser user))
  128. {
  129. user.GlobalUser.RemoveRef(Discord);
  130. return user as SocketGroupUser;
  131. }
  132. return null;
  133. }
  134. //Voice States
  135. internal SocketVoiceState AddOrUpdateVoiceState(ClientState state, VoiceStateModel model)
  136. {
  137. var voiceChannel = state.GetChannel(model.ChannelId.Value) as SocketVoiceChannel;
  138. var voiceState = SocketVoiceState.Create(voiceChannel, model);
  139. _voiceStates[model.UserId] = voiceState;
  140. return voiceState;
  141. }
  142. internal SocketVoiceState? GetVoiceState(ulong id)
  143. {
  144. if (_voiceStates.TryGetValue(id, out SocketVoiceState voiceState))
  145. return voiceState;
  146. return null;
  147. }
  148. internal SocketVoiceState? RemoveVoiceState(ulong id)
  149. {
  150. if (_voiceStates.TryRemove(id, out SocketVoiceState voiceState))
  151. return voiceState;
  152. return null;
  153. }
  154. public override string ToString() => Name;
  155. private string DebuggerDisplay => $"{Name} ({Id}, Group)";
  156. internal new SocketGroupChannel Clone() => MemberwiseClone() as SocketGroupChannel;
  157. //SocketChannel
  158. internal override IReadOnlyCollection<SocketUser> GetUsersInternal() => Users;
  159. internal override SocketUser GetUserInternal(ulong id) => GetUser(id);
  160. //ISocketPrivateChannel
  161. IReadOnlyCollection<SocketUser> ISocketPrivateChannel.Recipients => Recipients;
  162. //IPrivateChannel
  163. IReadOnlyCollection<IUser> IPrivateChannel.Recipients => Recipients;
  164. //IMessageChannel
  165. async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options)
  166. {
  167. if (mode == CacheMode.AllowDownload)
  168. return await GetMessageAsync(id, options).ConfigureAwait(false);
  169. else
  170. return GetCachedMessage(id);
  171. }
  172. IAsyncEnumerable<IMessage> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, RequestOptions options)
  173. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit, mode, options);
  174. IAsyncEnumerable<IMessage> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode, RequestOptions options)
  175. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit, mode, options);
  176. IAsyncEnumerable<IMessage> IMessageChannel.GetMessagesAsync(IMessage fromMessage, Direction dir, int limit, CacheMode mode, RequestOptions options)
  177. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessage.Id, dir, limit, mode, options);
  178. async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options)
  179. => await GetPinnedMessagesAsync(options).ConfigureAwait(false);
  180. #if FILESYSTEM
  181. async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, RequestOptions options)
  182. => await SendFileAsync(filePath, text, isTTS, options).ConfigureAwait(false);
  183. #endif
  184. async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, RequestOptions options)
  185. => await SendFileAsync(stream, filename, text, isTTS, options).ConfigureAwait(false);
  186. async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, Embed embed, RequestOptions options)
  187. => await SendMessageAsync(text, isTTS, embed, options).ConfigureAwait(false);
  188. IDisposable IMessageChannel.EnterTypingState(RequestOptions options)
  189. => EnterTypingState(options);
  190. //IAudioChannel
  191. Task<IAudioClient> IAudioChannel.ConnectAsync(Action<IAudioClient> configAction) { throw new NotSupportedException(); }
  192. //IChannel
  193. Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  194. => Task.FromResult<IUser>(GetUser(id));
  195. IAsyncEnumerable<IUser> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
  196. => ImmutableArray.Create<IUser>(Users.ToArray()).ToAsyncEnumerable();
  197. }
  198. }