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.

SocketGuild.cs 34 kB

Add support for channel categories (#907) commit a85c5814a74e473e95fe172f0379cbc7f9f951d8 Author: Christopher F <computerizedtaco@gmail.com> Date: Sat Jan 6 22:25:48 2018 -0500 Code cleanup commit 4b243fd3dd99152b4ebc7ee01d704bd8e57eeee1 Author: Christopher F <computerizedtaco@gmail.com> Date: Sat Jan 6 22:08:28 2018 -0500 Add support for channel categories (#907) commit 41ed9106f2b05530acbf06b245c9aa618011d815 Author: mrspits4ever <spits.lucas@gmail.com> Date: Thu Dec 14 20:02:57 2017 +0100 removed mentioning support for RestCategoryChannel, added channels property to SocketCategoryChannel commit 71142c310847886dff80c49e9357dd0786d67a1b Merge: 4589d731 678a7238 Author: mrspits4ever <spits.lucas@gmail.com> Date: Wed Dec 13 21:17:53 2017 +0100 Merge branch 'dev' of https://github.com/RogueException/Discord.Net into feature/channel-categories commit 4589d73187871c98485ed25c6d223706927af7ec Author: mrspits4ever <spits.lucas@gmail.com> Date: Wed Dec 13 21:17:46 2017 +0100 adressed requested changes commit d59b038efa048b2279602e2015ddd2c185e58d63 Author: pegasy <pegasy@users.noreply.github.com> Date: Mon Sep 25 18:53:23 2017 +0200 Renamed classes / properties / methods to use CategoryChannel instead of ChannelCategory to be consistant with how text / voice channels are named. commit 5c4777dc8cc443108f2e7e4afae98824c9a32b1f Author: pegasy <pegasy@users.noreply.github.com> Date: Sun Sep 24 19:08:25 2017 +0200 removed Guild from class name for ChannelCategory Renamed all properties to use Category instead of Parent Throw exception on GetUsers / GetInvites etc for categories commit e18bd8c799d2327270021c05866cb2e97ad4671b Author: pegasy <pegasy@users.noreply.github.com> Date: Sun Sep 24 15:49:51 2017 +0200 Add support for channel categories (as its own channel type)
8 years ago
Add support for channel categories (#907) commit a85c5814a74e473e95fe172f0379cbc7f9f951d8 Author: Christopher F <computerizedtaco@gmail.com> Date: Sat Jan 6 22:25:48 2018 -0500 Code cleanup commit 4b243fd3dd99152b4ebc7ee01d704bd8e57eeee1 Author: Christopher F <computerizedtaco@gmail.com> Date: Sat Jan 6 22:08:28 2018 -0500 Add support for channel categories (#907) commit 41ed9106f2b05530acbf06b245c9aa618011d815 Author: mrspits4ever <spits.lucas@gmail.com> Date: Thu Dec 14 20:02:57 2017 +0100 removed mentioning support for RestCategoryChannel, added channels property to SocketCategoryChannel commit 71142c310847886dff80c49e9357dd0786d67a1b Merge: 4589d731 678a7238 Author: mrspits4ever <spits.lucas@gmail.com> Date: Wed Dec 13 21:17:53 2017 +0100 Merge branch 'dev' of https://github.com/RogueException/Discord.Net into feature/channel-categories commit 4589d73187871c98485ed25c6d223706927af7ec Author: mrspits4ever <spits.lucas@gmail.com> Date: Wed Dec 13 21:17:46 2017 +0100 adressed requested changes commit d59b038efa048b2279602e2015ddd2c185e58d63 Author: pegasy <pegasy@users.noreply.github.com> Date: Mon Sep 25 18:53:23 2017 +0200 Renamed classes / properties / methods to use CategoryChannel instead of ChannelCategory to be consistant with how text / voice channels are named. commit 5c4777dc8cc443108f2e7e4afae98824c9a32b1f Author: pegasy <pegasy@users.noreply.github.com> Date: Sun Sep 24 19:08:25 2017 +0200 removed Guild from class name for ChannelCategory Renamed all properties to use Category instead of Parent Throw exception on GetUsers / GetInvites etc for categories commit e18bd8c799d2327270021c05866cb2e97ad4671b Author: pegasy <pegasy@users.noreply.github.com> Date: Sun Sep 24 15:49:51 2017 +0200 Add support for channel categories (as its own channel type)
8 years ago
Add support for channel categories (#907) commit a85c5814a74e473e95fe172f0379cbc7f9f951d8 Author: Christopher F <computerizedtaco@gmail.com> Date: Sat Jan 6 22:25:48 2018 -0500 Code cleanup commit 4b243fd3dd99152b4ebc7ee01d704bd8e57eeee1 Author: Christopher F <computerizedtaco@gmail.com> Date: Sat Jan 6 22:08:28 2018 -0500 Add support for channel categories (#907) commit 41ed9106f2b05530acbf06b245c9aa618011d815 Author: mrspits4ever <spits.lucas@gmail.com> Date: Thu Dec 14 20:02:57 2017 +0100 removed mentioning support for RestCategoryChannel, added channels property to SocketCategoryChannel commit 71142c310847886dff80c49e9357dd0786d67a1b Merge: 4589d731 678a7238 Author: mrspits4ever <spits.lucas@gmail.com> Date: Wed Dec 13 21:17:53 2017 +0100 Merge branch 'dev' of https://github.com/RogueException/Discord.Net into feature/channel-categories commit 4589d73187871c98485ed25c6d223706927af7ec Author: mrspits4ever <spits.lucas@gmail.com> Date: Wed Dec 13 21:17:46 2017 +0100 adressed requested changes commit d59b038efa048b2279602e2015ddd2c185e58d63 Author: pegasy <pegasy@users.noreply.github.com> Date: Mon Sep 25 18:53:23 2017 +0200 Renamed classes / properties / methods to use CategoryChannel instead of ChannelCategory to be consistant with how text / voice channels are named. commit 5c4777dc8cc443108f2e7e4afae98824c9a32b1f Author: pegasy <pegasy@users.noreply.github.com> Date: Sun Sep 24 19:08:25 2017 +0200 removed Guild from class name for ChannelCategory Renamed all properties to use Category instead of Parent Throw exception on GetUsers / GetInvites etc for categories commit e18bd8c799d2327270021c05866cb2e97ad4671b Author: pegasy <pegasy@users.noreply.github.com> Date: Sun Sep 24 15:49:51 2017 +0200 Add support for channel categories (as its own channel type)
8 years ago
Add support for channel categories (#907) commit a85c5814a74e473e95fe172f0379cbc7f9f951d8 Author: Christopher F <computerizedtaco@gmail.com> Date: Sat Jan 6 22:25:48 2018 -0500 Code cleanup commit 4b243fd3dd99152b4ebc7ee01d704bd8e57eeee1 Author: Christopher F <computerizedtaco@gmail.com> Date: Sat Jan 6 22:08:28 2018 -0500 Add support for channel categories (#907) commit 41ed9106f2b05530acbf06b245c9aa618011d815 Author: mrspits4ever <spits.lucas@gmail.com> Date: Thu Dec 14 20:02:57 2017 +0100 removed mentioning support for RestCategoryChannel, added channels property to SocketCategoryChannel commit 71142c310847886dff80c49e9357dd0786d67a1b Merge: 4589d731 678a7238 Author: mrspits4ever <spits.lucas@gmail.com> Date: Wed Dec 13 21:17:53 2017 +0100 Merge branch 'dev' of https://github.com/RogueException/Discord.Net into feature/channel-categories commit 4589d73187871c98485ed25c6d223706927af7ec Author: mrspits4ever <spits.lucas@gmail.com> Date: Wed Dec 13 21:17:46 2017 +0100 adressed requested changes commit d59b038efa048b2279602e2015ddd2c185e58d63 Author: pegasy <pegasy@users.noreply.github.com> Date: Mon Sep 25 18:53:23 2017 +0200 Renamed classes / properties / methods to use CategoryChannel instead of ChannelCategory to be consistant with how text / voice channels are named. commit 5c4777dc8cc443108f2e7e4afae98824c9a32b1f Author: pegasy <pegasy@users.noreply.github.com> Date: Sun Sep 24 19:08:25 2017 +0200 removed Guild from class name for ChannelCategory Renamed all properties to use Category instead of Parent Throw exception on GetUsers / GetInvites etc for categories commit e18bd8c799d2327270021c05866cb2e97ad4671b Author: pegasy <pegasy@users.noreply.github.com> Date: Sun Sep 24 15:49:51 2017 +0200 Add support for channel categories (as its own channel type)
8 years ago
Add support for channel categories (#907) commit a85c5814a74e473e95fe172f0379cbc7f9f951d8 Author: Christopher F <computerizedtaco@gmail.com> Date: Sat Jan 6 22:25:48 2018 -0500 Code cleanup commit 4b243fd3dd99152b4ebc7ee01d704bd8e57eeee1 Author: Christopher F <computerizedtaco@gmail.com> Date: Sat Jan 6 22:08:28 2018 -0500 Add support for channel categories (#907) commit 41ed9106f2b05530acbf06b245c9aa618011d815 Author: mrspits4ever <spits.lucas@gmail.com> Date: Thu Dec 14 20:02:57 2017 +0100 removed mentioning support for RestCategoryChannel, added channels property to SocketCategoryChannel commit 71142c310847886dff80c49e9357dd0786d67a1b Merge: 4589d731 678a7238 Author: mrspits4ever <spits.lucas@gmail.com> Date: Wed Dec 13 21:17:53 2017 +0100 Merge branch 'dev' of https://github.com/RogueException/Discord.Net into feature/channel-categories commit 4589d73187871c98485ed25c6d223706927af7ec Author: mrspits4ever <spits.lucas@gmail.com> Date: Wed Dec 13 21:17:46 2017 +0100 adressed requested changes commit d59b038efa048b2279602e2015ddd2c185e58d63 Author: pegasy <pegasy@users.noreply.github.com> Date: Mon Sep 25 18:53:23 2017 +0200 Renamed classes / properties / methods to use CategoryChannel instead of ChannelCategory to be consistant with how text / voice channels are named. commit 5c4777dc8cc443108f2e7e4afae98824c9a32b1f Author: pegasy <pegasy@users.noreply.github.com> Date: Sun Sep 24 19:08:25 2017 +0200 removed Guild from class name for ChannelCategory Renamed all properties to use Category instead of Parent Throw exception on GetUsers / GetInvites etc for categories commit e18bd8c799d2327270021c05866cb2e97ad4671b Author: pegasy <pegasy@users.noreply.github.com> Date: Sun Sep 24 15:49:51 2017 +0200 Add support for channel categories (as its own channel type)
8 years ago
Add support for channel categories (#907) commit a85c5814a74e473e95fe172f0379cbc7f9f951d8 Author: Christopher F <computerizedtaco@gmail.com> Date: Sat Jan 6 22:25:48 2018 -0500 Code cleanup commit 4b243fd3dd99152b4ebc7ee01d704bd8e57eeee1 Author: Christopher F <computerizedtaco@gmail.com> Date: Sat Jan 6 22:08:28 2018 -0500 Add support for channel categories (#907) commit 41ed9106f2b05530acbf06b245c9aa618011d815 Author: mrspits4ever <spits.lucas@gmail.com> Date: Thu Dec 14 20:02:57 2017 +0100 removed mentioning support for RestCategoryChannel, added channels property to SocketCategoryChannel commit 71142c310847886dff80c49e9357dd0786d67a1b Merge: 4589d731 678a7238 Author: mrspits4ever <spits.lucas@gmail.com> Date: Wed Dec 13 21:17:53 2017 +0100 Merge branch 'dev' of https://github.com/RogueException/Discord.Net into feature/channel-categories commit 4589d73187871c98485ed25c6d223706927af7ec Author: mrspits4ever <spits.lucas@gmail.com> Date: Wed Dec 13 21:17:46 2017 +0100 adressed requested changes commit d59b038efa048b2279602e2015ddd2c185e58d63 Author: pegasy <pegasy@users.noreply.github.com> Date: Mon Sep 25 18:53:23 2017 +0200 Renamed classes / properties / methods to use CategoryChannel instead of ChannelCategory to be consistant with how text / voice channels are named. commit 5c4777dc8cc443108f2e7e4afae98824c9a32b1f Author: pegasy <pegasy@users.noreply.github.com> Date: Sun Sep 24 19:08:25 2017 +0200 removed Guild from class name for ChannelCategory Renamed all properties to use Category instead of Parent Throw exception on GetUsers / GetInvites etc for categories commit e18bd8c799d2327270021c05866cb2e97ad4671b Author: pegasy <pegasy@users.noreply.github.com> Date: Sun Sep 24 15:49:51 2017 +0200 Add support for channel categories (as its own channel type)
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using ChannelModel = Discord.API.Channel;
  12. using EmojiUpdateModel = Discord.API.Gateway.GuildEmojiUpdateEvent;
  13. using ExtendedModel = Discord.API.Gateway.ExtendedGuild;
  14. using GuildSyncModel = Discord.API.Gateway.GuildSyncEvent;
  15. using MemberModel = Discord.API.GuildMember;
  16. using Model = Discord.API.Guild;
  17. using PresenceModel = Discord.API.Presence;
  18. using RoleModel = Discord.API.Role;
  19. using UserModel = Discord.API.User;
  20. using VoiceStateModel = Discord.API.VoiceState;
  21. namespace Discord.WebSocket
  22. {
  23. public class SocketGuild : SocketEntity<ulong>, IGuild
  24. {
  25. private readonly SemaphoreSlim _audioLock;
  26. private TaskCompletionSource<bool> _syncPromise, _downloaderPromise;
  27. private TaskCompletionSource<AudioClient> _audioConnectPromise;
  28. private ConcurrentHashSet<ulong> _channels;
  29. private ConcurrentDictionary<ulong, SocketGuildUser> _members;
  30. private ConcurrentDictionary<ulong, SocketRole> _roles;
  31. private ConcurrentDictionary<ulong, SocketVoiceState> _voiceStates;
  32. private ImmutableArray<GuildEmote> _emotes;
  33. private ImmutableArray<string> _features;
  34. private AudioClient _audioClient;
  35. public string Name { get; private set; }
  36. public int AFKTimeout { get; private set; }
  37. public bool IsEmbeddable { get; private set; }
  38. public VerificationLevel VerificationLevel { get; private set; }
  39. public MfaLevel MfaLevel { get; private set; }
  40. public DefaultMessageNotifications DefaultMessageNotifications { get; private set; }
  41. public int MemberCount { get; internal set; }
  42. public int DownloadedMemberCount { get; private set; }
  43. internal bool IsAvailable { get; private set; }
  44. public bool IsConnected { get; internal set; }
  45. internal ulong? AFKChannelId { get; private set; }
  46. internal ulong? EmbedChannelId { get; private set; }
  47. internal ulong? SystemChannelId { get; private set; }
  48. public ulong OwnerId { get; private set; }
  49. public SocketGuildUser Owner => GetUser(OwnerId);
  50. public string VoiceRegionId { get; private set; }
  51. public string IconId { get; private set; }
  52. public string SplashId { get; private set; }
  53. public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
  54. public string IconUrl => CDN.GetGuildIconUrl(Id, IconId);
  55. public string SplashUrl => CDN.GetGuildSplashUrl(Id, SplashId);
  56. public bool HasAllMembers => MemberCount == DownloadedMemberCount;// _downloaderPromise.Task.IsCompleted;
  57. public bool IsSynced => _syncPromise.Task.IsCompleted;
  58. public Task SyncPromise => _syncPromise.Task;
  59. public Task DownloaderPromise => _downloaderPromise.Task;
  60. public IAudioClient AudioClient => _audioClient;
  61. public SocketTextChannel DefaultChannel => TextChannels
  62. .Where(c => CurrentUser.GetPermissions(c).ViewChannel)
  63. .OrderBy(c => c.Position)
  64. .FirstOrDefault();
  65. public SocketVoiceChannel AFKChannel
  66. {
  67. get
  68. {
  69. var id = AFKChannelId;
  70. return id.HasValue ? GetVoiceChannel(id.Value) : null;
  71. }
  72. }
  73. public SocketGuildChannel EmbedChannel
  74. {
  75. get
  76. {
  77. var id = EmbedChannelId;
  78. return id.HasValue ? GetChannel(id.Value) : null;
  79. }
  80. }
  81. public SocketTextChannel SystemChannel
  82. {
  83. get
  84. {
  85. var id = SystemChannelId;
  86. return id.HasValue ? GetTextChannel(id.Value) : null;
  87. }
  88. }
  89. public IReadOnlyCollection<SocketTextChannel> TextChannels
  90. => Channels.Select(x => x as SocketTextChannel).Where(x => x != null).ToImmutableArray();
  91. public IReadOnlyCollection<SocketVoiceChannel> VoiceChannels
  92. => Channels.Select(x => x as SocketVoiceChannel).Where(x => x != null).ToImmutableArray();
  93. public IReadOnlyCollection<SocketCategoryChannel> CategoryChannels
  94. => Channels.Select(x => x as SocketCategoryChannel).Where(x => x != null).ToImmutableArray();
  95. public SocketGuildUser CurrentUser => _members.TryGetValue(Discord.CurrentUser.Id, out SocketGuildUser member) ? member : null;
  96. public SocketRole EveryoneRole => GetRole(Id);
  97. public IReadOnlyCollection<SocketGuildChannel> Channels
  98. {
  99. get
  100. {
  101. var channels = _channels;
  102. var state = Discord.State;
  103. return channels.Select(x => state.GetChannel(x) as SocketGuildChannel).Where(x => x != null).ToReadOnlyCollection(channels);
  104. }
  105. }
  106. public IReadOnlyCollection<GuildEmote> Emotes => _emotes;
  107. public IReadOnlyCollection<string> Features => _features;
  108. public IReadOnlyCollection<SocketGuildUser> Users => _members.ToReadOnlyCollection();
  109. public IReadOnlyCollection<SocketRole> Roles => _roles.ToReadOnlyCollection();
  110. internal SocketGuild(DiscordSocketClient client, ulong id)
  111. : base(client, id)
  112. {
  113. _audioLock = new SemaphoreSlim(1, 1);
  114. _emotes = ImmutableArray.Create<GuildEmote>();
  115. _features = ImmutableArray.Create<string>();
  116. }
  117. internal static SocketGuild Create(DiscordSocketClient discord, ClientState state, ExtendedModel model)
  118. {
  119. var entity = new SocketGuild(discord, model.Id);
  120. entity.Update(state, model);
  121. return entity;
  122. }
  123. internal void Update(ClientState state, ExtendedModel model)
  124. {
  125. IsAvailable = !(model.Unavailable ?? false);
  126. if (!IsAvailable)
  127. {
  128. if (_channels == null)
  129. _channels = new ConcurrentHashSet<ulong>();
  130. if (_members == null)
  131. _members = new ConcurrentDictionary<ulong, SocketGuildUser>();
  132. if (_roles == null)
  133. _roles = new ConcurrentDictionary<ulong, SocketRole>();
  134. /*if (Emojis == null)
  135. _emojis = ImmutableArray.Create<Emoji>();
  136. if (Features == null)
  137. _features = ImmutableArray.Create<string>();*/
  138. _syncPromise = new TaskCompletionSource<bool>();
  139. _downloaderPromise = new TaskCompletionSource<bool>();
  140. return;
  141. }
  142. Update(state, model as Model);
  143. var channels = new ConcurrentHashSet<ulong>(ConcurrentHashSet.DefaultConcurrencyLevel, (int)(model.Channels.Length * 1.05));
  144. {
  145. for (int i = 0; i < model.Channels.Length; i++)
  146. {
  147. var channel = SocketGuildChannel.Create(this, state, model.Channels[i]);
  148. state.AddChannel(channel);
  149. channels.TryAdd(channel.Id);
  150. }
  151. }
  152. _channels = channels;
  153. var members = new ConcurrentDictionary<ulong, SocketGuildUser>(ConcurrentHashSet.DefaultConcurrencyLevel, (int)(model.Members.Length * 1.05));
  154. {
  155. for (int i = 0; i < model.Members.Length; i++)
  156. {
  157. var member = SocketGuildUser.Create(this, state, model.Members[i]);
  158. members.TryAdd(member.Id, member);
  159. }
  160. DownloadedMemberCount = members.Count;
  161. for (int i = 0; i < model.Presences.Length; i++)
  162. {
  163. if (members.TryGetValue(model.Presences[i].User.Id, out SocketGuildUser member))
  164. member.Update(state, model.Presences[i], true);
  165. }
  166. }
  167. _members = members;
  168. MemberCount = model.MemberCount;
  169. var voiceStates = new ConcurrentDictionary<ulong, SocketVoiceState>(ConcurrentHashSet.DefaultConcurrencyLevel, (int)(model.VoiceStates.Length * 1.05));
  170. {
  171. for (int i = 0; i < model.VoiceStates.Length; i++)
  172. {
  173. SocketVoiceChannel channel = null;
  174. if (model.VoiceStates[i].ChannelId.HasValue)
  175. channel = state.GetChannel(model.VoiceStates[i].ChannelId.Value) as SocketVoiceChannel;
  176. var voiceState = SocketVoiceState.Create(channel, model.VoiceStates[i]);
  177. voiceStates.TryAdd(model.VoiceStates[i].UserId, voiceState);
  178. }
  179. }
  180. _voiceStates = voiceStates;
  181. _syncPromise = new TaskCompletionSource<bool>();
  182. _downloaderPromise = new TaskCompletionSource<bool>();
  183. var _ = _syncPromise.TrySetResultAsync(true);
  184. /*if (!model.Large)
  185. _ = _downloaderPromise.TrySetResultAsync(true);*/
  186. }
  187. internal void Update(ClientState state, Model model)
  188. {
  189. AFKChannelId = model.AFKChannelId;
  190. EmbedChannelId = model.EmbedChannelId;
  191. SystemChannelId = model.SystemChannelId;
  192. AFKTimeout = model.AFKTimeout;
  193. IsEmbeddable = model.EmbedEnabled;
  194. IconId = model.Icon;
  195. Name = model.Name;
  196. OwnerId = model.OwnerId;
  197. VoiceRegionId = model.Region;
  198. SplashId = model.Splash;
  199. VerificationLevel = model.VerificationLevel;
  200. MfaLevel = model.MfaLevel;
  201. DefaultMessageNotifications = model.DefaultMessageNotifications;
  202. if (model.Emojis != null)
  203. {
  204. var emojis = ImmutableArray.CreateBuilder<GuildEmote>(model.Emojis.Length);
  205. for (int i = 0; i < model.Emojis.Length; i++)
  206. emojis.Add(model.Emojis[i].ToEntity());
  207. _emotes = emojis.ToImmutable();
  208. }
  209. else
  210. _emotes = ImmutableArray.Create<GuildEmote>();
  211. if (model.Features != null)
  212. _features = model.Features.ToImmutableArray();
  213. else
  214. _features = ImmutableArray.Create<string>();
  215. var roles = new ConcurrentDictionary<ulong, SocketRole>(ConcurrentHashSet.DefaultConcurrencyLevel, (int)(model.Roles.Length * 1.05));
  216. if (model.Roles != null)
  217. {
  218. for (int i = 0; i < model.Roles.Length; i++)
  219. {
  220. var role = SocketRole.Create(this, state, model.Roles[i]);
  221. roles.TryAdd(role.Id, role);
  222. }
  223. }
  224. _roles = roles;
  225. }
  226. internal void Update(ClientState state, GuildSyncModel model)
  227. {
  228. var members = new ConcurrentDictionary<ulong, SocketGuildUser>(ConcurrentHashSet.DefaultConcurrencyLevel, (int)(model.Members.Length * 1.05));
  229. {
  230. for (int i = 0; i < model.Members.Length; i++)
  231. {
  232. var member = SocketGuildUser.Create(this, state, model.Members[i]);
  233. members.TryAdd(member.Id, member);
  234. }
  235. DownloadedMemberCount = members.Count;
  236. for (int i = 0; i < model.Presences.Length; i++)
  237. {
  238. if (members.TryGetValue(model.Presences[i].User.Id, out SocketGuildUser member))
  239. member.Update(state, model.Presences[i], true);
  240. }
  241. }
  242. _members = members;
  243. var _ = _syncPromise.TrySetResultAsync(true);
  244. /*if (!model.Large)
  245. _ = _downloaderPromise.TrySetResultAsync(true);*/
  246. }
  247. internal void Update(ClientState state, EmojiUpdateModel model)
  248. {
  249. var emotes = ImmutableArray.CreateBuilder<GuildEmote>(model.Emojis.Length);
  250. for (int i = 0; i < model.Emojis.Length; i++)
  251. emotes.Add(model.Emojis[i].ToEntity());
  252. _emotes = emotes.ToImmutable();
  253. }
  254. //General
  255. public Task DeleteAsync(RequestOptions options = null)
  256. => GuildHelper.DeleteAsync(this, Discord, options);
  257. public Task ModifyAsync(Action<GuildProperties> func, RequestOptions options = null)
  258. => GuildHelper.ModifyAsync(this, Discord, func, options);
  259. public Task ModifyEmbedAsync(Action<GuildEmbedProperties> func, RequestOptions options = null)
  260. => GuildHelper.ModifyEmbedAsync(this, Discord, func, options);
  261. public Task ReorderChannelsAsync(IEnumerable<ReorderChannelProperties> args, RequestOptions options = null)
  262. => GuildHelper.ReorderChannelsAsync(this, Discord, args, options);
  263. public Task ReorderRolesAsync(IEnumerable<ReorderRoleProperties> args, RequestOptions options = null)
  264. => GuildHelper.ReorderRolesAsync(this, Discord, args, options);
  265. public Task LeaveAsync(RequestOptions options = null)
  266. => GuildHelper.LeaveAsync(this, Discord, options);
  267. //Bans
  268. public Task<IReadOnlyCollection<RestBan>> GetBansAsync(RequestOptions options = null)
  269. => GuildHelper.GetBansAsync(this, Discord, options);
  270. public Task<RestBan> GetBanAsync(IUser user, RequestOptions options = null)
  271. => GuildHelper.GetBanAsync(this, Discord, user.Id, options);
  272. public Task<RestBan> GetBanAsync(ulong userId, RequestOptions options = null)
  273. => GuildHelper.GetBanAsync(this, Discord, userId, options);
  274. public Task AddBanAsync(IUser user, int pruneDays = 0, string reason = null, RequestOptions options = null)
  275. => GuildHelper.AddBanAsync(this, Discord, user.Id, pruneDays, reason, options);
  276. public Task AddBanAsync(ulong userId, int pruneDays = 0, string reason = null, RequestOptions options = null)
  277. => GuildHelper.AddBanAsync(this, Discord, userId, pruneDays, reason, options);
  278. public Task RemoveBanAsync(IUser user, RequestOptions options = null)
  279. => GuildHelper.RemoveBanAsync(this, Discord, user.Id, options);
  280. public Task RemoveBanAsync(ulong userId, RequestOptions options = null)
  281. => GuildHelper.RemoveBanAsync(this, Discord, userId, options);
  282. //Channels
  283. public SocketGuildChannel GetChannel(ulong id)
  284. {
  285. var channel = Discord.State.GetChannel(id) as SocketGuildChannel;
  286. if (channel?.Guild.Id == Id)
  287. return channel;
  288. return null;
  289. }
  290. public SocketTextChannel GetTextChannel(ulong id)
  291. => GetChannel(id) as SocketTextChannel;
  292. public SocketVoiceChannel GetVoiceChannel(ulong id)
  293. => GetChannel(id) as SocketVoiceChannel;
  294. public Task<RestTextChannel> CreateTextChannelAsync(string name, RequestOptions options = null)
  295. => GuildHelper.CreateTextChannelAsync(this, Discord, name, options);
  296. public Task<RestVoiceChannel> CreateVoiceChannelAsync(string name, RequestOptions options = null)
  297. => GuildHelper.CreateVoiceChannelAsync(this, Discord, name, options);
  298. public Task<RestCategoryChannel> CreateCategoryChannelAsync(string name, RequestOptions options = null)
  299. => GuildHelper.CreateCategoryChannelAsync(this, Discord, name, options);
  300. internal SocketGuildChannel AddChannel(ClientState state, ChannelModel model)
  301. {
  302. var channel = SocketGuildChannel.Create(this, state, model);
  303. _channels.TryAdd(model.Id);
  304. state.AddChannel(channel);
  305. return channel;
  306. }
  307. internal SocketGuildChannel RemoveChannel(ClientState state, ulong id)
  308. {
  309. if (_channels.TryRemove(id))
  310. return state.RemoveChannel(id) as SocketGuildChannel;
  311. return null;
  312. }
  313. //Integrations
  314. public Task<IReadOnlyCollection<RestGuildIntegration>> GetIntegrationsAsync(RequestOptions options = null)
  315. => GuildHelper.GetIntegrationsAsync(this, Discord, options);
  316. public Task<RestGuildIntegration> CreateIntegrationAsync(ulong id, string type, RequestOptions options = null)
  317. => GuildHelper.CreateIntegrationAsync(this, Discord, id, type, options);
  318. //Invites
  319. public Task<IReadOnlyCollection<RestInviteMetadata>> GetInvitesAsync(RequestOptions options = null)
  320. => GuildHelper.GetInvitesAsync(this, Discord, options);
  321. //Roles
  322. public SocketRole GetRole(ulong id)
  323. {
  324. if (_roles.TryGetValue(id, out SocketRole value))
  325. return value;
  326. return null;
  327. }
  328. public Task<RestRole> CreateRoleAsync(string name, GuildPermissions? permissions = default(GuildPermissions?), Color? color = default(Color?),
  329. bool isHoisted = false, RequestOptions options = null)
  330. => GuildHelper.CreateRoleAsync(this, Discord, name, permissions, color, isHoisted, options);
  331. internal SocketRole AddRole(RoleModel model)
  332. {
  333. var role = SocketRole.Create(this, Discord.State, model);
  334. _roles[model.Id] = role;
  335. return role;
  336. }
  337. internal SocketRole RemoveRole(ulong id)
  338. {
  339. if (_roles.TryRemove(id, out SocketRole role))
  340. return role;
  341. return null;
  342. }
  343. //Users
  344. public SocketGuildUser GetUser(ulong id)
  345. {
  346. if (_members.TryGetValue(id, out SocketGuildUser member))
  347. return member;
  348. return null;
  349. }
  350. public Task<int> PruneUsersAsync(int days = 30, bool simulate = false, RequestOptions options = null)
  351. => GuildHelper.PruneUsersAsync(this, Discord, days, simulate, options);
  352. internal SocketGuildUser AddOrUpdateUser(UserModel model)
  353. {
  354. if (_members.TryGetValue(model.Id, out SocketGuildUser member))
  355. member.GlobalUser?.Update(Discord.State, model);
  356. else
  357. {
  358. member = SocketGuildUser.Create(this, Discord.State, model);
  359. member.GlobalUser.AddRef();
  360. _members[member.Id] = member;
  361. DownloadedMemberCount++;
  362. }
  363. return member;
  364. }
  365. internal SocketGuildUser AddOrUpdateUser(MemberModel model)
  366. {
  367. if (_members.TryGetValue(model.User.Id, out SocketGuildUser member))
  368. member.Update(Discord.State, model);
  369. else
  370. {
  371. member = SocketGuildUser.Create(this, Discord.State, model);
  372. member.GlobalUser.AddRef();
  373. _members[member.Id] = member;
  374. DownloadedMemberCount++;
  375. }
  376. return member;
  377. }
  378. internal SocketGuildUser AddOrUpdateUser(PresenceModel model)
  379. {
  380. if (_members.TryGetValue(model.User.Id, out SocketGuildUser member))
  381. member.Update(Discord.State, model, false);
  382. else
  383. {
  384. member = SocketGuildUser.Create(this, Discord.State, model);
  385. member.GlobalUser.AddRef();
  386. _members[member.Id] = member;
  387. DownloadedMemberCount++;
  388. }
  389. return member;
  390. }
  391. internal SocketGuildUser RemoveUser(ulong id)
  392. {
  393. if (_members.TryRemove(id, out SocketGuildUser member))
  394. {
  395. DownloadedMemberCount--;
  396. member.GlobalUser.RemoveRef(Discord);
  397. return member;
  398. }
  399. return null;
  400. }
  401. public async Task DownloadUsersAsync()
  402. {
  403. await Discord.DownloadUsersAsync(new[] { this }).ConfigureAwait(false);
  404. }
  405. internal void CompleteDownloadUsers()
  406. {
  407. _downloaderPromise.TrySetResultAsync(true);
  408. }
  409. //Webhooks
  410. public Task<RestWebhook> GetWebhookAsync(ulong id, RequestOptions options = null)
  411. => GuildHelper.GetWebhookAsync(this, Discord, id, options);
  412. public Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(RequestOptions options = null)
  413. => GuildHelper.GetWebhooksAsync(this, Discord, options);
  414. //Emotes
  415. public Task<GuildEmote> GetEmoteAsync(ulong id, RequestOptions options = null)
  416. => GuildHelper.GetEmoteAsync(this, Discord, id, options);
  417. public Task<GuildEmote> CreateEmoteAsync(string name, Image image, Optional<IEnumerable<IRole>> roles = default(Optional<IEnumerable<IRole>>), RequestOptions options = null)
  418. => GuildHelper.CreateEmoteAsync(this, Discord, name, image, roles, options);
  419. public Task<GuildEmote> ModifyEmoteAsync(GuildEmote emote, Action<EmoteProperties> func, RequestOptions options = null)
  420. => GuildHelper.ModifyEmoteAsync(this, Discord, emote.Id, func, options);
  421. public Task DeleteEmoteAsync(GuildEmote emote, RequestOptions options = null)
  422. => GuildHelper.DeleteEmoteAsync(this, Discord, emote.Id, options);
  423. //Voice States
  424. internal async Task<SocketVoiceState> AddOrUpdateVoiceStateAsync(ClientState state, VoiceStateModel model)
  425. {
  426. var voiceChannel = state.GetChannel(model.ChannelId.Value) as SocketVoiceChannel;
  427. var before = GetVoiceState(model.UserId) ?? SocketVoiceState.Default;
  428. var after = SocketVoiceState.Create(voiceChannel, model);
  429. _voiceStates[model.UserId] = after;
  430. if (_audioClient != null && before.VoiceChannel?.Id != after.VoiceChannel?.Id)
  431. {
  432. if (model.UserId == CurrentUser.Id)
  433. {
  434. if (after.VoiceChannel != null && _audioClient.ChannelId != after.VoiceChannel?.Id)
  435. {
  436. _audioClient.ChannelId = after.VoiceChannel.Id;
  437. await RepopulateAudioStreamsAsync().ConfigureAwait(false);
  438. }
  439. }
  440. else
  441. {
  442. await _audioClient.RemoveInputStreamAsync(model.UserId).ConfigureAwait(false); //User changed channels, end their stream
  443. if (CurrentUser.VoiceChannel != null && after.VoiceChannel?.Id == CurrentUser.VoiceChannel?.Id)
  444. await _audioClient.CreateInputStreamAsync(model.UserId).ConfigureAwait(false);
  445. }
  446. }
  447. return after;
  448. }
  449. internal SocketVoiceState? GetVoiceState(ulong id)
  450. {
  451. if (_voiceStates.TryGetValue(id, out SocketVoiceState voiceState))
  452. return voiceState;
  453. return null;
  454. }
  455. internal async Task<SocketVoiceState?> RemoveVoiceStateAsync(ulong id)
  456. {
  457. if (_voiceStates.TryRemove(id, out SocketVoiceState voiceState))
  458. {
  459. if (_audioClient != null)
  460. await _audioClient.RemoveInputStreamAsync(id).ConfigureAwait(false); //User changed channels, end their stream
  461. return voiceState;
  462. }
  463. return null;
  464. }
  465. //Audio
  466. internal AudioInStream GetAudioStream(ulong userId)
  467. {
  468. return _audioClient?.GetInputStream(userId);
  469. }
  470. internal async Task<IAudioClient> ConnectAudioAsync(ulong channelId, bool selfDeaf, bool selfMute, bool external)
  471. {
  472. TaskCompletionSource<AudioClient> promise;
  473. await _audioLock.WaitAsync().ConfigureAwait(false);
  474. try
  475. {
  476. await DisconnectAudioInternalAsync().ConfigureAwait(false);
  477. promise = new TaskCompletionSource<AudioClient>();
  478. _audioConnectPromise = promise;
  479. if (external)
  480. {
  481. var _ = promise.TrySetResultAsync(null);
  482. await Discord.ApiClient.SendVoiceStateUpdateAsync(Id, channelId, selfDeaf, selfMute).ConfigureAwait(false);
  483. return null;
  484. }
  485. if (_audioClient == null)
  486. {
  487. var audioClient = new AudioClient(this, Discord.GetAudioId(), channelId);
  488. audioClient.Disconnected += async ex =>
  489. {
  490. if (!promise.Task.IsCompleted)
  491. {
  492. try
  493. { audioClient.Dispose(); }
  494. catch { }
  495. _audioClient = null;
  496. if (ex != null)
  497. await promise.TrySetExceptionAsync(ex);
  498. else
  499. await promise.TrySetCanceledAsync();
  500. return;
  501. }
  502. };
  503. audioClient.Connected += () =>
  504. {
  505. var _ = promise.TrySetResultAsync(_audioClient);
  506. return Task.Delay(0);
  507. };
  508. _audioClient = audioClient;
  509. }
  510. await Discord.ApiClient.SendVoiceStateUpdateAsync(Id, channelId, selfDeaf, selfMute).ConfigureAwait(false);
  511. }
  512. catch (Exception)
  513. {
  514. await DisconnectAudioInternalAsync().ConfigureAwait(false);
  515. throw;
  516. }
  517. finally
  518. {
  519. _audioLock.Release();
  520. }
  521. try
  522. {
  523. var timeoutTask = Task.Delay(15000);
  524. if (await Task.WhenAny(promise.Task, timeoutTask) == timeoutTask)
  525. throw new TimeoutException();
  526. return await promise.Task.ConfigureAwait(false);
  527. }
  528. catch (Exception)
  529. {
  530. await DisconnectAudioAsync().ConfigureAwait(false);
  531. throw;
  532. }
  533. }
  534. internal async Task DisconnectAudioAsync()
  535. {
  536. await _audioLock.WaitAsync().ConfigureAwait(false);
  537. try
  538. {
  539. await DisconnectAudioInternalAsync().ConfigureAwait(false);
  540. }
  541. finally
  542. {
  543. _audioLock.Release();
  544. }
  545. }
  546. private async Task DisconnectAudioInternalAsync()
  547. {
  548. _audioConnectPromise?.TrySetCanceledAsync(); //Cancel any previous audio connection
  549. _audioConnectPromise = null;
  550. if (_audioClient != null)
  551. await _audioClient.StopAsync().ConfigureAwait(false);
  552. await Discord.ApiClient.SendVoiceStateUpdateAsync(Id, null, false, false).ConfigureAwait(false);
  553. _audioClient = null;
  554. }
  555. internal async Task FinishConnectAudio(string url, string token)
  556. {
  557. //TODO: Mem Leak: Disconnected/Connected handlers arent cleaned up
  558. var voiceState = GetVoiceState(Discord.CurrentUser.Id).Value;
  559. await _audioLock.WaitAsync().ConfigureAwait(false);
  560. try
  561. {
  562. if (_audioClient != null)
  563. {
  564. await RepopulateAudioStreamsAsync().ConfigureAwait(false);
  565. await _audioClient.StartAsync(url, Discord.CurrentUser.Id, voiceState.VoiceSessionId, token).ConfigureAwait(false);
  566. }
  567. }
  568. catch (OperationCanceledException)
  569. {
  570. await DisconnectAudioInternalAsync().ConfigureAwait(false);
  571. }
  572. catch (Exception e)
  573. {
  574. await _audioConnectPromise.SetExceptionAsync(e).ConfigureAwait(false);
  575. await DisconnectAudioInternalAsync().ConfigureAwait(false);
  576. }
  577. finally
  578. {
  579. _audioLock.Release();
  580. }
  581. }
  582. internal async Task RepopulateAudioStreamsAsync()
  583. {
  584. await _audioClient.ClearInputStreamsAsync().ConfigureAwait(false); //We changed channels, end all current streams
  585. if (CurrentUser.VoiceChannel != null)
  586. {
  587. foreach (var pair in _voiceStates)
  588. {
  589. if (pair.Value.VoiceChannel?.Id == CurrentUser.VoiceChannel?.Id && pair.Key != CurrentUser.Id)
  590. await _audioClient.CreateInputStreamAsync(pair.Key).ConfigureAwait(false);
  591. }
  592. }
  593. }
  594. public override string ToString() => Name;
  595. private string DebuggerDisplay => $"{Name} ({Id})";
  596. internal SocketGuild Clone() => MemberwiseClone() as SocketGuild;
  597. //IGuild
  598. ulong? IGuild.AFKChannelId => AFKChannelId;
  599. IAudioClient IGuild.AudioClient => null;
  600. bool IGuild.Available => true;
  601. ulong IGuild.DefaultChannelId => DefaultChannel?.Id ?? 0;
  602. ulong? IGuild.EmbedChannelId => EmbedChannelId;
  603. ulong? IGuild.SystemChannelId => SystemChannelId;
  604. IRole IGuild.EveryoneRole => EveryoneRole;
  605. IReadOnlyCollection<IRole> IGuild.Roles => Roles;
  606. async Task<IReadOnlyCollection<IBan>> IGuild.GetBansAsync(RequestOptions options)
  607. => await GetBansAsync(options).ConfigureAwait(false);
  608. /// <inheritdoc/>
  609. async Task<IBan> IGuild.GetBanAsync(IUser user, RequestOptions options)
  610. => await GetBanAsync(user, options).ConfigureAwait(false);
  611. /// <inheritdoc/>
  612. async Task<IBan> IGuild.GetBanAsync(ulong userId, RequestOptions options)
  613. => await GetBanAsync(userId, options).ConfigureAwait(false);
  614. Task<IReadOnlyCollection<IGuildChannel>> IGuild.GetChannelsAsync(CacheMode mode, RequestOptions options)
  615. => Task.FromResult<IReadOnlyCollection<IGuildChannel>>(Channels);
  616. Task<IGuildChannel> IGuild.GetChannelAsync(ulong id, CacheMode mode, RequestOptions options)
  617. => Task.FromResult<IGuildChannel>(GetChannel(id));
  618. Task<IReadOnlyCollection<ITextChannel>> IGuild.GetTextChannelsAsync(CacheMode mode, RequestOptions options)
  619. => Task.FromResult<IReadOnlyCollection<ITextChannel>>(TextChannels);
  620. Task<ITextChannel> IGuild.GetTextChannelAsync(ulong id, CacheMode mode, RequestOptions options)
  621. => Task.FromResult<ITextChannel>(GetTextChannel(id));
  622. Task<IReadOnlyCollection<IVoiceChannel>> IGuild.GetVoiceChannelsAsync(CacheMode mode, RequestOptions options)
  623. => Task.FromResult<IReadOnlyCollection<IVoiceChannel>>(VoiceChannels);
  624. Task<IReadOnlyCollection<ICategoryChannel>> IGuild.GetCategoriesAsync(CacheMode mode , RequestOptions options)
  625. => Task.FromResult<IReadOnlyCollection<ICategoryChannel>>(CategoryChannels);
  626. Task<IVoiceChannel> IGuild.GetVoiceChannelAsync(ulong id, CacheMode mode, RequestOptions options)
  627. => Task.FromResult<IVoiceChannel>(GetVoiceChannel(id));
  628. Task<IVoiceChannel> IGuild.GetAFKChannelAsync(CacheMode mode, RequestOptions options)
  629. => Task.FromResult<IVoiceChannel>(AFKChannel);
  630. Task<ITextChannel> IGuild.GetDefaultChannelAsync(CacheMode mode, RequestOptions options)
  631. => Task.FromResult<ITextChannel>(DefaultChannel);
  632. Task<IGuildChannel> IGuild.GetEmbedChannelAsync(CacheMode mode, RequestOptions options)
  633. => Task.FromResult<IGuildChannel>(EmbedChannel);
  634. Task<ITextChannel> IGuild.GetSystemChannelAsync(CacheMode mode, RequestOptions options)
  635. => Task.FromResult<ITextChannel>(SystemChannel);
  636. async Task<ITextChannel> IGuild.CreateTextChannelAsync(string name, RequestOptions options)
  637. => await CreateTextChannelAsync(name, options).ConfigureAwait(false);
  638. async Task<IVoiceChannel> IGuild.CreateVoiceChannelAsync(string name, RequestOptions options)
  639. => await CreateVoiceChannelAsync(name, options).ConfigureAwait(false);
  640. async Task<ICategoryChannel> IGuild.CreateCategoryAsync(string name, RequestOptions options)
  641. => await CreateCategoryChannelAsync(name, options).ConfigureAwait(false);
  642. async Task<IReadOnlyCollection<IGuildIntegration>> IGuild.GetIntegrationsAsync(RequestOptions options)
  643. => await GetIntegrationsAsync(options).ConfigureAwait(false);
  644. async Task<IGuildIntegration> IGuild.CreateIntegrationAsync(ulong id, string type, RequestOptions options)
  645. => await CreateIntegrationAsync(id, type, options).ConfigureAwait(false);
  646. async Task<IReadOnlyCollection<IInviteMetadata>> IGuild.GetInvitesAsync(RequestOptions options)
  647. => await GetInvitesAsync(options).ConfigureAwait(false);
  648. IRole IGuild.GetRole(ulong id)
  649. => GetRole(id);
  650. async Task<IRole> IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options)
  651. => await CreateRoleAsync(name, permissions, color, isHoisted, options).ConfigureAwait(false);
  652. Task<IReadOnlyCollection<IGuildUser>> IGuild.GetUsersAsync(CacheMode mode, RequestOptions options)
  653. => Task.FromResult<IReadOnlyCollection<IGuildUser>>(Users);
  654. Task<IGuildUser> IGuild.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  655. => Task.FromResult<IGuildUser>(GetUser(id));
  656. Task<IGuildUser> IGuild.GetCurrentUserAsync(CacheMode mode, RequestOptions options)
  657. => Task.FromResult<IGuildUser>(CurrentUser);
  658. Task<IGuildUser> IGuild.GetOwnerAsync(CacheMode mode, RequestOptions options)
  659. => Task.FromResult<IGuildUser>(Owner);
  660. async Task<IWebhook> IGuild.GetWebhookAsync(ulong id, RequestOptions options)
  661. => await GetWebhookAsync(id, options);
  662. async Task<IReadOnlyCollection<IWebhook>> IGuild.GetWebhooksAsync(RequestOptions options)
  663. => await GetWebhooksAsync(options);
  664. }
  665. }