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.

DiscordSocketClient.cs 102 kB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849
  1. using Discord.API;
  2. using Discord.API.Gateway;
  3. using Discord.Logging;
  4. using Discord.Net.Converters;
  5. using Discord.Net.Udp;
  6. using Discord.Net.WebSockets;
  7. using Discord.Rest;
  8. using Newtonsoft.Json;
  9. using Newtonsoft.Json.Linq;
  10. using System;
  11. using System.Collections.Concurrent;
  12. using System.Collections.Generic;
  13. using System.Collections.Immutable;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using GameModel = Discord.API.Game;
  19. namespace Discord.WebSocket
  20. {
  21. public partial class DiscordSocketClient : BaseDiscordClient, IDiscordClient
  22. {
  23. private readonly ConcurrentQueue<ulong> _largeGuilds;
  24. private readonly JsonSerializer _serializer;
  25. private readonly SemaphoreSlim _connectionGroupLock;
  26. private readonly DiscordSocketClient _parentClient;
  27. private readonly ConcurrentQueue<long> _heartbeatTimes;
  28. private readonly ConnectionManager _connection;
  29. private readonly Logger _gatewayLogger;
  30. private readonly SemaphoreSlim _stateLock;
  31. private string _sessionId;
  32. private int _lastSeq;
  33. private ImmutableDictionary<string, RestVoiceRegion> _voiceRegions;
  34. private Task _heartbeatTask, _guildDownloadTask;
  35. private int _unavailableGuildCount;
  36. private long _lastGuildAvailableTime, _lastMessageTime;
  37. private int _nextAudioId;
  38. private DateTimeOffset? _statusSince;
  39. private RestApplication _applicationInfo;
  40. /// <summary> Gets the shard of of this client. </summary>
  41. public int ShardId { get; }
  42. /// <summary> Gets the current connection state of this client. </summary>
  43. public ConnectionState ConnectionState => _connection.State;
  44. /// <summary> Gets the estimated round-trip latency, in milliseconds, to the gateway server. </summary>
  45. public int Latency { get; private set; }
  46. internal UserStatus Status { get; private set; } = UserStatus.Online;
  47. internal Game? Game { get; private set; }
  48. //From DiscordSocketConfig
  49. internal int TotalShards { get; private set; }
  50. internal int MessageCacheSize { get; private set; }
  51. internal int LargeThreshold { get; private set; }
  52. internal ClientState State { get; private set; }
  53. internal UdpSocketProvider UdpSocketProvider { get; private set; }
  54. internal WebSocketProvider WebSocketProvider { get; private set; }
  55. internal bool AlwaysDownloadUsers { get; private set; }
  56. internal int? HandlerTimeout { get; private set; }
  57. internal new DiscordSocketApiClient ApiClient => base.ApiClient as DiscordSocketApiClient;
  58. public new SocketSelfUser CurrentUser { get => base.CurrentUser as SocketSelfUser; private set => base.CurrentUser = value; }
  59. public IReadOnlyCollection<SocketGuild> Guilds => State.Guilds;
  60. public IReadOnlyCollection<ISocketPrivateChannel> PrivateChannels => State.PrivateChannels;
  61. public IReadOnlyCollection<SocketDMChannel> DMChannels
  62. => State.PrivateChannels.Select(x => x as SocketDMChannel).Where(x => x != null).ToImmutableArray();
  63. public IReadOnlyCollection<SocketGroupChannel> GroupChannels
  64. => State.PrivateChannels.Select(x => x as SocketGroupChannel).Where(x => x != null).ToImmutableArray();
  65. public IReadOnlyCollection<RestVoiceRegion> VoiceRegions => _voiceRegions.ToReadOnlyCollection();
  66. /// <summary> Creates a new REST/WebSocket discord client. </summary>
  67. public DiscordSocketClient() : this(new DiscordSocketConfig()) { }
  68. /// <summary> Creates a new REST/WebSocket discord client. </summary>
  69. public DiscordSocketClient(DiscordSocketConfig config) : this(config, CreateApiClient(config), null, null) { }
  70. internal DiscordSocketClient(DiscordSocketConfig config, SemaphoreSlim groupLock, DiscordSocketClient parentClient) : this(config, CreateApiClient(config), groupLock, parentClient) { }
  71. private DiscordSocketClient(DiscordSocketConfig config, API.DiscordSocketApiClient client, SemaphoreSlim groupLock, DiscordSocketClient parentClient)
  72. : base(config, client)
  73. {
  74. ShardId = config.ShardId ?? 0;
  75. TotalShards = config.TotalShards ?? 1;
  76. MessageCacheSize = config.MessageCacheSize;
  77. LargeThreshold = config.LargeThreshold;
  78. UdpSocketProvider = config.UdpSocketProvider;
  79. WebSocketProvider = config.WebSocketProvider;
  80. AlwaysDownloadUsers = config.AlwaysDownloadUsers;
  81. HandlerTimeout = config.HandlerTimeout;
  82. State = new ClientState(0, 0);
  83. _heartbeatTimes = new ConcurrentQueue<long>();
  84. _stateLock = new SemaphoreSlim(1, 1);
  85. _gatewayLogger = LogManager.CreateLogger(ShardId == 0 && TotalShards == 1 ? "Gateway" : $"Shard #{ShardId}");
  86. _connection = new ConnectionManager(_stateLock, _gatewayLogger, config.ConnectionTimeout,
  87. OnConnectingAsync, OnDisconnectingAsync, x => ApiClient.Disconnected += x);
  88. _connection.Connected += () => TimedInvokeAsync(_connectedEvent, nameof(Connected));
  89. _connection.Disconnected += (ex, recon) => TimedInvokeAsync(_disconnectedEvent, nameof(Disconnected), ex);
  90. _nextAudioId = 1;
  91. _connectionGroupLock = groupLock;
  92. _parentClient = parentClient;
  93. _serializer = new JsonSerializer { ContractResolver = new DiscordContractResolver() };
  94. _serializer.Error += (s, e) =>
  95. {
  96. _gatewayLogger.WarningAsync("Serializer Error", e.ErrorContext.Error).GetAwaiter().GetResult();
  97. e.ErrorContext.Handled = true;
  98. };
  99. ApiClient.SentGatewayMessage += async opCode => await _gatewayLogger.DebugAsync($"Sent {opCode}").ConfigureAwait(false);
  100. ApiClient.ReceivedGatewayEvent += ProcessMessageAsync;
  101. LeftGuild += async g => await _gatewayLogger.InfoAsync($"Left {g.Name}").ConfigureAwait(false);
  102. JoinedGuild += async g => await _gatewayLogger.InfoAsync($"Joined {g.Name}").ConfigureAwait(false);
  103. GuildAvailable += async g => await _gatewayLogger.VerboseAsync($"Connected to {g.Name}").ConfigureAwait(false);
  104. GuildUnavailable += async g => await _gatewayLogger.VerboseAsync($"Disconnected from {g.Name}").ConfigureAwait(false);
  105. LatencyUpdated += async (old, val) => await _gatewayLogger.DebugAsync($"Latency = {val} ms").ConfigureAwait(false);
  106. GuildAvailable += g =>
  107. {
  108. if (ConnectionState == ConnectionState.Connected && AlwaysDownloadUsers && !g.HasAllMembers)
  109. {
  110. var _ = g.DownloadUsersAsync();
  111. }
  112. return Task.Delay(0);
  113. };
  114. _voiceRegions = ImmutableDictionary.Create<string, RestVoiceRegion>();
  115. _largeGuilds = new ConcurrentQueue<ulong>();
  116. }
  117. private static API.DiscordSocketApiClient CreateApiClient(DiscordSocketConfig config)
  118. => new API.DiscordSocketApiClient(config.RestClientProvider, config.WebSocketProvider, DiscordRestConfig.UserAgent, config.GatewayHost);
  119. internal override void Dispose(bool disposing)
  120. {
  121. if (disposing)
  122. {
  123. StopAsync().GetAwaiter().GetResult();
  124. ApiClient.Dispose();
  125. }
  126. }
  127. internal override async Task OnLoginAsync(TokenType tokenType, string token)
  128. {
  129. if (_parentClient == null)
  130. {
  131. var voiceRegions = await ApiClient.GetVoiceRegionsAsync(new RequestOptions { IgnoreState = true, RetryMode = RetryMode.AlwaysRetry }).ConfigureAwait(false);
  132. _voiceRegions = voiceRegions.Select(x => RestVoiceRegion.Create(this, x)).ToImmutableDictionary(x => x.Id);
  133. }
  134. else
  135. _voiceRegions = _parentClient._voiceRegions;
  136. }
  137. internal override async Task OnLogoutAsync()
  138. {
  139. await StopAsync().ConfigureAwait(false);
  140. _applicationInfo = null;
  141. _voiceRegions = ImmutableDictionary.Create<string, RestVoiceRegion>();
  142. }
  143. public async Task StartAsync()
  144. => await _connection.StartAsync().ConfigureAwait(false);
  145. public async Task StopAsync()
  146. => await _connection.StopAsync().ConfigureAwait(false);
  147. private async Task OnConnectingAsync()
  148. {
  149. if (_connectionGroupLock != null)
  150. await _connectionGroupLock.WaitAsync(_connection.CancelToken).ConfigureAwait(false);
  151. try
  152. {
  153. await _gatewayLogger.DebugAsync("Connecting ApiClient").ConfigureAwait(false);
  154. await ApiClient.ConnectAsync().ConfigureAwait(false);
  155. if (_sessionId != null)
  156. {
  157. await _gatewayLogger.DebugAsync("Resuming").ConfigureAwait(false);
  158. await ApiClient.SendResumeAsync(_sessionId, _lastSeq).ConfigureAwait(false);
  159. }
  160. else
  161. {
  162. await _gatewayLogger.DebugAsync("Identifying").ConfigureAwait(false);
  163. await ApiClient.SendIdentifyAsync(shardID: ShardId, totalShards: TotalShards).ConfigureAwait(false);
  164. }
  165. //Wait for READY
  166. await _connection.WaitAsync().ConfigureAwait(false);
  167. await _gatewayLogger.DebugAsync("Sending Status").ConfigureAwait(false);
  168. await SendStatusAsync().ConfigureAwait(false);
  169. }
  170. finally
  171. {
  172. if (_connectionGroupLock != null)
  173. {
  174. await Task.Delay(5000).ConfigureAwait(false);
  175. _connectionGroupLock.Release();
  176. }
  177. }
  178. }
  179. private async Task OnDisconnectingAsync(Exception ex)
  180. {
  181. await _gatewayLogger.DebugAsync("Disconnecting ApiClient").ConfigureAwait(false);
  182. await ApiClient.DisconnectAsync().ConfigureAwait(false);
  183. //Wait for tasks to complete
  184. await _gatewayLogger.DebugAsync("Waiting for heartbeater").ConfigureAwait(false);
  185. var heartbeatTask = _heartbeatTask;
  186. if (heartbeatTask != null)
  187. await heartbeatTask.ConfigureAwait(false);
  188. _heartbeatTask = null;
  189. while (_heartbeatTimes.TryDequeue(out long time)) { }
  190. _lastMessageTime = 0;
  191. await _gatewayLogger.DebugAsync("Waiting for guild downloader").ConfigureAwait(false);
  192. var guildDownloadTask = _guildDownloadTask;
  193. if (guildDownloadTask != null)
  194. await guildDownloadTask.ConfigureAwait(false);
  195. _guildDownloadTask = null;
  196. //Clear large guild queue
  197. await _gatewayLogger.DebugAsync("Clearing large guild queue").ConfigureAwait(false);
  198. while (_largeGuilds.TryDequeue(out ulong guildId)) { }
  199. //Raise virtual GUILD_UNAVAILABLEs
  200. await _gatewayLogger.DebugAsync("Raising virtual GuildUnavailables").ConfigureAwait(false);
  201. foreach (var guild in State.Guilds)
  202. {
  203. if (guild.IsAvailable)
  204. await GuildUnavailableAsync(guild).ConfigureAwait(false);
  205. }
  206. }
  207. /// <inheritdoc />
  208. public async Task<RestApplication> GetApplicationInfoAsync()
  209. {
  210. return _applicationInfo ?? (_applicationInfo = await ClientHelper.GetApplicationInfoAsync(this, new RequestOptions()));
  211. }
  212. /// <inheritdoc />
  213. public SocketGuild GetGuild(ulong id)
  214. {
  215. return State.GetGuild(id);
  216. }
  217. /// <inheritdoc />
  218. public Task<RestGuild> CreateGuildAsync(string name, IVoiceRegion region, Stream jpegIcon = null)
  219. => ClientHelper.CreateGuildAsync(this, name, region, jpegIcon, new RequestOptions());
  220. /// <inheritdoc />
  221. public SocketChannel GetChannel(ulong id)
  222. {
  223. return State.GetChannel(id);
  224. }
  225. /// <inheritdoc />
  226. public Task<IReadOnlyCollection<RestConnection>> GetConnectionsAsync()
  227. => ClientHelper.GetConnectionsAsync(this, new RequestOptions());
  228. /// <inheritdoc />
  229. public Task<RestInvite> GetInviteAsync(string inviteId)
  230. => ClientHelper.GetInviteAsync(this, inviteId, new RequestOptions());
  231. /// <inheritdoc />
  232. public SocketUser GetUser(ulong id)
  233. {
  234. return State.GetUser(id);
  235. }
  236. /// <inheritdoc />
  237. public SocketUser GetUser(string username, string discriminator)
  238. {
  239. return State.Users.FirstOrDefault(x => x.Discriminator == discriminator && x.Username == username);
  240. }
  241. internal SocketGlobalUser GetOrCreateUser(ClientState state, Discord.API.User model)
  242. {
  243. return state.GetOrAddUser(model.Id, x =>
  244. {
  245. var user = SocketGlobalUser.Create(this, state, model);
  246. user.GlobalUser.AddRef();
  247. return user;
  248. });
  249. }
  250. internal SocketGlobalUser GetOrCreateSelfUser(ClientState state, Discord.API.User model)
  251. {
  252. return state.GetOrAddUser(model.Id, x =>
  253. {
  254. var user = SocketGlobalUser.Create(this, state, model);
  255. user.GlobalUser.AddRef();
  256. user.Presence = new SocketPresence(UserStatus.Online, null);
  257. return user;
  258. });
  259. }
  260. internal void RemoveUser(ulong id)
  261. {
  262. State.RemoveUser(id);
  263. }
  264. /// <inheritdoc />
  265. public RestVoiceRegion GetVoiceRegion(string id)
  266. {
  267. if (_voiceRegions.TryGetValue(id, out RestVoiceRegion region))
  268. return region;
  269. return null;
  270. }
  271. /// <summary> Downloads the users list for the provided guilds, if they don't have a complete list. </summary>
  272. public async Task DownloadUsersAsync(IEnumerable<IGuild> guilds)
  273. {
  274. if (ConnectionState == ConnectionState.Connected)
  275. {
  276. //Race condition leads to guilds being requested twice, probably okay
  277. await ProcessUserDownloadsAsync(guilds.Select(x => GetGuild(x.Id)).Where(x => x != null)).ConfigureAwait(false);
  278. }
  279. }
  280. private async Task ProcessUserDownloadsAsync(IEnumerable<SocketGuild> guilds)
  281. {
  282. var cachedGuilds = guilds.ToImmutableArray();
  283. const short batchSize = 50;
  284. ulong[] batchIds = new ulong[Math.Min(batchSize, cachedGuilds.Length)];
  285. Task[] batchTasks = new Task[batchIds.Length];
  286. int batchCount = (cachedGuilds.Length + (batchSize - 1)) / batchSize;
  287. for (int i = 0, k = 0; i < batchCount; i++)
  288. {
  289. bool isLast = i == batchCount - 1;
  290. int count = isLast ? (batchIds.Length - (batchCount - 1) * batchSize) : batchSize;
  291. for (int j = 0; j < count; j++, k++)
  292. {
  293. var guild = cachedGuilds[k];
  294. batchIds[j] = guild.Id;
  295. batchTasks[j] = guild.DownloaderPromise;
  296. }
  297. await ApiClient.SendRequestMembersAsync(batchIds).ConfigureAwait(false);
  298. if (isLast && batchCount > 1)
  299. await Task.WhenAll(batchTasks.Take(count)).ConfigureAwait(false);
  300. else
  301. await Task.WhenAll(batchTasks).ConfigureAwait(false);
  302. }
  303. }
  304. public async Task SetStatusAsync(UserStatus status)
  305. {
  306. Status = status;
  307. if (status == UserStatus.AFK)
  308. _statusSince = DateTimeOffset.UtcNow;
  309. else
  310. _statusSince = null;
  311. await SendStatusAsync().ConfigureAwait(false);
  312. }
  313. public async Task SetGameAsync(string name, string streamUrl = null, StreamType streamType = StreamType.NotStreaming)
  314. {
  315. if (name != null)
  316. Game = new Game(name, streamUrl, streamType);
  317. else
  318. Game = null;
  319. await SendStatusAsync().ConfigureAwait(false);
  320. }
  321. private async Task SendStatusAsync()
  322. {
  323. if (CurrentUser == null)
  324. return;
  325. var game = Game;
  326. var status = Status;
  327. var statusSince = _statusSince;
  328. CurrentUser.Presence = new SocketPresence(status, game);
  329. GameModel gameModel;
  330. if (game != null)
  331. {
  332. gameModel = new API.Game
  333. {
  334. Name = game.Value.Name,
  335. StreamType = game.Value.StreamType,
  336. StreamUrl = game.Value.StreamUrl
  337. };
  338. }
  339. else
  340. gameModel = null;
  341. await ApiClient.SendStatusUpdateAsync(
  342. status,
  343. status == UserStatus.AFK,
  344. statusSince != null ? DateTimeUtils.ToUnixMilliseconds(_statusSince.Value) : (long?)null,
  345. gameModel).ConfigureAwait(false);
  346. }
  347. private async Task ProcessMessageAsync(GatewayOpCode opCode, int? seq, string type, object payload)
  348. {
  349. if (seq != null)
  350. _lastSeq = seq.Value;
  351. _lastMessageTime = Environment.TickCount;
  352. try
  353. {
  354. switch (opCode)
  355. {
  356. case GatewayOpCode.Hello:
  357. {
  358. await _gatewayLogger.DebugAsync("Received Hello").ConfigureAwait(false);
  359. var data = (payload as JToken).ToObject<HelloEvent>(_serializer);
  360. _heartbeatTask = RunHeartbeatAsync(data.HeartbeatInterval, _connection.CancelToken);
  361. }
  362. break;
  363. case GatewayOpCode.Heartbeat:
  364. {
  365. await _gatewayLogger.DebugAsync("Received Heartbeat").ConfigureAwait(false);
  366. await ApiClient.SendHeartbeatAsync(_lastSeq).ConfigureAwait(false);
  367. }
  368. break;
  369. case GatewayOpCode.HeartbeatAck:
  370. {
  371. await _gatewayLogger.DebugAsync("Received HeartbeatAck").ConfigureAwait(false);
  372. if (_heartbeatTimes.TryDequeue(out long time))
  373. {
  374. int latency = (int)(Environment.TickCount - time);
  375. int before = Latency;
  376. Latency = latency;
  377. await TimedInvokeAsync(_latencyUpdatedEvent, nameof(LatencyUpdated), before, latency).ConfigureAwait(false);
  378. }
  379. }
  380. break;
  381. case GatewayOpCode.InvalidSession:
  382. {
  383. await _gatewayLogger.DebugAsync("Received InvalidSession").ConfigureAwait(false);
  384. await _gatewayLogger.WarningAsync("Failed to resume previous session").ConfigureAwait(false);
  385. _sessionId = null;
  386. _lastSeq = 0;
  387. bool retry = (bool)payload;
  388. if (retry)
  389. _connection.Reconnect(); //TODO: Untested
  390. else
  391. await ApiClient.SendIdentifyAsync(shardID: ShardId, totalShards: TotalShards).ConfigureAwait(false);
  392. }
  393. break;
  394. case GatewayOpCode.Reconnect:
  395. {
  396. await _gatewayLogger.DebugAsync("Received Reconnect").ConfigureAwait(false);
  397. _connection.Error(new Exception("Server requested a reconnect"));
  398. }
  399. break;
  400. case GatewayOpCode.Dispatch:
  401. switch (type)
  402. {
  403. //Connection
  404. case "READY":
  405. {
  406. try
  407. {
  408. await _gatewayLogger.DebugAsync("Received Dispatch (READY)").ConfigureAwait(false);
  409. var data = (payload as JToken).ToObject<ReadyEvent>(_serializer);
  410. var state = new ClientState(data.Guilds.Length, data.PrivateChannels.Length);
  411. var currentUser = SocketSelfUser.Create(this, state, data.User);
  412. ApiClient.CurrentUserId = currentUser.Id;
  413. int unavailableGuilds = 0;
  414. for (int i = 0; i < data.Guilds.Length; i++)
  415. {
  416. var model = data.Guilds[i];
  417. var guild = AddGuild(model, state);
  418. if (!guild.IsAvailable || ApiClient.AuthTokenType == TokenType.User)
  419. unavailableGuilds++;
  420. else
  421. await GuildAvailableAsync(guild).ConfigureAwait(false);
  422. }
  423. for (int i = 0; i < data.PrivateChannels.Length; i++)
  424. AddPrivateChannel(data.PrivateChannels[i], state);
  425. _sessionId = data.SessionId;
  426. _unavailableGuildCount = unavailableGuilds;
  427. CurrentUser = currentUser;
  428. State = state;
  429. }
  430. catch (Exception ex)
  431. {
  432. _connection.CriticalError(new Exception("Processing READY failed", ex));
  433. return;
  434. }
  435. if (ApiClient.AuthTokenType == TokenType.User)
  436. await SyncGuildsAsync().ConfigureAwait(false);
  437. _lastGuildAvailableTime = Environment.TickCount;
  438. _guildDownloadTask = WaitForGuildsAsync(_connection.CancelToken, _gatewayLogger)
  439. .ContinueWith(async x =>
  440. {
  441. if (x.IsFaulted)
  442. {
  443. _connection.Error(x.Exception);
  444. return;
  445. }
  446. else if (_connection.CancelToken.IsCancellationRequested)
  447. return;
  448. await TimedInvokeAsync(_readyEvent, nameof(Ready)).ConfigureAwait(false);
  449. await _gatewayLogger.InfoAsync("Ready").ConfigureAwait(false);
  450. });
  451. var _ = _connection.CompleteAsync();
  452. }
  453. break;
  454. case "RESUMED":
  455. {
  456. await _gatewayLogger.DebugAsync("Received Dispatch (RESUMED)").ConfigureAwait(false);
  457. var _ = _connection.CompleteAsync();
  458. //Notify the client that these guilds are available again
  459. foreach (var guild in State.Guilds)
  460. {
  461. if (guild.IsAvailable)
  462. await GuildAvailableAsync(guild).ConfigureAwait(false);
  463. }
  464. await _gatewayLogger.InfoAsync("Resumed previous session").ConfigureAwait(false);
  465. }
  466. break;
  467. //Guilds
  468. case "GUILD_CREATE":
  469. {
  470. var data = (payload as JToken).ToObject<ExtendedGuild>(_serializer);
  471. if (data.Unavailable == false)
  472. {
  473. type = "GUILD_AVAILABLE";
  474. _lastGuildAvailableTime = Environment.TickCount;
  475. await _gatewayLogger.DebugAsync($"Received Dispatch (GUILD_AVAILABLE)").ConfigureAwait(false);
  476. var guild = State.GetGuild(data.Id);
  477. if (guild != null)
  478. {
  479. guild.Update(State, data);
  480. if (_unavailableGuildCount != 0)
  481. _unavailableGuildCount--;
  482. await GuildAvailableAsync(guild).ConfigureAwait(false);
  483. if (guild.DownloadedMemberCount >= guild.MemberCount && !guild.DownloaderPromise.IsCompleted)
  484. {
  485. guild.CompleteDownloadUsers();
  486. await TimedInvokeAsync(_guildMembersDownloadedEvent, nameof(GuildMembersDownloaded), guild).ConfigureAwait(false);
  487. }
  488. }
  489. else
  490. {
  491. await UnknownGuildAsync(type, data.Id).ConfigureAwait(false);
  492. return;
  493. }
  494. }
  495. else
  496. {
  497. await _gatewayLogger.DebugAsync($"Received Dispatch (GUILD_CREATE)").ConfigureAwait(false);
  498. var guild = AddGuild(data, State);
  499. if (guild != null)
  500. {
  501. if (ApiClient.AuthTokenType == TokenType.User)
  502. await SyncGuildsAsync().ConfigureAwait(false);
  503. await TimedInvokeAsync(_joinedGuildEvent, nameof(JoinedGuild), guild).ConfigureAwait(false);
  504. }
  505. else
  506. {
  507. await UnknownGuildAsync(type, data.Id).ConfigureAwait(false);
  508. return;
  509. }
  510. }
  511. }
  512. break;
  513. case "GUILD_UPDATE":
  514. {
  515. await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_UPDATE)").ConfigureAwait(false);
  516. var data = (payload as JToken).ToObject<API.Guild>(_serializer);
  517. var guild = State.GetGuild(data.Id);
  518. if (guild != null)
  519. {
  520. var before = guild.Clone();
  521. guild.Update(State, data);
  522. await TimedInvokeAsync(_guildUpdatedEvent, nameof(GuildUpdated), before, guild).ConfigureAwait(false);
  523. }
  524. else
  525. {
  526. await UnknownGuildAsync(type, data.Id).ConfigureAwait(false);
  527. return;
  528. }
  529. }
  530. break;
  531. case "GUILD_EMOJIS_UPDATE":
  532. {
  533. await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_EMOJIS_UPDATE)").ConfigureAwait(false);
  534. var data = (payload as JToken).ToObject<API.Gateway.GuildEmojiUpdateEvent>(_serializer);
  535. var guild = State.GetGuild(data.GuildId);
  536. if (guild != null)
  537. {
  538. var before = guild.Clone();
  539. guild.Update(State, data);
  540. await TimedInvokeAsync(_guildUpdatedEvent, nameof(GuildUpdated), before, guild).ConfigureAwait(false);
  541. }
  542. else
  543. {
  544. await UnknownGuildAsync(type, data.GuildId).ConfigureAwait(false);
  545. return;
  546. }
  547. }
  548. break;
  549. case "GUILD_SYNC":
  550. {
  551. await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_SYNC)").ConfigureAwait(false);
  552. var data = (payload as JToken).ToObject<GuildSyncEvent>(_serializer);
  553. var guild = State.GetGuild(data.Id);
  554. if (guild != null)
  555. {
  556. var before = guild.Clone();
  557. guild.Update(State, data);
  558. //This is treated as an extension of GUILD_AVAILABLE
  559. _unavailableGuildCount--;
  560. _lastGuildAvailableTime = Environment.TickCount;
  561. await GuildAvailableAsync(guild).ConfigureAwait(false);
  562. await TimedInvokeAsync(_guildUpdatedEvent, nameof(GuildUpdated), before, guild).ConfigureAwait(false);
  563. }
  564. else
  565. {
  566. await UnknownGuildAsync(type, data.Id).ConfigureAwait(false);
  567. return;
  568. }
  569. }
  570. break;
  571. case "GUILD_DELETE":
  572. {
  573. var data = (payload as JToken).ToObject<ExtendedGuild>(_serializer);
  574. if (data.Unavailable == true)
  575. {
  576. type = "GUILD_UNAVAILABLE";
  577. await _gatewayLogger.DebugAsync($"Received Dispatch (GUILD_UNAVAILABLE)").ConfigureAwait(false);
  578. var guild = State.GetGuild(data.Id);
  579. if (guild != null)
  580. {
  581. await GuildUnavailableAsync(guild).ConfigureAwait(false);
  582. _unavailableGuildCount++;
  583. }
  584. else
  585. {
  586. await UnknownGuildAsync(type, data.Id).ConfigureAwait(false);
  587. return;
  588. }
  589. }
  590. else
  591. {
  592. await _gatewayLogger.DebugAsync($"Received Dispatch (GUILD_DELETE)").ConfigureAwait(false);
  593. var guild = RemoveGuild(data.Id);
  594. if (guild != null)
  595. {
  596. await GuildUnavailableAsync(guild).ConfigureAwait(false);
  597. await TimedInvokeAsync(_leftGuildEvent, nameof(LeftGuild), guild).ConfigureAwait(false);
  598. }
  599. else
  600. {
  601. await UnknownGuildAsync(type, data.Id).ConfigureAwait(false);
  602. return;
  603. }
  604. }
  605. }
  606. break;
  607. //Channels
  608. case "CHANNEL_CREATE":
  609. {
  610. await _gatewayLogger.DebugAsync("Received Dispatch (CHANNEL_CREATE)").ConfigureAwait(false);
  611. var data = (payload as JToken).ToObject<API.Channel>(_serializer);
  612. SocketChannel channel = null;
  613. if (data.GuildId.IsSpecified)
  614. {
  615. var guild = State.GetGuild(data.GuildId.Value);
  616. if (guild != null)
  617. {
  618. channel = guild.AddChannel(State, data);
  619. if (!guild.IsSynced)
  620. {
  621. await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
  622. return;
  623. }
  624. }
  625. else
  626. {
  627. await UnknownGuildAsync(type, data.GuildId.Value).ConfigureAwait(false);
  628. return;
  629. }
  630. }
  631. else
  632. {
  633. channel = State.GetChannel(data.Id);
  634. if (channel != null)
  635. return; //Discord may send duplicate CHANNEL_CREATEs for DMs
  636. channel = AddPrivateChannel(data, State) as SocketChannel;
  637. }
  638. if (channel != null)
  639. await TimedInvokeAsync(_channelCreatedEvent, nameof(ChannelCreated), channel).ConfigureAwait(false);
  640. }
  641. break;
  642. case "CHANNEL_UPDATE":
  643. {
  644. await _gatewayLogger.DebugAsync("Received Dispatch (CHANNEL_UPDATE)").ConfigureAwait(false);
  645. var data = (payload as JToken).ToObject<API.Channel>(_serializer);
  646. var channel = State.GetChannel(data.Id);
  647. if (channel != null)
  648. {
  649. var before = channel.Clone();
  650. channel.Update(State, data);
  651. var guild = (channel as SocketGuildChannel)?.Guild;
  652. if (!(guild?.IsSynced ?? true))
  653. {
  654. await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
  655. return;
  656. }
  657. await TimedInvokeAsync(_channelUpdatedEvent, nameof(ChannelUpdated), before, channel).ConfigureAwait(false);
  658. }
  659. else
  660. {
  661. await UnknownChannelAsync(type, data.Id).ConfigureAwait(false);
  662. return;
  663. }
  664. }
  665. break;
  666. case "CHANNEL_DELETE":
  667. {
  668. await _gatewayLogger.DebugAsync("Received Dispatch (CHANNEL_DELETE)").ConfigureAwait(false);
  669. SocketChannel channel = null;
  670. var data = (payload as JToken).ToObject<API.Channel>(_serializer);
  671. if (data.GuildId.IsSpecified)
  672. {
  673. var guild = State.GetGuild(data.GuildId.Value);
  674. if (guild != null)
  675. {
  676. channel = guild.RemoveChannel(State, data.Id);
  677. if (!guild.IsSynced)
  678. {
  679. await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
  680. return;
  681. }
  682. }
  683. else
  684. {
  685. await UnknownGuildAsync(type, data.GuildId.Value).ConfigureAwait(false);
  686. return;
  687. }
  688. }
  689. else
  690. channel = RemovePrivateChannel(data.Id) as SocketChannel;
  691. if (channel != null)
  692. await TimedInvokeAsync(_channelDestroyedEvent, nameof(ChannelDestroyed), channel).ConfigureAwait(false);
  693. else
  694. {
  695. await UnknownChannelAsync(type, data.Id, data.GuildId.GetValueOrDefault(0)).ConfigureAwait(false);
  696. return;
  697. }
  698. }
  699. break;
  700. //Members
  701. case "GUILD_MEMBER_ADD":
  702. {
  703. await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_MEMBER_ADD)").ConfigureAwait(false);
  704. var data = (payload as JToken).ToObject<GuildMemberAddEvent>(_serializer);
  705. var guild = State.GetGuild(data.GuildId);
  706. if (guild != null)
  707. {
  708. var user = guild.AddOrUpdateUser(data);
  709. guild.MemberCount++;
  710. if (!guild.IsSynced)
  711. {
  712. await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
  713. return;
  714. }
  715. await TimedInvokeAsync(_userJoinedEvent, nameof(UserJoined), user).ConfigureAwait(false);
  716. }
  717. else
  718. {
  719. await UnknownGuildAsync(type, data.GuildId).ConfigureAwait(false);
  720. return;
  721. }
  722. }
  723. break;
  724. case "GUILD_MEMBER_UPDATE":
  725. {
  726. await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_MEMBER_UPDATE)").ConfigureAwait(false);
  727. var data = (payload as JToken).ToObject<GuildMemberUpdateEvent>(_serializer);
  728. var guild = State.GetGuild(data.GuildId);
  729. if (guild != null)
  730. {
  731. var user = guild.GetUser(data.User.Id);
  732. if (!guild.IsSynced)
  733. {
  734. await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
  735. return;
  736. }
  737. if (user != null)
  738. {
  739. var before = user.Clone();
  740. user.Update(State, data);
  741. await TimedInvokeAsync(_guildMemberUpdatedEvent, nameof(GuildMemberUpdated), before, user).ConfigureAwait(false);
  742. }
  743. else
  744. {
  745. if (!guild.HasAllMembers)
  746. await IncompleteGuildUserAsync(type, data.User.Id, data.GuildId).ConfigureAwait(false);
  747. else
  748. await UnknownGuildUserAsync(type, data.User.Id, data.GuildId).ConfigureAwait(false);
  749. return;
  750. }
  751. }
  752. else
  753. {
  754. await UnknownGuildAsync(type, data.GuildId).ConfigureAwait(false);
  755. return;
  756. }
  757. }
  758. break;
  759. case "GUILD_MEMBER_REMOVE":
  760. {
  761. await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_MEMBER_REMOVE)").ConfigureAwait(false);
  762. var data = (payload as JToken).ToObject<GuildMemberRemoveEvent>(_serializer);
  763. var guild = State.GetGuild(data.GuildId);
  764. if (guild != null)
  765. {
  766. var user = guild.RemoveUser(data.User.Id);
  767. guild.MemberCount--;
  768. if (!guild.IsSynced)
  769. {
  770. await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
  771. return;
  772. }
  773. if (user != null)
  774. await TimedInvokeAsync(_userLeftEvent, nameof(UserLeft), user).ConfigureAwait(false);
  775. else
  776. {
  777. if (!guild.HasAllMembers)
  778. await IncompleteGuildUserAsync(type, data.User.Id, data.GuildId).ConfigureAwait(false);
  779. else
  780. await UnknownGuildUserAsync(type, data.User.Id, data.GuildId).ConfigureAwait(false);
  781. return;
  782. }
  783. }
  784. else
  785. {
  786. await UnknownGuildAsync(type, data.GuildId).ConfigureAwait(false);
  787. return;
  788. }
  789. }
  790. break;
  791. case "GUILD_MEMBERS_CHUNK":
  792. {
  793. await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_MEMBERS_CHUNK)").ConfigureAwait(false);
  794. var data = (payload as JToken).ToObject<GuildMembersChunkEvent>(_serializer);
  795. var guild = State.GetGuild(data.GuildId);
  796. if (guild != null)
  797. {
  798. foreach (var memberModel in data.Members)
  799. guild.AddOrUpdateUser(memberModel);
  800. if (guild.DownloadedMemberCount >= guild.MemberCount && !guild.DownloaderPromise.IsCompleted)
  801. {
  802. guild.CompleteDownloadUsers();
  803. await TimedInvokeAsync(_guildMembersDownloadedEvent, nameof(GuildMembersDownloaded), guild).ConfigureAwait(false);
  804. }
  805. }
  806. else
  807. {
  808. await UnknownGuildAsync(type, data.GuildId).ConfigureAwait(false);
  809. return;
  810. }
  811. }
  812. break;
  813. case "CHANNEL_RECIPIENT_ADD":
  814. {
  815. await _gatewayLogger.DebugAsync("Received Dispatch (CHANNEL_RECIPIENT_ADD)").ConfigureAwait(false);
  816. var data = (payload as JToken).ToObject<RecipientEvent>(_serializer);
  817. if (State.GetChannel(data.ChannelId) is SocketGroupChannel channel)
  818. {
  819. var user = channel.GetOrAddUser(data.User);
  820. await TimedInvokeAsync(_recipientAddedEvent, nameof(RecipientAdded), user).ConfigureAwait(false);
  821. }
  822. else
  823. {
  824. await UnknownChannelAsync(type, data.ChannelId).ConfigureAwait(false);
  825. return;
  826. }
  827. }
  828. break;
  829. case "CHANNEL_RECIPIENT_REMOVE":
  830. {
  831. await _gatewayLogger.DebugAsync("Received Dispatch (CHANNEL_RECIPIENT_REMOVE)").ConfigureAwait(false);
  832. var data = (payload as JToken).ToObject<RecipientEvent>(_serializer);
  833. if (State.GetChannel(data.ChannelId) is SocketGroupChannel channel)
  834. {
  835. var user = channel.RemoveUser(data.User.Id);
  836. if (user != null)
  837. await TimedInvokeAsync(_recipientRemovedEvent, nameof(RecipientRemoved), user).ConfigureAwait(false);
  838. else
  839. {
  840. await UnknownChannelUserAsync(type, data.User.Id, data.ChannelId).ConfigureAwait(false);
  841. return;
  842. }
  843. }
  844. else
  845. {
  846. await UnknownChannelAsync(type, data.ChannelId).ConfigureAwait(false);
  847. return;
  848. }
  849. }
  850. break;
  851. //Roles
  852. case "GUILD_ROLE_CREATE":
  853. {
  854. await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_ROLE_CREATE)").ConfigureAwait(false);
  855. var data = (payload as JToken).ToObject<GuildRoleCreateEvent>(_serializer);
  856. var guild = State.GetGuild(data.GuildId);
  857. if (guild != null)
  858. {
  859. var role = guild.AddRole(data.Role);
  860. if (!guild.IsSynced)
  861. {
  862. await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
  863. return;
  864. }
  865. await TimedInvokeAsync(_roleCreatedEvent, nameof(RoleCreated), role).ConfigureAwait(false);
  866. }
  867. else
  868. {
  869. await UnknownGuildAsync(type, data.GuildId).ConfigureAwait(false);
  870. return;
  871. }
  872. }
  873. break;
  874. case "GUILD_ROLE_UPDATE":
  875. {
  876. await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_ROLE_UPDATE)").ConfigureAwait(false);
  877. var data = (payload as JToken).ToObject<GuildRoleUpdateEvent>(_serializer);
  878. var guild = State.GetGuild(data.GuildId);
  879. if (guild != null)
  880. {
  881. var role = guild.GetRole(data.Role.Id);
  882. if (role != null)
  883. {
  884. var before = role.Clone();
  885. role.Update(State, data.Role);
  886. if (!guild.IsSynced)
  887. {
  888. await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
  889. return;
  890. }
  891. await TimedInvokeAsync(_roleUpdatedEvent, nameof(RoleUpdated), before, role).ConfigureAwait(false);
  892. }
  893. else
  894. {
  895. await UnknownRoleAsync(type, data.Role.Id, guild.Id).ConfigureAwait(false);
  896. return;
  897. }
  898. }
  899. else
  900. {
  901. await UnknownGuildAsync(type, data.GuildId).ConfigureAwait(false);
  902. return;
  903. }
  904. }
  905. break;
  906. case "GUILD_ROLE_DELETE":
  907. {
  908. await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_ROLE_DELETE)").ConfigureAwait(false);
  909. var data = (payload as JToken).ToObject<GuildRoleDeleteEvent>(_serializer);
  910. var guild = State.GetGuild(data.GuildId);
  911. if (guild != null)
  912. {
  913. var role = guild.RemoveRole(data.RoleId);
  914. if (role != null)
  915. {
  916. if (!guild.IsSynced)
  917. {
  918. await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
  919. return;
  920. }
  921. await TimedInvokeAsync(_roleDeletedEvent, nameof(RoleDeleted), role).ConfigureAwait(false);
  922. }
  923. else
  924. {
  925. await UnknownRoleAsync(type, data.RoleId, guild.Id).ConfigureAwait(false);
  926. return;
  927. }
  928. }
  929. else
  930. {
  931. await UnknownGuildAsync(type, data.GuildId).ConfigureAwait(false);
  932. return;
  933. }
  934. }
  935. break;
  936. //Bans
  937. case "GUILD_BAN_ADD":
  938. {
  939. await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_BAN_ADD)").ConfigureAwait(false);
  940. var data = (payload as JToken).ToObject<GuildBanEvent>(_serializer);
  941. var guild = State.GetGuild(data.GuildId);
  942. if (guild != null)
  943. {
  944. if (!guild.IsSynced)
  945. {
  946. await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
  947. return;
  948. }
  949. SocketUser user = guild.GetUser(data.User.Id);
  950. if (user == null)
  951. user = SocketUnknownUser.Create(this, State, data.User);
  952. await TimedInvokeAsync(_userBannedEvent, nameof(UserBanned), user, guild).ConfigureAwait(false);
  953. }
  954. else
  955. {
  956. await UnknownGuildAsync(type, data.GuildId).ConfigureAwait(false);
  957. return;
  958. }
  959. }
  960. break;
  961. case "GUILD_BAN_REMOVE":
  962. {
  963. await _gatewayLogger.DebugAsync("Received Dispatch (GUILD_BAN_REMOVE)").ConfigureAwait(false);
  964. var data = (payload as JToken).ToObject<GuildBanEvent>(_serializer);
  965. var guild = State.GetGuild(data.GuildId);
  966. if (guild != null)
  967. {
  968. if (!guild.IsSynced)
  969. {
  970. await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
  971. return;
  972. }
  973. SocketUser user = State.GetUser(data.User.Id);
  974. if (user == null)
  975. user = SocketUnknownUser.Create(this, State, data.User);
  976. await TimedInvokeAsync(_userUnbannedEvent, nameof(UserUnbanned), user, guild).ConfigureAwait(false);
  977. }
  978. else
  979. {
  980. await UnknownGuildAsync(type, data.GuildId).ConfigureAwait(false);
  981. return;
  982. }
  983. }
  984. break;
  985. //Messages
  986. case "MESSAGE_CREATE":
  987. {
  988. await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_CREATE)").ConfigureAwait(false);
  989. var data = (payload as JToken).ToObject<API.Message>(_serializer);
  990. if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
  991. {
  992. var guild = (channel as SocketGuildChannel)?.Guild;
  993. if (guild != null && !guild.IsSynced)
  994. {
  995. await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
  996. return;
  997. }
  998. SocketUser author;
  999. if (guild != null)
  1000. {
  1001. if (data.WebhookId.IsSpecified)
  1002. author = SocketWebhookUser.Create(guild, State, data.Author.Value, data.WebhookId.Value);
  1003. else
  1004. author = guild.GetUser(data.Author.Value.Id);
  1005. }
  1006. else
  1007. author = (channel as SocketChannel).GetUser(data.Author.Value.Id);
  1008. if (author == null)
  1009. {
  1010. if (guild != null)
  1011. author = guild.AddOrUpdateUser(data.Author.Value); //User has no guild-specific data
  1012. else if (channel is SocketGroupChannel)
  1013. author = (channel as SocketGroupChannel).GetOrAddUser(data.Author.Value);
  1014. else
  1015. await UnknownChannelUserAsync(type, data.Author.Value.Id, channel.Id).ConfigureAwait(false);
  1016. return;
  1017. }
  1018. var msg = SocketMessage.Create(this, State, author, channel, data);
  1019. SocketChannelHelper.AddMessage(channel, this, msg);
  1020. await TimedInvokeAsync(_messageReceivedEvent, nameof(MessageReceived), msg).ConfigureAwait(false);
  1021. }
  1022. else
  1023. {
  1024. await UnknownChannelAsync(type, data.ChannelId).ConfigureAwait(false);
  1025. return;
  1026. }
  1027. }
  1028. break;
  1029. case "MESSAGE_UPDATE":
  1030. {
  1031. await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_UPDATE)").ConfigureAwait(false);
  1032. var data = (payload as JToken).ToObject<API.Message>(_serializer);
  1033. if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
  1034. {
  1035. var guild = (channel as SocketGuildChannel)?.Guild;
  1036. if (guild != null && !guild.IsSynced)
  1037. {
  1038. await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
  1039. return;
  1040. }
  1041. SocketMessage before = null, after = null;
  1042. SocketMessage cachedMsg = channel.GetCachedMessage(data.Id);
  1043. bool isCached = cachedMsg != null;
  1044. if (isCached)
  1045. {
  1046. before = cachedMsg.Clone();
  1047. cachedMsg.Update(State, data);
  1048. after = cachedMsg;
  1049. }
  1050. else if (data.Author.IsSpecified)
  1051. {
  1052. //Edited message isnt in cache, create a detached one
  1053. SocketUser author;
  1054. if (guild != null)
  1055. author = guild.GetUser(data.Author.Value.Id);
  1056. else
  1057. author = (channel as SocketChannel).GetUser(data.Author.Value.Id);
  1058. if (author == null)
  1059. author = SocketUnknownUser.Create(this, State, data.Author.Value);
  1060. after = SocketMessage.Create(this, State, author, channel, data);
  1061. }
  1062. var cacheableBefore = new Cacheable<IMessage, ulong>(before, data.Id, isCached, async () => await channel.GetMessageAsync(data.Id));
  1063. await TimedInvokeAsync(_messageUpdatedEvent, nameof(MessageUpdated), cacheableBefore, after, channel).ConfigureAwait(false);
  1064. }
  1065. else
  1066. {
  1067. await UnknownChannelAsync(type, data.ChannelId).ConfigureAwait(false);
  1068. return;
  1069. }
  1070. }
  1071. break;
  1072. case "MESSAGE_DELETE":
  1073. {
  1074. await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_DELETE)").ConfigureAwait(false);
  1075. var data = (payload as JToken).ToObject<API.Message>(_serializer);
  1076. if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
  1077. {
  1078. var guild = (channel as SocketGuildChannel)?.Guild;
  1079. if (!(guild?.IsSynced ?? true))
  1080. {
  1081. await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
  1082. return;
  1083. }
  1084. var msg = SocketChannelHelper.RemoveMessage(channel, this, data.Id);
  1085. bool isCached = msg != null;
  1086. var cacheable = new Cacheable<IMessage, ulong>(msg, data.Id, isCached, async () => await channel.GetMessageAsync(data.Id));
  1087. await TimedInvokeAsync(_messageDeletedEvent, nameof(MessageDeleted), cacheable, channel).ConfigureAwait(false);
  1088. }
  1089. else
  1090. {
  1091. await UnknownChannelAsync(type, data.ChannelId).ConfigureAwait(false);
  1092. return;
  1093. }
  1094. }
  1095. break;
  1096. case "MESSAGE_REACTION_ADD":
  1097. {
  1098. await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_REACTION_ADD)").ConfigureAwait(false);
  1099. var data = (payload as JToken).ToObject<API.Gateway.Reaction>(_serializer);
  1100. if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
  1101. {
  1102. var cachedMsg = channel.GetCachedMessage(data.MessageId) as SocketUserMessage;
  1103. bool isCached = cachedMsg != null;
  1104. var user = await channel.GetUserAsync(data.UserId, CacheMode.CacheOnly);
  1105. var reaction = SocketReaction.Create(data, channel, cachedMsg, Optional.Create(user));
  1106. var cacheable = new Cacheable<IUserMessage, ulong>(cachedMsg, data.MessageId, isCached, async () => await channel.GetMessageAsync(data.MessageId) as IUserMessage);
  1107. cachedMsg?.AddReaction(reaction);
  1108. await TimedInvokeAsync(_reactionAddedEvent, nameof(ReactionAdded), cacheable, channel, reaction).ConfigureAwait(false);
  1109. }
  1110. else
  1111. {
  1112. await UnknownChannelAsync(type, data.ChannelId).ConfigureAwait(false);
  1113. return;
  1114. }
  1115. }
  1116. break;
  1117. case "MESSAGE_REACTION_REMOVE":
  1118. {
  1119. await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_REACTION_REMOVE)").ConfigureAwait(false);
  1120. var data = (payload as JToken).ToObject<API.Gateway.Reaction>(_serializer);
  1121. if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
  1122. {
  1123. var cachedMsg = channel.GetCachedMessage(data.MessageId) as SocketUserMessage;
  1124. bool isCached = cachedMsg != null;
  1125. var user = await channel.GetUserAsync(data.UserId, CacheMode.CacheOnly);
  1126. var reaction = SocketReaction.Create(data, channel, cachedMsg, Optional.Create(user));
  1127. var cacheable = new Cacheable<IUserMessage, ulong>(cachedMsg, data.MessageId, isCached, async () => await channel.GetMessageAsync(data.MessageId) as IUserMessage);
  1128. cachedMsg?.RemoveReaction(reaction);
  1129. await TimedInvokeAsync(_reactionRemovedEvent, nameof(ReactionRemoved), cacheable, channel, reaction).ConfigureAwait(false);
  1130. }
  1131. else
  1132. {
  1133. await UnknownChannelAsync(type, data.ChannelId).ConfigureAwait(false);
  1134. return;
  1135. }
  1136. }
  1137. break;
  1138. case "MESSAGE_REACTION_REMOVE_ALL":
  1139. {
  1140. await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_REACTION_REMOVE_ALL)").ConfigureAwait(false);
  1141. var data = (payload as JToken).ToObject<RemoveAllReactionsEvent>(_serializer);
  1142. if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
  1143. {
  1144. var cachedMsg = channel.GetCachedMessage(data.MessageId) as SocketUserMessage;
  1145. bool isCached = cachedMsg != null;
  1146. var cacheable = new Cacheable<IUserMessage, ulong>(cachedMsg, data.MessageId, isCached, async () => await channel.GetMessageAsync(data.MessageId) as IUserMessage);
  1147. cachedMsg?.ClearReactions();
  1148. await TimedInvokeAsync(_reactionsClearedEvent, nameof(ReactionsCleared), cacheable, channel).ConfigureAwait(false);
  1149. }
  1150. else
  1151. {
  1152. await UnknownChannelAsync(type, data.ChannelId).ConfigureAwait(false);
  1153. return;
  1154. }
  1155. }
  1156. break;
  1157. case "MESSAGE_DELETE_BULK":
  1158. {
  1159. await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_DELETE_BULK)").ConfigureAwait(false);
  1160. var data = (payload as JToken).ToObject<MessageDeleteBulkEvent>(_serializer);
  1161. if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
  1162. {
  1163. var guild = (channel as SocketGuildChannel)?.Guild;
  1164. if (!(guild?.IsSynced ?? true))
  1165. {
  1166. await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
  1167. return;
  1168. }
  1169. foreach (ulong id in data.Ids)
  1170. {
  1171. var msg = SocketChannelHelper.RemoveMessage(channel, this, id);
  1172. bool isCached = msg != null;
  1173. var cacheable = new Cacheable<IMessage, ulong>(msg, id, isCached, async () => await channel.GetMessageAsync(id));
  1174. await TimedInvokeAsync(_messageDeletedEvent, nameof(MessageDeleted), cacheable, channel).ConfigureAwait(false);
  1175. }
  1176. }
  1177. else
  1178. {
  1179. await UnknownChannelAsync(type, data.ChannelId).ConfigureAwait(false);
  1180. return;
  1181. }
  1182. }
  1183. break;
  1184. //Statuses
  1185. case "PRESENCE_UPDATE":
  1186. {
  1187. await _gatewayLogger.DebugAsync("Received Dispatch (PRESENCE_UPDATE)").ConfigureAwait(false);
  1188. var data = (payload as JToken).ToObject<API.Presence>(_serializer);
  1189. if (data.GuildId.IsSpecified)
  1190. {
  1191. var guild = State.GetGuild(data.GuildId.Value);
  1192. if (guild == null)
  1193. {
  1194. await UnknownGuildAsync(type, data.GuildId.Value).ConfigureAwait(false);
  1195. return;
  1196. }
  1197. if (!guild.IsSynced)
  1198. {
  1199. await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
  1200. return;
  1201. }
  1202. var user = guild.GetUser(data.User.Id);
  1203. if (user == null)
  1204. {
  1205. if (data.Status == UserStatus.Offline)
  1206. {
  1207. return;
  1208. }
  1209. user = guild.AddOrUpdateUser(data);
  1210. }
  1211. else
  1212. {
  1213. var globalBefore = user.GlobalUser.Clone();
  1214. if (user.GlobalUser.Update(State, data.User))
  1215. {
  1216. //Global data was updated, trigger UserUpdated
  1217. await TimedInvokeAsync(_userUpdatedEvent, nameof(UserUpdated), globalBefore, user).ConfigureAwait(false);
  1218. }
  1219. }
  1220. var before = user.Clone();
  1221. user.Update(State, data, true);
  1222. await TimedInvokeAsync(_guildMemberUpdatedEvent, nameof(GuildMemberUpdated), before, user).ConfigureAwait(false);
  1223. }
  1224. else
  1225. {
  1226. var globalUser = State.GetUser(data.User.Id);
  1227. if (globalUser == null)
  1228. {
  1229. await UnknownGlobalUserAsync(type, data.User.Id).ConfigureAwait(false);
  1230. return;
  1231. }
  1232. var before = globalUser.Clone();
  1233. globalUser.Update(State, data.User);
  1234. globalUser.Update(State, data);
  1235. await TimedInvokeAsync(_userUpdatedEvent, nameof(UserUpdated), before, globalUser).ConfigureAwait(false);
  1236. }
  1237. }
  1238. break;
  1239. case "TYPING_START":
  1240. {
  1241. await _gatewayLogger.DebugAsync("Received Dispatch (TYPING_START)").ConfigureAwait(false);
  1242. var data = (payload as JToken).ToObject<TypingStartEvent>(_serializer);
  1243. if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel)
  1244. {
  1245. var guild = (channel as SocketGuildChannel)?.Guild;
  1246. if (!(guild?.IsSynced ?? true))
  1247. {
  1248. await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
  1249. return;
  1250. }
  1251. var user = (channel as SocketChannel).GetUser(data.UserId);
  1252. if (user != null)
  1253. await TimedInvokeAsync(_userIsTypingEvent, nameof(UserIsTyping), user, channel).ConfigureAwait(false);
  1254. }
  1255. }
  1256. break;
  1257. //Users
  1258. case "USER_UPDATE":
  1259. {
  1260. await _gatewayLogger.DebugAsync("Received Dispatch (USER_UPDATE)").ConfigureAwait(false);
  1261. var data = (payload as JToken).ToObject<API.User>(_serializer);
  1262. if (data.Id == CurrentUser.Id)
  1263. {
  1264. var before = CurrentUser.Clone();
  1265. CurrentUser.Update(State, data);
  1266. await TimedInvokeAsync(_selfUpdatedEvent, nameof(CurrentUserUpdated), before, CurrentUser).ConfigureAwait(false);
  1267. }
  1268. else
  1269. {
  1270. await _gatewayLogger.WarningAsync("Received USER_UPDATE for wrong user.").ConfigureAwait(false);
  1271. return;
  1272. }
  1273. }
  1274. break;
  1275. //Voice
  1276. case "VOICE_STATE_UPDATE":
  1277. {
  1278. await _gatewayLogger.DebugAsync("Received Dispatch (VOICE_STATE_UPDATE)").ConfigureAwait(false);
  1279. var data = (payload as JToken).ToObject<API.VoiceState>(_serializer);
  1280. SocketUser user;
  1281. SocketVoiceState before, after;
  1282. if (data.GuildId != null)
  1283. {
  1284. var guild = State.GetGuild(data.GuildId.Value);
  1285. if (guild == null)
  1286. {
  1287. await UnknownGuildAsync(type, data.GuildId.Value).ConfigureAwait(false);
  1288. return;
  1289. }
  1290. else if (!guild.IsSynced)
  1291. {
  1292. await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false);
  1293. return;
  1294. }
  1295. if (data.ChannelId != null)
  1296. {
  1297. before = guild.GetVoiceState(data.UserId)?.Clone() ?? SocketVoiceState.Default;
  1298. after = await guild.AddOrUpdateVoiceStateAsync(State, data).ConfigureAwait(false);
  1299. /*if (data.UserId == CurrentUser.Id)
  1300. {
  1301. var _ = guild.FinishJoinAudioChannel().ConfigureAwait(false);
  1302. }*/
  1303. }
  1304. else
  1305. {
  1306. before = await guild.RemoveVoiceStateAsync(data.UserId).ConfigureAwait(false) ?? SocketVoiceState.Default;
  1307. after = SocketVoiceState.Create(null, data);
  1308. }
  1309. user = guild.GetUser(data.UserId);
  1310. if (user == null)
  1311. {
  1312. await UnknownGuildUserAsync(type, data.UserId, guild.Id).ConfigureAwait(false);
  1313. return;
  1314. }
  1315. }
  1316. else
  1317. {
  1318. var groupChannel = State.GetChannel(data.ChannelId.Value) as SocketGroupChannel;
  1319. if (groupChannel == null)
  1320. {
  1321. await UnknownChannelAsync(type, data.ChannelId.Value).ConfigureAwait(false);
  1322. return;
  1323. }
  1324. if (data.ChannelId != null)
  1325. {
  1326. before = groupChannel.GetVoiceState(data.UserId)?.Clone() ?? SocketVoiceState.Default;
  1327. after = groupChannel.AddOrUpdateVoiceState(State, data);
  1328. }
  1329. else
  1330. {
  1331. before = groupChannel.RemoveVoiceState(data.UserId) ?? SocketVoiceState.Default;
  1332. after = SocketVoiceState.Create(null, data);
  1333. }
  1334. user = groupChannel.GetUser(data.UserId);
  1335. if (user == null)
  1336. {
  1337. await UnknownChannelUserAsync(type, data.UserId, groupChannel.Id).ConfigureAwait(false);
  1338. return;
  1339. }
  1340. }
  1341. await TimedInvokeAsync(_userVoiceStateUpdatedEvent, nameof(UserVoiceStateUpdated), user, before, after).ConfigureAwait(false);
  1342. }
  1343. break;
  1344. case "VOICE_SERVER_UPDATE":
  1345. {
  1346. await _gatewayLogger.DebugAsync("Received Dispatch (VOICE_SERVER_UPDATE)").ConfigureAwait(false);
  1347. var data = (payload as JToken).ToObject<VoiceServerUpdateEvent>(_serializer);
  1348. var guild = State.GetGuild(data.GuildId);
  1349. if (guild != null)
  1350. {
  1351. string endpoint = data.Endpoint.Substring(0, data.Endpoint.LastIndexOf(':'));
  1352. var _ = guild.FinishConnectAudio(endpoint, data.Token).ConfigureAwait(false);
  1353. }
  1354. else
  1355. {
  1356. await UnknownGuildAsync(type, data.GuildId).ConfigureAwait(false);
  1357. return;
  1358. }
  1359. }
  1360. break;
  1361. //Ignored (User only)
  1362. case "CHANNEL_PINS_ACK":
  1363. await _gatewayLogger.DebugAsync("Ignored Dispatch (CHANNEL_PINS_ACK)").ConfigureAwait(false);
  1364. break;
  1365. case "CHANNEL_PINS_UPDATE":
  1366. await _gatewayLogger.DebugAsync("Ignored Dispatch (CHANNEL_PINS_UPDATE)").ConfigureAwait(false);
  1367. break;
  1368. case "GUILD_INTEGRATIONS_UPDATE":
  1369. await _gatewayLogger.DebugAsync("Ignored Dispatch (GUILD_INTEGRATIONS_UPDATE)").ConfigureAwait(false);
  1370. break;
  1371. case "MESSAGE_ACK":
  1372. await _gatewayLogger.DebugAsync("Ignored Dispatch (MESSAGE_ACK)").ConfigureAwait(false);
  1373. break;
  1374. case "USER_SETTINGS_UPDATE":
  1375. await _gatewayLogger.DebugAsync("Ignored Dispatch (USER_SETTINGS_UPDATE)").ConfigureAwait(false);
  1376. break;
  1377. case "WEBHOOKS_UPDATE":
  1378. await _gatewayLogger.DebugAsync("Ignored Dispatch (WEBHOOKS_UPDATE)").ConfigureAwait(false);
  1379. break;
  1380. //Others
  1381. default:
  1382. await _gatewayLogger.WarningAsync($"Unknown Dispatch ({type})").ConfigureAwait(false);
  1383. break;
  1384. }
  1385. break;
  1386. default:
  1387. await _gatewayLogger.WarningAsync($"Unknown OpCode ({opCode})").ConfigureAwait(false);
  1388. break;
  1389. }
  1390. }
  1391. catch (Exception ex)
  1392. {
  1393. await _gatewayLogger.ErrorAsync($"Error handling {opCode}{(type != null ? $" ({type})" : "")}", ex).ConfigureAwait(false);
  1394. }
  1395. }
  1396. private async Task RunHeartbeatAsync(int intervalMillis, CancellationToken cancelToken)
  1397. {
  1398. try
  1399. {
  1400. await _gatewayLogger.DebugAsync("Heartbeat Started").ConfigureAwait(false);
  1401. while (!cancelToken.IsCancellationRequested)
  1402. {
  1403. int now = Environment.TickCount;
  1404. //Did server respond to our last heartbeat, or are we still receiving messages (long load?)
  1405. if (_heartbeatTimes.Count != 0 && (now - _lastMessageTime) > intervalMillis)
  1406. {
  1407. if (ConnectionState == ConnectionState.Connected && (_guildDownloadTask?.IsCompleted ?? true))
  1408. {
  1409. _connection.Error(new Exception("Server missed last heartbeat"));
  1410. return;
  1411. }
  1412. }
  1413. _heartbeatTimes.Enqueue(now);
  1414. try
  1415. {
  1416. await ApiClient.SendHeartbeatAsync(_lastSeq).ConfigureAwait(false);
  1417. }
  1418. catch (Exception ex)
  1419. {
  1420. await _gatewayLogger.WarningAsync("Heartbeat Errored", ex).ConfigureAwait(false);
  1421. }
  1422. await Task.Delay(intervalMillis, cancelToken).ConfigureAwait(false);
  1423. }
  1424. await _gatewayLogger.DebugAsync("Heartbeat Stopped").ConfigureAwait(false);
  1425. }
  1426. catch (OperationCanceledException)
  1427. {
  1428. await _gatewayLogger.DebugAsync("Heartbeat Stopped").ConfigureAwait(false);
  1429. }
  1430. catch (Exception ex)
  1431. {
  1432. await _gatewayLogger.ErrorAsync("Heartbeat Errored", ex).ConfigureAwait(false);
  1433. }
  1434. }
  1435. /*public async Task WaitForGuildsAsync()
  1436. {
  1437. var downloadTask = _guildDownloadTask;
  1438. if (downloadTask != null)
  1439. await _guildDownloadTask.ConfigureAwait(false);
  1440. }*/
  1441. private async Task WaitForGuildsAsync(CancellationToken cancelToken, Logger logger)
  1442. {
  1443. //Wait for GUILD_AVAILABLEs
  1444. try
  1445. {
  1446. await logger.DebugAsync("GuildDownloader Started").ConfigureAwait(false);
  1447. while ((_unavailableGuildCount != 0) && (Environment.TickCount - _lastGuildAvailableTime < 2000))
  1448. await Task.Delay(500, cancelToken).ConfigureAwait(false);
  1449. await logger.DebugAsync("GuildDownloader Stopped").ConfigureAwait(false);
  1450. }
  1451. catch (OperationCanceledException)
  1452. {
  1453. await logger.DebugAsync("GuildDownloader Stopped").ConfigureAwait(false);
  1454. }
  1455. catch (Exception ex)
  1456. {
  1457. await logger.ErrorAsync("GuildDownloader Errored", ex).ConfigureAwait(false);
  1458. }
  1459. }
  1460. private async Task SyncGuildsAsync()
  1461. {
  1462. var guildIds = Guilds.Where(x => !x.IsSynced).Select(x => x.Id).ToImmutableArray();
  1463. if (guildIds.Length > 0)
  1464. await ApiClient.SendGuildSyncAsync(guildIds).ConfigureAwait(false);
  1465. }
  1466. internal SocketGuild AddGuild(ExtendedGuild model, ClientState state)
  1467. {
  1468. var guild = SocketGuild.Create(this, state, model);
  1469. state.AddGuild(guild);
  1470. if (model.Large)
  1471. _largeGuilds.Enqueue(model.Id);
  1472. return guild;
  1473. }
  1474. internal SocketGuild RemoveGuild(ulong id)
  1475. {
  1476. var guild = State.RemoveGuild(id);
  1477. if (guild != null)
  1478. {
  1479. foreach (var channel in guild.Channels)
  1480. State.RemoveChannel(id);
  1481. foreach (var user in guild.Users)
  1482. user.GlobalUser.RemoveRef(this);
  1483. }
  1484. return guild;
  1485. }
  1486. internal ISocketPrivateChannel AddPrivateChannel(API.Channel model, ClientState state)
  1487. {
  1488. var channel = SocketChannel.CreatePrivate(this, state, model);
  1489. state.AddChannel(channel as SocketChannel);
  1490. if (channel is SocketDMChannel dm)
  1491. dm.Recipient.GlobalUser.DMChannel = dm;
  1492. return channel;
  1493. }
  1494. internal ISocketPrivateChannel RemovePrivateChannel(ulong id)
  1495. {
  1496. var channel = State.RemoveChannel(id) as ISocketPrivateChannel;
  1497. if (channel != null)
  1498. {
  1499. if (channel is SocketDMChannel dmChannel)
  1500. dmChannel.Recipient.GlobalUser.DMChannel = null;
  1501. foreach (var recipient in channel.Recipients)
  1502. recipient.GlobalUser.RemoveRef(this);
  1503. }
  1504. return channel;
  1505. }
  1506. private async Task GuildAvailableAsync(SocketGuild guild)
  1507. {
  1508. if (!guild.IsConnected)
  1509. {
  1510. guild.IsConnected = true;
  1511. await TimedInvokeAsync(_guildAvailableEvent, nameof(GuildAvailable), guild).ConfigureAwait(false);
  1512. }
  1513. }
  1514. private async Task GuildUnavailableAsync(SocketGuild guild)
  1515. {
  1516. if (guild.IsConnected)
  1517. {
  1518. guild.IsConnected = false;
  1519. await TimedInvokeAsync(_guildUnavailableEvent, nameof(GuildUnavailable), guild).ConfigureAwait(false);
  1520. }
  1521. }
  1522. private async Task TimedInvokeAsync(AsyncEvent<Func<Task>> eventHandler, string name)
  1523. {
  1524. if (eventHandler.HasSubscribers)
  1525. {
  1526. if (HandlerTimeout.HasValue)
  1527. await TimeoutWrap(name, () => eventHandler.InvokeAsync()).ConfigureAwait(false);
  1528. else
  1529. await eventHandler.InvokeAsync().ConfigureAwait(false);
  1530. }
  1531. }
  1532. private async Task TimedInvokeAsync<T>(AsyncEvent<Func<T, Task>> eventHandler, string name, T arg)
  1533. {
  1534. if (eventHandler.HasSubscribers)
  1535. {
  1536. if (HandlerTimeout.HasValue)
  1537. await TimeoutWrap(name, () => eventHandler.InvokeAsync(arg)).ConfigureAwait(false);
  1538. else
  1539. await eventHandler.InvokeAsync(arg).ConfigureAwait(false);
  1540. }
  1541. }
  1542. private async Task TimedInvokeAsync<T1, T2>(AsyncEvent<Func<T1, T2, Task>> eventHandler, string name, T1 arg1, T2 arg2)
  1543. {
  1544. if (eventHandler.HasSubscribers)
  1545. {
  1546. if (HandlerTimeout.HasValue)
  1547. await TimeoutWrap(name, () => eventHandler.InvokeAsync(arg1, arg2)).ConfigureAwait(false);
  1548. else
  1549. await eventHandler.InvokeAsync(arg1, arg2).ConfigureAwait(false);
  1550. }
  1551. }
  1552. private async Task TimedInvokeAsync<T1, T2, T3>(AsyncEvent<Func<T1, T2, T3, Task>> eventHandler, string name, T1 arg1, T2 arg2, T3 arg3)
  1553. {
  1554. if (eventHandler.HasSubscribers)
  1555. {
  1556. if (HandlerTimeout.HasValue)
  1557. await TimeoutWrap(name, () => eventHandler.InvokeAsync(arg1, arg2, arg3)).ConfigureAwait(false);
  1558. else
  1559. await eventHandler.InvokeAsync(arg1, arg2, arg3).ConfigureAwait(false);
  1560. }
  1561. }
  1562. private async Task TimedInvokeAsync<T1, T2, T3, T4>(AsyncEvent<Func<T1, T2, T3, T4, Task>> eventHandler, string name, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
  1563. {
  1564. if (eventHandler.HasSubscribers)
  1565. {
  1566. if (HandlerTimeout.HasValue)
  1567. await TimeoutWrap(name, () => eventHandler.InvokeAsync(arg1, arg2, arg3, arg4)).ConfigureAwait(false);
  1568. else
  1569. await eventHandler.InvokeAsync(arg1, arg2, arg3, arg4).ConfigureAwait(false);
  1570. }
  1571. }
  1572. private async Task TimedInvokeAsync<T1, T2, T3, T4, T5>(AsyncEvent<Func<T1, T2, T3, T4, T5, Task>> eventHandler, string name, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
  1573. {
  1574. if (eventHandler.HasSubscribers)
  1575. {
  1576. if (HandlerTimeout.HasValue)
  1577. await TimeoutWrap(name, () => eventHandler.InvokeAsync(arg1, arg2, arg3, arg4, arg5)).ConfigureAwait(false);
  1578. else
  1579. await eventHandler.InvokeAsync(arg1, arg2, arg3, arg4, arg5).ConfigureAwait(false);
  1580. }
  1581. }
  1582. private async Task TimeoutWrap(string name, Func<Task> action)
  1583. {
  1584. try
  1585. {
  1586. var timeoutTask = Task.Delay(HandlerTimeout.Value);
  1587. var handlersTask = action();
  1588. if (await Task.WhenAny(timeoutTask, handlersTask).ConfigureAwait(false) == timeoutTask)
  1589. {
  1590. await _gatewayLogger.WarningAsync($"A {name} handler is blocking the gateway task.").ConfigureAwait(false);
  1591. await handlersTask.ConfigureAwait(false); //Ensure the handler completes
  1592. }
  1593. }
  1594. catch (Exception ex)
  1595. {
  1596. await _gatewayLogger.WarningAsync($"A {name} handler has thrown an unhandled exception.", ex).ConfigureAwait(false);
  1597. }
  1598. }
  1599. private async Task UnknownGlobalUserAsync(string evnt, ulong userId)
  1600. {
  1601. string details = $"{evnt} User={userId}";
  1602. await _gatewayLogger.WarningAsync($"Unknown User ({details}).").ConfigureAwait(false);
  1603. }
  1604. private async Task UnknownChannelUserAsync(string evnt, ulong userId, ulong channelId)
  1605. {
  1606. string details = $"{evnt} User={userId} Channel={channelId}";
  1607. await _gatewayLogger.WarningAsync($"Unknown User ({details}).").ConfigureAwait(false);
  1608. }
  1609. private async Task UnknownGuildUserAsync(string evnt, ulong userId, ulong guildId)
  1610. {
  1611. string details = $"{evnt} User={userId} Guild={guildId}";
  1612. await _gatewayLogger.WarningAsync($"Unknown User ({details}).").ConfigureAwait(false);
  1613. }
  1614. private async Task IncompleteGuildUserAsync(string evnt, ulong userId, ulong guildId)
  1615. {
  1616. string details = $"{evnt} User={userId} Guild={guildId}";
  1617. await _gatewayLogger.DebugAsync($"User has not been downloaded ({details}).").ConfigureAwait(false);
  1618. }
  1619. private async Task UnknownChannelAsync(string evnt, ulong channelId)
  1620. {
  1621. string details = $"{evnt} Channel={channelId}";
  1622. await _gatewayLogger.WarningAsync($"Unknown Channel ({details}).").ConfigureAwait(false);
  1623. }
  1624. private async Task UnknownChannelAsync(string evnt, ulong channelId, ulong guildId)
  1625. {
  1626. if (guildId == 0)
  1627. {
  1628. await UnknownChannelAsync(evnt, channelId).ConfigureAwait(false);
  1629. return;
  1630. }
  1631. string details = $"{evnt} Channel={channelId} Guild={guildId}";
  1632. await _gatewayLogger.WarningAsync($"Unknown Channel ({details}).").ConfigureAwait(false);
  1633. }
  1634. private async Task UnknownRoleAsync(string evnt, ulong roleId, ulong guildId)
  1635. {
  1636. string details = $"{evnt} Role={roleId} Guild={guildId}";
  1637. await _gatewayLogger.WarningAsync($"Unknown Role ({details}).").ConfigureAwait(false);
  1638. }
  1639. private async Task UnknownGuildAsync(string evnt, ulong guildId)
  1640. {
  1641. string details = $"{evnt} Guild={guildId}";
  1642. await _gatewayLogger.WarningAsync($"Unknown Guild ({details}).").ConfigureAwait(false);
  1643. }
  1644. private async Task UnsyncedGuildAsync(string evnt, ulong guildId)
  1645. {
  1646. string details = $"{evnt} Guild={guildId}";
  1647. await _gatewayLogger.DebugAsync($"Unsynced Guild ({details}).").ConfigureAwait(false);
  1648. }
  1649. internal int GetAudioId() => _nextAudioId++;
  1650. //IDiscordClient
  1651. async Task<IApplication> IDiscordClient.GetApplicationInfoAsync(RequestOptions options)
  1652. => await GetApplicationInfoAsync().ConfigureAwait(false);
  1653. Task<IChannel> IDiscordClient.GetChannelAsync(ulong id, CacheMode mode, RequestOptions options)
  1654. => Task.FromResult<IChannel>(GetChannel(id));
  1655. Task<IReadOnlyCollection<IPrivateChannel>> IDiscordClient.GetPrivateChannelsAsync(CacheMode mode, RequestOptions options)
  1656. => Task.FromResult<IReadOnlyCollection<IPrivateChannel>>(PrivateChannels);
  1657. Task<IReadOnlyCollection<IDMChannel>> IDiscordClient.GetDMChannelsAsync(CacheMode mode, RequestOptions options)
  1658. => Task.FromResult<IReadOnlyCollection<IDMChannel>>(DMChannels);
  1659. Task<IReadOnlyCollection<IGroupChannel>> IDiscordClient.GetGroupChannelsAsync(CacheMode mode, RequestOptions options)
  1660. => Task.FromResult<IReadOnlyCollection<IGroupChannel>>(GroupChannels);
  1661. async Task<IReadOnlyCollection<IConnection>> IDiscordClient.GetConnectionsAsync(RequestOptions options)
  1662. => await GetConnectionsAsync().ConfigureAwait(false);
  1663. async Task<IInvite> IDiscordClient.GetInviteAsync(string inviteId, RequestOptions options)
  1664. => await GetInviteAsync(inviteId).ConfigureAwait(false);
  1665. Task<IGuild> IDiscordClient.GetGuildAsync(ulong id, CacheMode mode, RequestOptions options)
  1666. => Task.FromResult<IGuild>(GetGuild(id));
  1667. Task<IReadOnlyCollection<IGuild>> IDiscordClient.GetGuildsAsync(CacheMode mode, RequestOptions options)
  1668. => Task.FromResult<IReadOnlyCollection<IGuild>>(Guilds);
  1669. async Task<IGuild> IDiscordClient.CreateGuildAsync(string name, IVoiceRegion region, Stream jpegIcon, RequestOptions options)
  1670. => await CreateGuildAsync(name, region, jpegIcon).ConfigureAwait(false);
  1671. Task<IUser> IDiscordClient.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  1672. => Task.FromResult<IUser>(GetUser(id));
  1673. Task<IUser> IDiscordClient.GetUserAsync(string username, string discriminator, RequestOptions options)
  1674. => Task.FromResult<IUser>(GetUser(username, discriminator));
  1675. Task<IReadOnlyCollection<IVoiceRegion>> IDiscordClient.GetVoiceRegionsAsync(RequestOptions options)
  1676. => Task.FromResult<IReadOnlyCollection<IVoiceRegion>>(VoiceRegions);
  1677. Task<IVoiceRegion> IDiscordClient.GetVoiceRegionAsync(string id, RequestOptions options)
  1678. => Task.FromResult<IVoiceRegion>(GetVoiceRegion(id));
  1679. async Task IDiscordClient.StartAsync()
  1680. => await StartAsync().ConfigureAwait(false);
  1681. async Task IDiscordClient.StopAsync()
  1682. => await StopAsync().ConfigureAwait(false);
  1683. }
  1684. }