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 36 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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.OfType<SocketTextChannel>().ToImmutableArray();
  91. public IReadOnlyCollection<SocketVoiceChannel> VoiceChannels
  92. => Channels.OfType<SocketVoiceChannel>().ToImmutableArray();
  93. public IReadOnlyCollection<SocketCategoryChannel> CategoryChannels
  94. => Channels.OfType<SocketCategoryChannel>().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, Action<TextChannelProperties> func = null, RequestOptions options = null)
  295. => GuildHelper.CreateTextChannelAsync(this, Discord, name, options, func);
  296. public Task<RestVoiceChannel> CreateVoiceChannelAsync(string name, Action<VoiceChannelProperties> func = null, RequestOptions options = null)
  297. => GuildHelper.CreateVoiceChannelAsync(this, Discord, name, options, func);
  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. /// <summary>
  322. /// Gets the vanity invite URL of this guild.
  323. /// </summary>
  324. /// <param name="options">The options to be used when sending the request.</param>
  325. /// <returns>
  326. /// A partial metadata of the vanity invite found within this guild.
  327. /// </returns>
  328. public Task<RestInviteMetadata> GetVanityInviteAsync(RequestOptions options = null)
  329. => GuildHelper.GetVanityInviteAsync(this, Discord, options);
  330. //Roles
  331. public SocketRole GetRole(ulong id)
  332. {
  333. if (_roles.TryGetValue(id, out SocketRole value))
  334. return value;
  335. return null;
  336. }
  337. public Task<RestRole> CreateRoleAsync(string name, GuildPermissions? permissions = default(GuildPermissions?), Color? color = default(Color?),
  338. bool isHoisted = false, RequestOptions options = null)
  339. => GuildHelper.CreateRoleAsync(this, Discord, name, permissions, color, isHoisted, options);
  340. internal SocketRole AddRole(RoleModel model)
  341. {
  342. var role = SocketRole.Create(this, Discord.State, model);
  343. _roles[model.Id] = role;
  344. return role;
  345. }
  346. internal SocketRole RemoveRole(ulong id)
  347. {
  348. if (_roles.TryRemove(id, out SocketRole role))
  349. return role;
  350. return null;
  351. }
  352. //Users
  353. public SocketGuildUser GetUser(ulong id)
  354. {
  355. if (_members.TryGetValue(id, out SocketGuildUser member))
  356. return member;
  357. return null;
  358. }
  359. public Task<int> PruneUsersAsync(int days = 30, bool simulate = false, RequestOptions options = null)
  360. => GuildHelper.PruneUsersAsync(this, Discord, days, simulate, options);
  361. internal SocketGuildUser AddOrUpdateUser(UserModel model)
  362. {
  363. if (_members.TryGetValue(model.Id, out SocketGuildUser member))
  364. member.GlobalUser?.Update(Discord.State, model);
  365. else
  366. {
  367. member = SocketGuildUser.Create(this, Discord.State, model);
  368. member.GlobalUser.AddRef();
  369. _members[member.Id] = member;
  370. DownloadedMemberCount++;
  371. }
  372. return member;
  373. }
  374. internal SocketGuildUser AddOrUpdateUser(MemberModel model)
  375. {
  376. if (_members.TryGetValue(model.User.Id, out SocketGuildUser member))
  377. member.Update(Discord.State, model);
  378. else
  379. {
  380. member = SocketGuildUser.Create(this, Discord.State, model);
  381. member.GlobalUser.AddRef();
  382. _members[member.Id] = member;
  383. DownloadedMemberCount++;
  384. }
  385. return member;
  386. }
  387. internal SocketGuildUser AddOrUpdateUser(PresenceModel model)
  388. {
  389. if (_members.TryGetValue(model.User.Id, out SocketGuildUser member))
  390. member.Update(Discord.State, model, false);
  391. else
  392. {
  393. member = SocketGuildUser.Create(this, Discord.State, model);
  394. member.GlobalUser.AddRef();
  395. _members[member.Id] = member;
  396. DownloadedMemberCount++;
  397. }
  398. return member;
  399. }
  400. internal SocketGuildUser RemoveUser(ulong id)
  401. {
  402. if (_members.TryRemove(id, out SocketGuildUser member))
  403. {
  404. DownloadedMemberCount--;
  405. member.GlobalUser.RemoveRef(Discord);
  406. return member;
  407. }
  408. return null;
  409. }
  410. public async Task DownloadUsersAsync()
  411. {
  412. await Discord.DownloadUsersAsync(new[] { this }).ConfigureAwait(false);
  413. }
  414. internal void CompleteDownloadUsers()
  415. {
  416. _downloaderPromise.TrySetResultAsync(true);
  417. }
  418. //Audit logs
  419. public IAsyncEnumerable<IReadOnlyCollection<RestAuditLogEntry>> GetAuditLogsAsync(int limit, RequestOptions options = null)
  420. => GuildHelper.GetAuditLogsAsync(this, Discord, null, limit, options);
  421. //Webhooks
  422. public Task<RestWebhook> GetWebhookAsync(ulong id, RequestOptions options = null)
  423. => GuildHelper.GetWebhookAsync(this, Discord, id, options);
  424. public Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(RequestOptions options = null)
  425. => GuildHelper.GetWebhooksAsync(this, Discord, options);
  426. //Emotes
  427. public Task<GuildEmote> GetEmoteAsync(ulong id, RequestOptions options = null)
  428. => GuildHelper.GetEmoteAsync(this, Discord, id, options);
  429. public Task<GuildEmote> CreateEmoteAsync(string name, Image image, Optional<IEnumerable<IRole>> roles = default(Optional<IEnumerable<IRole>>), RequestOptions options = null)
  430. => GuildHelper.CreateEmoteAsync(this, Discord, name, image, roles, options);
  431. public Task<GuildEmote> ModifyEmoteAsync(GuildEmote emote, Action<EmoteProperties> func, RequestOptions options = null)
  432. => GuildHelper.ModifyEmoteAsync(this, Discord, emote.Id, func, options);
  433. public Task DeleteEmoteAsync(GuildEmote emote, RequestOptions options = null)
  434. => GuildHelper.DeleteEmoteAsync(this, Discord, emote.Id, options);
  435. //Voice States
  436. internal async Task<SocketVoiceState> AddOrUpdateVoiceStateAsync(ClientState state, VoiceStateModel model)
  437. {
  438. var voiceChannel = state.GetChannel(model.ChannelId.Value) as SocketVoiceChannel;
  439. var before = GetVoiceState(model.UserId) ?? SocketVoiceState.Default;
  440. var after = SocketVoiceState.Create(voiceChannel, model);
  441. _voiceStates[model.UserId] = after;
  442. if (_audioClient != null && before.VoiceChannel?.Id != after.VoiceChannel?.Id)
  443. {
  444. if (model.UserId == CurrentUser.Id)
  445. {
  446. if (after.VoiceChannel != null && _audioClient.ChannelId != after.VoiceChannel?.Id)
  447. {
  448. _audioClient.ChannelId = after.VoiceChannel.Id;
  449. await RepopulateAudioStreamsAsync().ConfigureAwait(false);
  450. }
  451. }
  452. else
  453. {
  454. await _audioClient.RemoveInputStreamAsync(model.UserId).ConfigureAwait(false); //User changed channels, end their stream
  455. if (CurrentUser.VoiceChannel != null && after.VoiceChannel?.Id == CurrentUser.VoiceChannel?.Id)
  456. await _audioClient.CreateInputStreamAsync(model.UserId).ConfigureAwait(false);
  457. }
  458. }
  459. return after;
  460. }
  461. internal SocketVoiceState? GetVoiceState(ulong id)
  462. {
  463. if (_voiceStates.TryGetValue(id, out SocketVoiceState voiceState))
  464. return voiceState;
  465. return null;
  466. }
  467. internal async Task<SocketVoiceState?> RemoveVoiceStateAsync(ulong id)
  468. {
  469. if (_voiceStates.TryRemove(id, out SocketVoiceState voiceState))
  470. {
  471. if (_audioClient != null)
  472. await _audioClient.RemoveInputStreamAsync(id).ConfigureAwait(false); //User changed channels, end their stream
  473. return voiceState;
  474. }
  475. return null;
  476. }
  477. //Audio
  478. internal AudioInStream GetAudioStream(ulong userId)
  479. {
  480. return _audioClient?.GetInputStream(userId);
  481. }
  482. internal async Task<IAudioClient> ConnectAudioAsync(ulong channelId, bool selfDeaf, bool selfMute, bool external)
  483. {
  484. TaskCompletionSource<AudioClient> promise;
  485. await _audioLock.WaitAsync().ConfigureAwait(false);
  486. try
  487. {
  488. await DisconnectAudioInternalAsync().ConfigureAwait(false);
  489. promise = new TaskCompletionSource<AudioClient>();
  490. _audioConnectPromise = promise;
  491. if (external)
  492. {
  493. var _ = promise.TrySetResultAsync(null);
  494. await Discord.ApiClient.SendVoiceStateUpdateAsync(Id, channelId, selfDeaf, selfMute).ConfigureAwait(false);
  495. return null;
  496. }
  497. if (_audioClient == null)
  498. {
  499. var audioClient = new AudioClient(this, Discord.GetAudioId(), channelId);
  500. audioClient.Disconnected += async ex =>
  501. {
  502. if (!promise.Task.IsCompleted)
  503. {
  504. try
  505. { audioClient.Dispose(); }
  506. catch { }
  507. _audioClient = null;
  508. if (ex != null)
  509. await promise.TrySetExceptionAsync(ex);
  510. else
  511. await promise.TrySetCanceledAsync();
  512. return;
  513. }
  514. };
  515. audioClient.Connected += () =>
  516. {
  517. var _ = promise.TrySetResultAsync(_audioClient);
  518. return Task.Delay(0);
  519. };
  520. _audioClient = audioClient;
  521. }
  522. await Discord.ApiClient.SendVoiceStateUpdateAsync(Id, channelId, selfDeaf, selfMute).ConfigureAwait(false);
  523. }
  524. catch
  525. {
  526. await DisconnectAudioInternalAsync().ConfigureAwait(false);
  527. throw;
  528. }
  529. finally
  530. {
  531. _audioLock.Release();
  532. }
  533. try
  534. {
  535. var timeoutTask = Task.Delay(15000);
  536. if (await Task.WhenAny(promise.Task, timeoutTask) == timeoutTask)
  537. throw new TimeoutException();
  538. return await promise.Task.ConfigureAwait(false);
  539. }
  540. catch
  541. {
  542. await DisconnectAudioAsync().ConfigureAwait(false);
  543. throw;
  544. }
  545. }
  546. internal async Task DisconnectAudioAsync()
  547. {
  548. await _audioLock.WaitAsync().ConfigureAwait(false);
  549. try
  550. {
  551. await DisconnectAudioInternalAsync().ConfigureAwait(false);
  552. }
  553. finally
  554. {
  555. _audioLock.Release();
  556. }
  557. }
  558. private async Task DisconnectAudioInternalAsync()
  559. {
  560. _audioConnectPromise?.TrySetCanceledAsync(); //Cancel any previous audio connection
  561. _audioConnectPromise = null;
  562. if (_audioClient != null)
  563. await _audioClient.StopAsync().ConfigureAwait(false);
  564. await Discord.ApiClient.SendVoiceStateUpdateAsync(Id, null, false, false).ConfigureAwait(false);
  565. _audioClient = null;
  566. }
  567. internal async Task FinishConnectAudio(string url, string token)
  568. {
  569. //TODO: Mem Leak: Disconnected/Connected handlers arent cleaned up
  570. var voiceState = GetVoiceState(Discord.CurrentUser.Id).Value;
  571. await _audioLock.WaitAsync().ConfigureAwait(false);
  572. try
  573. {
  574. if (_audioClient != null)
  575. {
  576. await RepopulateAudioStreamsAsync().ConfigureAwait(false);
  577. await _audioClient.StartAsync(url, Discord.CurrentUser.Id, voiceState.VoiceSessionId, token).ConfigureAwait(false);
  578. }
  579. }
  580. catch (OperationCanceledException)
  581. {
  582. await DisconnectAudioInternalAsync().ConfigureAwait(false);
  583. }
  584. catch (Exception e)
  585. {
  586. await _audioConnectPromise.SetExceptionAsync(e).ConfigureAwait(false);
  587. await DisconnectAudioInternalAsync().ConfigureAwait(false);
  588. }
  589. finally
  590. {
  591. _audioLock.Release();
  592. }
  593. }
  594. internal async Task RepopulateAudioStreamsAsync()
  595. {
  596. await _audioClient.ClearInputStreamsAsync().ConfigureAwait(false); //We changed channels, end all current streams
  597. if (CurrentUser.VoiceChannel != null)
  598. {
  599. foreach (var pair in _voiceStates)
  600. {
  601. if (pair.Value.VoiceChannel?.Id == CurrentUser.VoiceChannel?.Id && pair.Key != CurrentUser.Id)
  602. await _audioClient.CreateInputStreamAsync(pair.Key).ConfigureAwait(false);
  603. }
  604. }
  605. }
  606. public override string ToString() => Name;
  607. private string DebuggerDisplay => $"{Name} ({Id})";
  608. internal SocketGuild Clone() => MemberwiseClone() as SocketGuild;
  609. //IGuild
  610. ulong? IGuild.AFKChannelId => AFKChannelId;
  611. IAudioClient IGuild.AudioClient => null;
  612. bool IGuild.Available => true;
  613. ulong IGuild.DefaultChannelId => DefaultChannel?.Id ?? 0;
  614. ulong? IGuild.EmbedChannelId => EmbedChannelId;
  615. ulong? IGuild.SystemChannelId => SystemChannelId;
  616. IRole IGuild.EveryoneRole => EveryoneRole;
  617. IReadOnlyCollection<IRole> IGuild.Roles => Roles;
  618. async Task<IReadOnlyCollection<IBan>> IGuild.GetBansAsync(RequestOptions options)
  619. => await GetBansAsync(options).ConfigureAwait(false);
  620. /// <inheritdoc/>
  621. async Task<IBan> IGuild.GetBanAsync(IUser user, RequestOptions options)
  622. => await GetBanAsync(user, options).ConfigureAwait(false);
  623. /// <inheritdoc/>
  624. async Task<IBan> IGuild.GetBanAsync(ulong userId, RequestOptions options)
  625. => await GetBanAsync(userId, options).ConfigureAwait(false);
  626. Task<IReadOnlyCollection<IGuildChannel>> IGuild.GetChannelsAsync(CacheMode mode, RequestOptions options)
  627. => Task.FromResult<IReadOnlyCollection<IGuildChannel>>(Channels);
  628. Task<IGuildChannel> IGuild.GetChannelAsync(ulong id, CacheMode mode, RequestOptions options)
  629. => Task.FromResult<IGuildChannel>(GetChannel(id));
  630. Task<IReadOnlyCollection<ITextChannel>> IGuild.GetTextChannelsAsync(CacheMode mode, RequestOptions options)
  631. => Task.FromResult<IReadOnlyCollection<ITextChannel>>(TextChannels);
  632. Task<ITextChannel> IGuild.GetTextChannelAsync(ulong id, CacheMode mode, RequestOptions options)
  633. => Task.FromResult<ITextChannel>(GetTextChannel(id));
  634. Task<IReadOnlyCollection<IVoiceChannel>> IGuild.GetVoiceChannelsAsync(CacheMode mode, RequestOptions options)
  635. => Task.FromResult<IReadOnlyCollection<IVoiceChannel>>(VoiceChannels);
  636. Task<IReadOnlyCollection<ICategoryChannel>> IGuild.GetCategoriesAsync(CacheMode mode , RequestOptions options)
  637. => Task.FromResult<IReadOnlyCollection<ICategoryChannel>>(CategoryChannels);
  638. Task<IVoiceChannel> IGuild.GetVoiceChannelAsync(ulong id, CacheMode mode, RequestOptions options)
  639. => Task.FromResult<IVoiceChannel>(GetVoiceChannel(id));
  640. Task<IVoiceChannel> IGuild.GetAFKChannelAsync(CacheMode mode, RequestOptions options)
  641. => Task.FromResult<IVoiceChannel>(AFKChannel);
  642. Task<ITextChannel> IGuild.GetDefaultChannelAsync(CacheMode mode, RequestOptions options)
  643. => Task.FromResult<ITextChannel>(DefaultChannel);
  644. Task<IGuildChannel> IGuild.GetEmbedChannelAsync(CacheMode mode, RequestOptions options)
  645. => Task.FromResult<IGuildChannel>(EmbedChannel);
  646. Task<ITextChannel> IGuild.GetSystemChannelAsync(CacheMode mode, RequestOptions options)
  647. => Task.FromResult<ITextChannel>(SystemChannel);
  648. async Task<ITextChannel> IGuild.CreateTextChannelAsync(string name, Action<TextChannelProperties> func, RequestOptions options)
  649. => await CreateTextChannelAsync(name, func, options).ConfigureAwait(false);
  650. async Task<IVoiceChannel> IGuild.CreateVoiceChannelAsync(string name, Action<VoiceChannelProperties> func, RequestOptions options)
  651. => await CreateVoiceChannelAsync(name, func, options).ConfigureAwait(false);
  652. async Task<ICategoryChannel> IGuild.CreateCategoryAsync(string name, RequestOptions options)
  653. => await CreateCategoryChannelAsync(name, options).ConfigureAwait(false);
  654. async Task<IReadOnlyCollection<IGuildIntegration>> IGuild.GetIntegrationsAsync(RequestOptions options)
  655. => await GetIntegrationsAsync(options).ConfigureAwait(false);
  656. async Task<IGuildIntegration> IGuild.CreateIntegrationAsync(ulong id, string type, RequestOptions options)
  657. => await CreateIntegrationAsync(id, type, options).ConfigureAwait(false);
  658. async Task<IReadOnlyCollection<IInviteMetadata>> IGuild.GetInvitesAsync(RequestOptions options)
  659. => await GetInvitesAsync(options).ConfigureAwait(false);
  660. /// <inheritdoc />
  661. async Task<IInviteMetadata> IGuild.GetVanityInviteAsync(RequestOptions options)
  662. => await GetVanityInviteAsync(options).ConfigureAwait(false);
  663. IRole IGuild.GetRole(ulong id)
  664. => GetRole(id);
  665. async Task<IRole> IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options)
  666. => await CreateRoleAsync(name, permissions, color, isHoisted, options).ConfigureAwait(false);
  667. Task<IReadOnlyCollection<IGuildUser>> IGuild.GetUsersAsync(CacheMode mode, RequestOptions options)
  668. => Task.FromResult<IReadOnlyCollection<IGuildUser>>(Users);
  669. Task<IGuildUser> IGuild.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  670. => Task.FromResult<IGuildUser>(GetUser(id));
  671. Task<IGuildUser> IGuild.GetCurrentUserAsync(CacheMode mode, RequestOptions options)
  672. => Task.FromResult<IGuildUser>(CurrentUser);
  673. Task<IGuildUser> IGuild.GetOwnerAsync(CacheMode mode, RequestOptions options)
  674. => Task.FromResult<IGuildUser>(Owner);
  675. async Task<IReadOnlyCollection<IAuditLogEntry>> IGuild.GetAuditLogsAsync(int limit, CacheMode cacheMode, RequestOptions options)
  676. {
  677. if (cacheMode == CacheMode.AllowDownload)
  678. return (await GetAuditLogsAsync(limit, options).FlattenAsync().ConfigureAwait(false)).ToImmutableArray();
  679. else
  680. return ImmutableArray.Create<IAuditLogEntry>();
  681. }
  682. async Task<IWebhook> IGuild.GetWebhookAsync(ulong id, RequestOptions options)
  683. => await GetWebhookAsync(id, options);
  684. async Task<IReadOnlyCollection<IWebhook>> IGuild.GetWebhooksAsync(RequestOptions options)
  685. => await GetWebhooksAsync(options);
  686. }
  687. }