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

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