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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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. public ulong OwnerId { get; private set; }
  48. public SocketGuildUser Owner => GetUser(OwnerId);
  49. public string VoiceRegionId { get; private set; }
  50. public string IconId { get; private set; }
  51. public string SplashId { get; private set; }
  52. public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
  53. public SocketTextChannel DefaultChannel => GetTextChannel(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 SocketVoiceChannel AFKChannel
  62. {
  63. get
  64. {
  65. var id = AFKChannelId;
  66. return id.HasValue ? GetVoiceChannel(id.Value) : null;
  67. }
  68. }
  69. public SocketGuildChannel EmbedChannel
  70. {
  71. get
  72. {
  73. var id = EmbedChannelId;
  74. return id.HasValue ? GetChannel(id.Value) : null;
  75. }
  76. }
  77. public IReadOnlyCollection<SocketTextChannel> TextChannels
  78. => Channels.Select(x => x as SocketTextChannel).Where(x => x != null).ToImmutableArray();
  79. public IReadOnlyCollection<SocketVoiceChannel> VoiceChannels
  80. => Channels.Select(x => x as SocketVoiceChannel).Where(x => x != null).ToImmutableArray();
  81. public SocketGuildUser CurrentUser => _members.TryGetValue(Discord.CurrentUser.Id, out SocketGuildUser member) ? member : null;
  82. public SocketRole EveryoneRole => GetRole(Id);
  83. public IReadOnlyCollection<SocketGuildChannel> Channels
  84. {
  85. get
  86. {
  87. var channels = _channels;
  88. var state = Discord.State;
  89. return channels.Select(x => state.GetChannel(x) as SocketGuildChannel).Where(x => x != null).ToReadOnlyCollection(channels);
  90. }
  91. }
  92. public IReadOnlyCollection<GuildEmote> Emotes => _emotes;
  93. public IReadOnlyCollection<string> Features => _features;
  94. public IReadOnlyCollection<SocketGuildUser> Users => _members.ToReadOnlyCollection();
  95. public IReadOnlyCollection<SocketRole> Roles => _roles.ToReadOnlyCollection();
  96. internal SocketGuild(DiscordSocketClient client, ulong id)
  97. : base(client, id)
  98. {
  99. _audioLock = new SemaphoreSlim(1, 1);
  100. _emotes = ImmutableArray.Create<GuildEmote>();
  101. _features = ImmutableArray.Create<string>();
  102. }
  103. internal static SocketGuild Create(DiscordSocketClient discord, ClientState state, ExtendedModel model)
  104. {
  105. var entity = new SocketGuild(discord, model.Id);
  106. entity.Update(state, model);
  107. return entity;
  108. }
  109. internal void Update(ClientState state, ExtendedModel model)
  110. {
  111. IsAvailable = !(model.Unavailable ?? false);
  112. if (!IsAvailable)
  113. {
  114. if (_channels == null)
  115. _channels = new ConcurrentHashSet<ulong>();
  116. if (_members == null)
  117. _members = new ConcurrentDictionary<ulong, SocketGuildUser>();
  118. if (_roles == null)
  119. _roles = new ConcurrentDictionary<ulong, SocketRole>();
  120. /*if (Emojis == null)
  121. _emojis = ImmutableArray.Create<Emoji>();
  122. if (Features == null)
  123. _features = ImmutableArray.Create<string>();*/
  124. _syncPromise = new TaskCompletionSource<bool>();
  125. _downloaderPromise = new TaskCompletionSource<bool>();
  126. return;
  127. }
  128. Update(state, model as Model);
  129. var channels = new ConcurrentHashSet<ulong>(ConcurrentHashSet.DefaultConcurrencyLevel, (int)(model.Channels.Length * 1.05));
  130. {
  131. for (int i = 0; i < model.Channels.Length; i++)
  132. {
  133. var channel = SocketGuildChannel.Create(this, state, model.Channels[i]);
  134. state.AddChannel(channel);
  135. channels.TryAdd(channel.Id);
  136. }
  137. }
  138. _channels = channels;
  139. var members = new ConcurrentDictionary<ulong, SocketGuildUser>(ConcurrentHashSet.DefaultConcurrencyLevel, (int)(model.Members.Length * 1.05));
  140. {
  141. for (int i = 0; i < model.Members.Length; i++)
  142. {
  143. var member = SocketGuildUser.Create(this, state, model.Members[i]);
  144. members.TryAdd(member.Id, member);
  145. }
  146. DownloadedMemberCount = members.Count;
  147. for (int i = 0; i < model.Presences.Length; i++)
  148. {
  149. if (members.TryGetValue(model.Presences[i].User.Id, out SocketGuildUser member))
  150. member.Update(state, model.Presences[i], true);
  151. else
  152. Debug.Assert(false);
  153. }
  154. }
  155. _members = members;
  156. MemberCount = model.MemberCount;
  157. var voiceStates = new ConcurrentDictionary<ulong, SocketVoiceState>(ConcurrentHashSet.DefaultConcurrencyLevel, (int)(model.VoiceStates.Length * 1.05));
  158. {
  159. for (int i = 0; i < model.VoiceStates.Length; i++)
  160. {
  161. SocketVoiceChannel channel = null;
  162. if (model.VoiceStates[i].ChannelId.HasValue)
  163. channel = state.GetChannel(model.VoiceStates[i].ChannelId.Value) as SocketVoiceChannel;
  164. var voiceState = SocketVoiceState.Create(channel, model.VoiceStates[i]);
  165. voiceStates.TryAdd(model.VoiceStates[i].UserId, voiceState);
  166. }
  167. }
  168. _voiceStates = voiceStates;
  169. _syncPromise = new TaskCompletionSource<bool>();
  170. _downloaderPromise = new TaskCompletionSource<bool>();
  171. if (Discord.ApiClient.AuthTokenType != TokenType.User)
  172. {
  173. var _ = _syncPromise.TrySetResultAsync(true);
  174. /*if (!model.Large)
  175. _ = _downloaderPromise.TrySetResultAsync(true);*/
  176. }
  177. }
  178. internal void Update(ClientState state, Model model)
  179. {
  180. AFKChannelId = model.AFKChannelId;
  181. EmbedChannelId = model.EmbedChannelId;
  182. AFKTimeout = model.AFKTimeout;
  183. IsEmbeddable = model.EmbedEnabled;
  184. IconId = model.Icon;
  185. Name = model.Name;
  186. OwnerId = model.OwnerId;
  187. VoiceRegionId = model.Region;
  188. SplashId = model.Splash;
  189. VerificationLevel = model.VerificationLevel;
  190. MfaLevel = model.MfaLevel;
  191. DefaultMessageNotifications = model.DefaultMessageNotifications;
  192. if (model.Emojis != null)
  193. {
  194. var emojis = ImmutableArray.CreateBuilder<GuildEmote>(model.Emojis.Length);
  195. for (int i = 0; i < model.Emojis.Length; i++)
  196. emojis.Add(model.Emojis[i].ToEntity());
  197. _emotes = emojis.ToImmutable();
  198. }
  199. else
  200. _emotes = ImmutableArray.Create<GuildEmote>();
  201. if (model.Features != null)
  202. _features = model.Features.ToImmutableArray();
  203. else
  204. _features = ImmutableArray.Create<string>();
  205. var roles = new ConcurrentDictionary<ulong, SocketRole>(ConcurrentHashSet.DefaultConcurrencyLevel, (int)(model.Roles.Length * 1.05));
  206. if (model.Roles != null)
  207. {
  208. for (int i = 0; i < model.Roles.Length; i++)
  209. {
  210. var role = SocketRole.Create(this, state, model.Roles[i]);
  211. roles.TryAdd(role.Id, role);
  212. }
  213. }
  214. _roles = roles;
  215. }
  216. internal void Update(ClientState state, GuildSyncModel model)
  217. {
  218. var members = new ConcurrentDictionary<ulong, SocketGuildUser>(ConcurrentHashSet.DefaultConcurrencyLevel, (int)(model.Members.Length * 1.05));
  219. {
  220. for (int i = 0; i < model.Members.Length; i++)
  221. {
  222. var member = SocketGuildUser.Create(this, state, model.Members[i]);
  223. members.TryAdd(member.Id, member);
  224. }
  225. DownloadedMemberCount = members.Count;
  226. for (int i = 0; i < model.Presences.Length; i++)
  227. {
  228. if (members.TryGetValue(model.Presences[i].User.Id, out SocketGuildUser member))
  229. member.Update(state, model.Presences[i], true);
  230. else
  231. Debug.Assert(false);
  232. }
  233. }
  234. _members = members;
  235. var _ = _syncPromise.TrySetResultAsync(true);
  236. /*if (!model.Large)
  237. _ = _downloaderPromise.TrySetResultAsync(true);*/
  238. }
  239. internal void Update(ClientState state, EmojiUpdateModel model)
  240. {
  241. var emotes = ImmutableArray.CreateBuilder<GuildEmote>(model.Emojis.Length);
  242. for (int i = 0; i < model.Emojis.Length; i++)
  243. emotes.Add(model.Emojis[i].ToEntity());
  244. _emotes = emotes.ToImmutable();
  245. }
  246. //General
  247. public Task DeleteAsync(RequestOptions options = null)
  248. => GuildHelper.DeleteAsync(this, Discord, options);
  249. public Task ModifyAsync(Action<GuildProperties> func, RequestOptions options = null)
  250. => GuildHelper.ModifyAsync(this, Discord, func, options);
  251. public Task ModifyEmbedAsync(Action<GuildEmbedProperties> func, RequestOptions options = null)
  252. => GuildHelper.ModifyEmbedAsync(this, Discord, func, options);
  253. public Task ReorderChannelsAsync(IEnumerable<ReorderChannelProperties> args, RequestOptions options = null)
  254. => GuildHelper.ReorderChannelsAsync(this, Discord, args, options);
  255. public Task ReorderRolesAsync(IEnumerable<ReorderRoleProperties> args, RequestOptions options = null)
  256. => GuildHelper.ReorderRolesAsync(this, Discord, args, options);
  257. public Task LeaveAsync(RequestOptions options = null)
  258. => GuildHelper.LeaveAsync(this, Discord, options);
  259. //Bans
  260. public Task<IReadOnlyCollection<RestBan>> GetBansAsync(RequestOptions options = null)
  261. => GuildHelper.GetBansAsync(this, Discord, options);
  262. public Task AddBanAsync(IUser user, int pruneDays = 0, string reason = null, RequestOptions options = null)
  263. => GuildHelper.AddBanAsync(this, Discord, user.Id, pruneDays, reason, options);
  264. public Task AddBanAsync(ulong userId, int pruneDays = 0, string reason = null, RequestOptions options = null)
  265. => GuildHelper.AddBanAsync(this, Discord, userId, pruneDays, reason, options);
  266. public Task RemoveBanAsync(IUser user, RequestOptions options = null)
  267. => GuildHelper.RemoveBanAsync(this, Discord, user.Id, options);
  268. public Task RemoveBanAsync(ulong userId, RequestOptions options = null)
  269. => GuildHelper.RemoveBanAsync(this, Discord, userId, options);
  270. //Channels
  271. public SocketGuildChannel GetChannel(ulong id)
  272. {
  273. var channel = Discord.State.GetChannel(id) as SocketGuildChannel;
  274. if (channel?.Guild.Id == Id)
  275. return channel;
  276. return null;
  277. }
  278. public SocketTextChannel GetTextChannel(ulong id)
  279. => GetChannel(id) as SocketTextChannel;
  280. public SocketVoiceChannel GetVoiceChannel(ulong id)
  281. => GetChannel(id) as SocketVoiceChannel;
  282. public Task<RestTextChannel> CreateTextChannelAsync(string name, RequestOptions options = null)
  283. => GuildHelper.CreateTextChannelAsync(this, Discord, name, options);
  284. public Task<RestVoiceChannel> CreateVoiceChannelAsync(string name, RequestOptions options = null)
  285. => GuildHelper.CreateVoiceChannelAsync(this, Discord, name, options);
  286. internal SocketGuildChannel AddChannel(ClientState state, ChannelModel model)
  287. {
  288. var channel = SocketGuildChannel.Create(this, state, model);
  289. _channels.TryAdd(model.Id);
  290. state.AddChannel(channel);
  291. return channel;
  292. }
  293. internal SocketGuildChannel RemoveChannel(ClientState state, ulong id)
  294. {
  295. if (_channels.TryRemove(id))
  296. return state.RemoveChannel(id) as SocketGuildChannel;
  297. return null;
  298. }
  299. //Integrations
  300. public Task<IReadOnlyCollection<RestGuildIntegration>> GetIntegrationsAsync(RequestOptions options = null)
  301. => GuildHelper.GetIntegrationsAsync(this, Discord, options);
  302. public Task<RestGuildIntegration> CreateIntegrationAsync(ulong id, string type, RequestOptions options = null)
  303. => GuildHelper.CreateIntegrationAsync(this, Discord, id, type, options);
  304. //Invites
  305. public Task<IReadOnlyCollection<RestInviteMetadata>> GetInvitesAsync(RequestOptions options = null)
  306. => GuildHelper.GetInvitesAsync(this, Discord, options);
  307. //Roles
  308. public SocketRole GetRole(ulong id)
  309. {
  310. if (_roles.TryGetValue(id, out SocketRole value))
  311. return value;
  312. return null;
  313. }
  314. public Task<RestRole> CreateRoleAsync(string name, GuildPermissions? permissions = default(GuildPermissions?), Color? color = default(Color?),
  315. bool isHoisted = false, RequestOptions options = null)
  316. => GuildHelper.CreateRoleAsync(this, Discord, name, permissions, color, isHoisted, options);
  317. internal SocketRole AddRole(RoleModel model)
  318. {
  319. var role = SocketRole.Create(this, Discord.State, model);
  320. _roles[model.Id] = role;
  321. return role;
  322. }
  323. internal SocketRole RemoveRole(ulong id)
  324. {
  325. if (_roles.TryRemove(id, out SocketRole role))
  326. return role;
  327. return null;
  328. }
  329. //Users
  330. public SocketGuildUser GetUser(ulong id)
  331. {
  332. if (_members.TryGetValue(id, out SocketGuildUser member))
  333. return member;
  334. return null;
  335. }
  336. public Task<int> PruneUsersAsync(int days = 30, bool simulate = false, RequestOptions options = null)
  337. => GuildHelper.PruneUsersAsync(this, Discord, days, simulate, options);
  338. internal SocketGuildUser AddOrUpdateUser(UserModel model)
  339. {
  340. if (_members.TryGetValue(model.Id, out SocketGuildUser member))
  341. member.GlobalUser?.Update(Discord.State, model);
  342. else
  343. {
  344. member = SocketGuildUser.Create(this, Discord.State, model);
  345. member.GlobalUser.AddRef();
  346. _members[member.Id] = member;
  347. DownloadedMemberCount++;
  348. }
  349. return member;
  350. }
  351. internal SocketGuildUser AddOrUpdateUser(MemberModel model)
  352. {
  353. if (_members.TryGetValue(model.User.Id, out SocketGuildUser member))
  354. member.Update(Discord.State, model);
  355. else
  356. {
  357. member = SocketGuildUser.Create(this, Discord.State, model);
  358. member.GlobalUser.AddRef();
  359. _members[member.Id] = member;
  360. DownloadedMemberCount++;
  361. }
  362. return member;
  363. }
  364. internal SocketGuildUser AddOrUpdateUser(PresenceModel model)
  365. {
  366. if (_members.TryGetValue(model.User.Id, out SocketGuildUser member))
  367. member.Update(Discord.State, model, false);
  368. else
  369. {
  370. member = SocketGuildUser.Create(this, Discord.State, model);
  371. member.GlobalUser.AddRef();
  372. _members[member.Id] = member;
  373. DownloadedMemberCount++;
  374. }
  375. return member;
  376. }
  377. internal SocketGuildUser RemoveUser(ulong id)
  378. {
  379. if (_members.TryRemove(id, out SocketGuildUser member))
  380. {
  381. DownloadedMemberCount--;
  382. member.GlobalUser.RemoveRef(Discord);
  383. return member;
  384. }
  385. return null;
  386. }
  387. public async Task DownloadUsersAsync()
  388. {
  389. await Discord.DownloadUsersAsync(new[] { this }).ConfigureAwait(false);
  390. }
  391. internal void CompleteDownloadUsers()
  392. {
  393. _downloaderPromise.TrySetResultAsync(true);
  394. }
  395. //Webhooks
  396. public Task<RestWebhook> GetWebhookAsync(ulong id, RequestOptions options = null)
  397. => GuildHelper.GetWebhookAsync(this, Discord, id, options);
  398. public Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(RequestOptions options = null)
  399. => GuildHelper.GetWebhooksAsync(this, Discord, options);
  400. //Voice States
  401. internal async Task<SocketVoiceState> AddOrUpdateVoiceStateAsync(ClientState state, VoiceStateModel model)
  402. {
  403. var voiceChannel = state.GetChannel(model.ChannelId.Value) as SocketVoiceChannel;
  404. var before = GetVoiceState(model.UserId) ?? SocketVoiceState.Default;
  405. var after = SocketVoiceState.Create(voiceChannel, model);
  406. _voiceStates[model.UserId] = after;
  407. if (_audioClient != null && before.VoiceChannel?.Id != after.VoiceChannel?.Id)
  408. {
  409. if (model.UserId == CurrentUser.Id)
  410. {
  411. if (after.VoiceChannel != null && _audioClient.ChannelId != after.VoiceChannel?.Id)
  412. {
  413. _audioClient.ChannelId = after.VoiceChannel.Id;
  414. await RepopulateAudioStreamsAsync().ConfigureAwait(false);
  415. }
  416. }
  417. else
  418. {
  419. await _audioClient.RemoveInputStreamAsync(model.UserId).ConfigureAwait(false); //User changed channels, end their stream
  420. if (CurrentUser.VoiceChannel != null && after.VoiceChannel?.Id == CurrentUser.VoiceChannel?.Id)
  421. await _audioClient.CreateInputStreamAsync(model.UserId).ConfigureAwait(false);
  422. }
  423. }
  424. return after;
  425. }
  426. internal SocketVoiceState? GetVoiceState(ulong id)
  427. {
  428. if (_voiceStates.TryGetValue(id, out SocketVoiceState voiceState))
  429. return voiceState;
  430. return null;
  431. }
  432. internal async Task<SocketVoiceState?> RemoveVoiceStateAsync(ulong id)
  433. {
  434. if (_voiceStates.TryRemove(id, out SocketVoiceState voiceState))
  435. {
  436. if (_audioClient != null)
  437. await _audioClient.RemoveInputStreamAsync(id).ConfigureAwait(false); //User changed channels, end their stream
  438. return voiceState;
  439. }
  440. return null;
  441. }
  442. //Audio
  443. internal AudioInStream GetAudioStream(ulong userId)
  444. {
  445. return _audioClient?.GetInputStream(userId);
  446. }
  447. internal async Task<IAudioClient> ConnectAudioAsync(ulong channelId, bool selfDeaf, bool selfMute, Action<IAudioClient> configAction)
  448. {
  449. selfDeaf = false;
  450. selfMute = false;
  451. TaskCompletionSource<AudioClient> promise;
  452. await _audioLock.WaitAsync().ConfigureAwait(false);
  453. try
  454. {
  455. await DisconnectAudioInternalAsync().ConfigureAwait(false);
  456. promise = new TaskCompletionSource<AudioClient>();
  457. _audioConnectPromise = promise;
  458. if (_audioClient == null)
  459. {
  460. var audioClient = new AudioClient(this, Discord.GetAudioId(), channelId);
  461. audioClient.Disconnected += async ex =>
  462. {
  463. if (!promise.Task.IsCompleted)
  464. {
  465. try { audioClient.Dispose(); } catch { }
  466. _audioClient = null;
  467. if (ex != null)
  468. await promise.TrySetExceptionAsync(ex);
  469. else
  470. await promise.TrySetCanceledAsync();
  471. return;
  472. }
  473. };
  474. audioClient.Connected += () =>
  475. {
  476. var _ = promise.TrySetResultAsync(_audioClient);
  477. return Task.Delay(0);
  478. };
  479. configAction?.Invoke(audioClient);
  480. _audioClient = audioClient;
  481. }
  482. await Discord.ApiClient.SendVoiceStateUpdateAsync(Id, channelId, selfDeaf, selfMute).ConfigureAwait(false);
  483. }
  484. catch (Exception)
  485. {
  486. await DisconnectAudioInternalAsync().ConfigureAwait(false);
  487. throw;
  488. }
  489. finally
  490. {
  491. _audioLock.Release();
  492. }
  493. try
  494. {
  495. var timeoutTask = Task.Delay(15000);
  496. if (await Task.WhenAny(promise.Task, timeoutTask) == timeoutTask)
  497. throw new TimeoutException();
  498. return await promise.Task.ConfigureAwait(false);
  499. }
  500. catch (Exception)
  501. {
  502. await DisconnectAudioAsync().ConfigureAwait(false);
  503. throw;
  504. }
  505. }
  506. internal async Task DisconnectAudioAsync()
  507. {
  508. await _audioLock.WaitAsync().ConfigureAwait(false);
  509. try
  510. {
  511. await DisconnectAudioInternalAsync().ConfigureAwait(false);
  512. }
  513. finally
  514. {
  515. _audioLock.Release();
  516. }
  517. }
  518. private async Task DisconnectAudioInternalAsync()
  519. {
  520. _audioConnectPromise?.TrySetCanceledAsync(); //Cancel any previous audio connection
  521. _audioConnectPromise = null;
  522. if (_audioClient != null)
  523. await _audioClient.StopAsync().ConfigureAwait(false);
  524. await Discord.ApiClient.SendVoiceStateUpdateAsync(Id, null, false, false).ConfigureAwait(false);
  525. _audioClient = null;
  526. }
  527. internal async Task FinishConnectAudio(string url, string token)
  528. {
  529. //TODO: Mem Leak: Disconnected/Connected handlers arent cleaned up
  530. var voiceState = GetVoiceState(Discord.CurrentUser.Id).Value;
  531. await _audioLock.WaitAsync().ConfigureAwait(false);
  532. try
  533. {
  534. await RepopulateAudioStreamsAsync().ConfigureAwait(false);
  535. await _audioClient.StartAsync(url, Discord.CurrentUser.Id, voiceState.VoiceSessionId, token).ConfigureAwait(false);
  536. }
  537. catch (OperationCanceledException)
  538. {
  539. await DisconnectAudioInternalAsync().ConfigureAwait(false);
  540. }
  541. catch (Exception e)
  542. {
  543. await _audioConnectPromise.SetExceptionAsync(e).ConfigureAwait(false);
  544. await DisconnectAudioInternalAsync().ConfigureAwait(false);
  545. }
  546. finally
  547. {
  548. _audioLock.Release();
  549. }
  550. }
  551. internal async Task RepopulateAudioStreamsAsync()
  552. {
  553. await _audioClient.ClearInputStreamsAsync().ConfigureAwait(false); //We changed channels, end all current streams
  554. if (CurrentUser.VoiceChannel != null)
  555. {
  556. foreach (var pair in _voiceStates)
  557. {
  558. if (pair.Value.VoiceChannel?.Id == CurrentUser.VoiceChannel?.Id && pair.Key != CurrentUser.Id)
  559. await _audioClient.CreateInputStreamAsync(pair.Key).ConfigureAwait(false);
  560. }
  561. }
  562. }
  563. public override string ToString() => Name;
  564. private string DebuggerDisplay => $"{Name} ({Id})";
  565. internal SocketGuild Clone() => MemberwiseClone() as SocketGuild;
  566. //IGuild
  567. ulong? IGuild.AFKChannelId => AFKChannelId;
  568. IAudioClient IGuild.AudioClient => null;
  569. bool IGuild.Available => true;
  570. ulong IGuild.DefaultChannelId => Id;
  571. ulong? IGuild.EmbedChannelId => EmbedChannelId;
  572. IRole IGuild.EveryoneRole => EveryoneRole;
  573. IReadOnlyCollection<IRole> IGuild.Roles => Roles;
  574. async Task<IReadOnlyCollection<IBan>> IGuild.GetBansAsync(RequestOptions options)
  575. => await GetBansAsync(options).ConfigureAwait(false);
  576. Task<IReadOnlyCollection<IGuildChannel>> IGuild.GetChannelsAsync(CacheMode mode, RequestOptions options)
  577. => Task.FromResult<IReadOnlyCollection<IGuildChannel>>(Channels);
  578. Task<IGuildChannel> IGuild.GetChannelAsync(ulong id, CacheMode mode, RequestOptions options)
  579. => Task.FromResult<IGuildChannel>(GetChannel(id));
  580. Task<IReadOnlyCollection<ITextChannel>> IGuild.GetTextChannelsAsync(CacheMode mode, RequestOptions options)
  581. => Task.FromResult<IReadOnlyCollection<ITextChannel>>(TextChannels);
  582. Task<ITextChannel> IGuild.GetTextChannelAsync(ulong id, CacheMode mode, RequestOptions options)
  583. => Task.FromResult<ITextChannel>(GetTextChannel(id));
  584. Task<IReadOnlyCollection<IVoiceChannel>> IGuild.GetVoiceChannelsAsync(CacheMode mode, RequestOptions options)
  585. => Task.FromResult<IReadOnlyCollection<IVoiceChannel>>(VoiceChannels);
  586. Task<IVoiceChannel> IGuild.GetVoiceChannelAsync(ulong id, CacheMode mode, RequestOptions options)
  587. => Task.FromResult<IVoiceChannel>(GetVoiceChannel(id));
  588. Task<IVoiceChannel> IGuild.GetAFKChannelAsync(CacheMode mode, RequestOptions options)
  589. => Task.FromResult<IVoiceChannel>(AFKChannel);
  590. Task<ITextChannel> IGuild.GetDefaultChannelAsync(CacheMode mode, RequestOptions options)
  591. => Task.FromResult<ITextChannel>(DefaultChannel);
  592. Task<IGuildChannel> IGuild.GetEmbedChannelAsync(CacheMode mode, RequestOptions options)
  593. => Task.FromResult<IGuildChannel>(EmbedChannel);
  594. async Task<ITextChannel> IGuild.CreateTextChannelAsync(string name, RequestOptions options)
  595. => await CreateTextChannelAsync(name, options).ConfigureAwait(false);
  596. async Task<IVoiceChannel> IGuild.CreateVoiceChannelAsync(string name, RequestOptions options)
  597. => await CreateVoiceChannelAsync(name, options).ConfigureAwait(false);
  598. async Task<IReadOnlyCollection<IGuildIntegration>> IGuild.GetIntegrationsAsync(RequestOptions options)
  599. => await GetIntegrationsAsync(options).ConfigureAwait(false);
  600. async Task<IGuildIntegration> IGuild.CreateIntegrationAsync(ulong id, string type, RequestOptions options)
  601. => await CreateIntegrationAsync(id, type, options).ConfigureAwait(false);
  602. async Task<IReadOnlyCollection<IInviteMetadata>> IGuild.GetInvitesAsync(RequestOptions options)
  603. => await GetInvitesAsync(options).ConfigureAwait(false);
  604. IRole IGuild.GetRole(ulong id)
  605. => GetRole(id);
  606. async Task<IRole> IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options)
  607. => await CreateRoleAsync(name, permissions, color, isHoisted, options).ConfigureAwait(false);
  608. Task<IReadOnlyCollection<IGuildUser>> IGuild.GetUsersAsync(CacheMode mode, RequestOptions options)
  609. => Task.FromResult<IReadOnlyCollection<IGuildUser>>(Users);
  610. Task<IGuildUser> IGuild.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  611. => Task.FromResult<IGuildUser>(GetUser(id));
  612. Task<IGuildUser> IGuild.GetCurrentUserAsync(CacheMode mode, RequestOptions options)
  613. => Task.FromResult<IGuildUser>(CurrentUser);
  614. Task<IGuildUser> IGuild.GetOwnerAsync(CacheMode mode, RequestOptions options)
  615. => Task.FromResult<IGuildUser>(Owner);
  616. Task IGuild.DownloadUsersAsync() { throw new NotSupportedException(); }
  617. async Task<IWebhook> IGuild.GetWebhookAsync(ulong id, RequestOptions options)
  618. => await GetWebhookAsync(id, options);
  619. async Task<IReadOnlyCollection<IWebhook>> IGuild.GetWebhooksAsync(RequestOptions options)
  620. => await GetWebhooksAsync(options);
  621. }
  622. }