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 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. internal virtual 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()
  58. => ChannelHelper.DeleteAsync(this, Discord);
  59. //Messages
  60. public SocketMessage GetCachedMessage(ulong id)
  61. => _messages?.Get(id);
  62. public async Task<IMessage> GetMessageAsync(ulong id)
  63. {
  64. IMessage msg = _messages?.Get(id);
  65. if (msg == null)
  66. msg = await ChannelHelper.GetMessageAsync(this, Discord, id);
  67. return msg;
  68. }
  69. public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch)
  70. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit, CacheMode.AllowDownload);
  71. public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
  72. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit, CacheMode.AllowDownload);
  73. public IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch)
  74. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessage.Id, dir, limit, CacheMode.AllowDownload);
  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()
  82. => ChannelHelper.GetPinnedMessagesAsync(this, Discord);
  83. public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS)
  84. => ChannelHelper.SendMessageAsync(this, Discord, text, isTTS);
  85. public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS)
  86. => ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS);
  87. public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS)
  88. => ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS);
  89. public Task DeleteMessagesAsync(IEnumerable<IMessage> messages)
  90. => ChannelHelper.DeleteMessagesAsync(this, Discord, messages);
  91. public IDisposable EnterTypingState()
  92. => ChannelHelper.EnterTypingState(this, Discord);
  93. internal void AddMessage(SocketMessage msg)
  94. => _messages.Add(msg);
  95. internal SocketMessage RemoveMessage(ulong id)
  96. => _messages.Remove(id);
  97. //Users
  98. public new SocketGroupUser GetUser(ulong id)
  99. {
  100. SocketGroupUser user;
  101. if (_users.TryGetValue(id, out user))
  102. return user;
  103. return null;
  104. }
  105. internal SocketGroupUser AddUser(UserModel model)
  106. {
  107. SocketGroupUser user;
  108. if (_users.TryGetValue(model.Id, out user))
  109. return user as SocketGroupUser;
  110. else
  111. {
  112. var privateUser = SocketGroupUser.Create(this, Discord.State, model);
  113. _users[privateUser.Id] = privateUser;
  114. return privateUser;
  115. }
  116. }
  117. internal SocketGroupUser RemoveUser(ulong id)
  118. {
  119. SocketGroupUser user;
  120. if (_users.TryRemove(id, out user))
  121. {
  122. user.GlobalUser.RemoveRef(Discord);
  123. return user as SocketGroupUser;
  124. }
  125. return null;
  126. }
  127. //Voice States
  128. internal SocketVoiceState AddOrUpdateVoiceState(ClientState state, VoiceStateModel model)
  129. {
  130. var voiceChannel = state.GetChannel(model.ChannelId.Value) as SocketVoiceChannel;
  131. var voiceState = SocketVoiceState.Create(voiceChannel, model);
  132. _voiceStates[model.UserId] = voiceState;
  133. return voiceState;
  134. }
  135. internal SocketVoiceState? GetVoiceState(ulong id)
  136. {
  137. SocketVoiceState voiceState;
  138. if (_voiceStates.TryGetValue(id, out voiceState))
  139. return voiceState;
  140. return null;
  141. }
  142. internal SocketVoiceState? RemoveVoiceState(ulong id)
  143. {
  144. SocketVoiceState voiceState;
  145. if (_voiceStates.TryRemove(id, out voiceState))
  146. return voiceState;
  147. return null;
  148. }
  149. public override string ToString() => Name;
  150. private string DebuggerDisplay => $"{Name} ({Id}, Group)";
  151. internal new SocketGroupChannel Clone() => MemberwiseClone() as SocketGroupChannel;
  152. //SocketChannel
  153. internal override IReadOnlyCollection<SocketUser> GetUsersInternal() => Users;
  154. internal override SocketUser GetUserInternal(ulong id) => GetUser(id);
  155. //ISocketPrivateChannel
  156. IReadOnlyCollection<SocketUser> ISocketPrivateChannel.Recipients => Recipients;
  157. //IPrivateChannel
  158. IReadOnlyCollection<IUser> IPrivateChannel.Recipients => Recipients;
  159. //IMessageChannel
  160. async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode)
  161. {
  162. if (mode == CacheMode.AllowDownload)
  163. return await GetMessageAsync(id);
  164. else
  165. return GetCachedMessage(id);
  166. }
  167. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode)
  168. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, null, Direction.Before, limit, mode);
  169. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode)
  170. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessageId, dir, limit, mode);
  171. IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(IMessage fromMessage, Direction dir, int limit, CacheMode mode)
  172. => SocketChannelHelper.GetMessagesAsync(this, Discord, _messages, fromMessage.Id, dir, limit, mode);
  173. async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync()
  174. => await GetPinnedMessagesAsync();
  175. async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS)
  176. => await SendFileAsync(filePath, text, isTTS);
  177. async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS)
  178. => await SendFileAsync(stream, filename, text, isTTS);
  179. async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS)
  180. => await SendMessageAsync(text, isTTS);
  181. IDisposable IMessageChannel.EnterTypingState()
  182. => EnterTypingState();
  183. //IChannel
  184. Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode)
  185. => Task.FromResult<IUser>(GetUser(id));
  186. IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode)
  187. => ImmutableArray.Create<IReadOnlyCollection<IUser>>(Users).ToAsyncEnumerable();
  188. }
  189. }