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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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<IReadOnlyCollection<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<IReadOnlyCollection<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<IReadOnlyCollection<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, Embed embed = null, RequestOptions options = null)
  92. => ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, embed, options);
  93. #endif
  94. public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null)
  95. => ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, embed, options);
  96. public Task TriggerTypingAsync(RequestOptions options = null)
  97. => ChannelHelper.TriggerTypingAsync(this, Discord, options);
  98. public IDisposable EnterTypingState(RequestOptions options = null)
  99. => ChannelHelper.EnterTypingState(this, Discord, options);
  100. internal void AddMessage(SocketMessage msg)
  101. => _messages?.Add(msg);
  102. internal SocketMessage RemoveMessage(ulong id)
  103. => _messages?.Remove(id);
  104. //Users
  105. public new SocketGroupUser GetUser(ulong id)
  106. {
  107. if (_users.TryGetValue(id, out SocketGroupUser user))
  108. return user;
  109. return null;
  110. }
  111. internal SocketGroupUser GetOrAddUser(UserModel model)
  112. {
  113. if (_users.TryGetValue(model.Id, out SocketGroupUser user))
  114. return user as SocketGroupUser;
  115. else
  116. {
  117. var privateUser = SocketGroupUser.Create(this, Discord.State, model);
  118. privateUser.GlobalUser.AddRef();
  119. _users[privateUser.Id] = privateUser;
  120. return privateUser;
  121. }
  122. }
  123. internal SocketGroupUser RemoveUser(ulong id)
  124. {
  125. if (_users.TryRemove(id, out SocketGroupUser user))
  126. {
  127. user.GlobalUser.RemoveRef(Discord);
  128. return user as SocketGroupUser;
  129. }
  130. return null;
  131. }
  132. //Voice States
  133. internal SocketVoiceState AddOrUpdateVoiceState(ClientState state, VoiceStateModel model)
  134. {
  135. var voiceChannel = state.GetChannel(model.ChannelId.Value) as SocketVoiceChannel;
  136. var voiceState = SocketVoiceState.Create(voiceChannel, model);
  137. _voiceStates[model.UserId] = voiceState;
  138. return voiceState;
  139. }
  140. internal SocketVoiceState? GetVoiceState(ulong id)
  141. {
  142. if (_voiceStates.TryGetValue(id, out SocketVoiceState voiceState))
  143. return voiceState;
  144. return null;
  145. }
  146. internal SocketVoiceState? RemoveVoiceState(ulong id)
  147. {
  148. if (_voiceStates.TryRemove(id, out SocketVoiceState voiceState))
  149. return voiceState;
  150. return null;
  151. }
  152. public override string ToString() => Name;
  153. private string DebuggerDisplay => $"{Name} ({Id}, Group)";
  154. internal new SocketGroupChannel Clone() => MemberwiseClone() as SocketGroupChannel;
  155. //SocketChannel
  156. internal override IReadOnlyCollection<SocketUser> GetUsersInternal() => Users;
  157. internal override SocketUser GetUserInternal(ulong id) => GetUser(id);
  158. //ISocketPrivateChannel
  159. IReadOnlyCollection<SocketUser> ISocketPrivateChannel.Recipients => Recipients;
  160. //IPrivateChannel
  161. IReadOnlyCollection<IUser> IPrivateChannel.Recipients => Recipients;
  162. //IMessageChannel
  163. async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options)
  164. {
  165. if (mode == CacheMode.AllowDownload)
  166. return await GetMessageAsync(id, options).ConfigureAwait(false);
  167. else
  168. return GetCachedMessage(id);
  169. }
  170. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, RequestOptions options)
  171. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit, mode, options);
  172. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode, RequestOptions options)
  173. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit, mode, options);
  174. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(IMessage fromMessage, Direction dir, int limit, CacheMode mode, RequestOptions options)
  175. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessage.Id, dir, limit, mode, options);
  176. async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options)
  177. => await GetPinnedMessagesAsync(options).ConfigureAwait(false);
  178. #if FILESYSTEM
  179. async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, Embed embed, RequestOptions options)
  180. => await SendFileAsync(filePath, text, isTTS, embed, options).ConfigureAwait(false);
  181. #endif
  182. async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options)
  183. => await SendFileAsync(stream, filename, text, isTTS, embed, options).ConfigureAwait(false);
  184. async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, Embed embed, RequestOptions options)
  185. => await SendMessageAsync(text, isTTS, embed, options).ConfigureAwait(false);
  186. IDisposable IMessageChannel.EnterTypingState(RequestOptions options)
  187. => EnterTypingState(options);
  188. //IAudioChannel
  189. Task<IAudioClient> IAudioChannel.ConnectAsync(Action<IAudioClient> configAction) { throw new NotSupportedException(); }
  190. //IChannel
  191. Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  192. => Task.FromResult<IUser>(GetUser(id));
  193. IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
  194. => ImmutableArray.Create<IReadOnlyCollection<IUser>>(Users).ToAsyncEnumerable();
  195. }
  196. }