diff --git a/src/Discord.Net.Core/Entities/Messages/Emoji.cs b/src/Discord.Net.Core/Entities/Messages/Emoji.cs index f0a0489e2..a9c5a6bbd 100644 --- a/src/Discord.Net.Core/Entities/Messages/Emoji.cs +++ b/src/Discord.Net.Core/Entities/Messages/Emoji.cs @@ -20,8 +20,7 @@ namespace Discord public static Emoji Parse(string text) { - Emoji result; - if (TryParse(text, out result)) + if (TryParse(text, out Emoji result)) return result; throw new ArgumentException("Invalid emoji format", nameof(text)); } @@ -35,8 +34,7 @@ namespace Discord if (splitIndex == -1) return false; - ulong id; - if (!ulong.TryParse(text.Substring(splitIndex + 1, text.Length - splitIndex - 2), NumberStyles.None, CultureInfo.InvariantCulture, out id)) + if (!ulong.TryParse(text.Substring(splitIndex + 1, text.Length - splitIndex - 2), NumberStyles.None, CultureInfo.InvariantCulture, out ulong id)) return false; string name = text.Substring(2, splitIndex - 2); diff --git a/src/Discord.Net.Core/Utils/ConcurrentHashSet.cs b/src/Discord.Net.Core/Utils/ConcurrentHashSet.cs index 1ef105527..1fc11587e 100644 --- a/src/Discord.Net.Core/Utils/ConcurrentHashSet.cs +++ b/src/Discord.Net.Core/Utils/ConcurrentHashSet.cs @@ -239,10 +239,8 @@ namespace Discord { while (true) { - int bucketNo, lockNo; - Tables tables = _tables; - GetBucketAndLockNo(hashcode, out bucketNo, out lockNo, tables._buckets.Length, tables._locks.Length); + GetBucketAndLockNo(hashcode, out int bucketNo, out int lockNo, tables._buckets.Length, tables._locks.Length); bool resizeDesired = false; bool lockTaken = false; @@ -292,9 +290,7 @@ namespace Discord while (true) { Tables tables = _tables; - - int bucketNo, lockNo; - GetBucketAndLockNo(hashcode, out bucketNo, out lockNo, tables._buckets.Length, tables._locks.Length); + GetBucketAndLockNo(hashcode, out int bucketNo, out int lockNo, tables._buckets.Length, tables._locks.Length); lock (tables._locks[lockNo]) { @@ -426,8 +422,7 @@ namespace Discord while (current != null) { Node next = current._next; - int newBucketNo, newLockNo; - GetBucketAndLockNo(current._hashcode, out newBucketNo, out newLockNo, newBuckets.Length, newLocks.Length); + GetBucketAndLockNo(current._hashcode, out int newBucketNo, out int newLockNo, newBuckets.Length, newLocks.Length); newBuckets[newBucketNo] = new Node(current._value, current._hashcode, newBuckets[newBucketNo]); diff --git a/src/Discord.Net.Core/Utils/MentionUtils.cs b/src/Discord.Net.Core/Utils/MentionUtils.cs index 60e065b62..2af254fde 100644 --- a/src/Discord.Net.Core/Utils/MentionUtils.cs +++ b/src/Discord.Net.Core/Utils/MentionUtils.cs @@ -19,8 +19,7 @@ namespace Discord /// Parses a provided user mention string. public static ulong ParseUser(string text) { - ulong id; - if (TryParseUser(text, out id)) + if (TryParseUser(text, out ulong id)) return id; throw new ArgumentException("Invalid mention format", nameof(text)); } @@ -44,8 +43,7 @@ namespace Discord /// Parses a provided channel mention string. public static ulong ParseChannel(string text) { - ulong id; - if (TryParseChannel(text, out id)) + if (TryParseChannel(text, out ulong id)) return id; throw new ArgumentException("Invalid mention format", nameof(text)); } @@ -66,8 +64,7 @@ namespace Discord /// Parses a provided role mention string. public static ulong ParseRole(string text) { - ulong id; - if (TryParseRole(text, out id)) + if (TryParseRole(text, out ulong id)) return id; throw new ArgumentException("Invalid mention format", nameof(text)); } diff --git a/src/Discord.Net.Rest/Entities/Channels/RestGroupChannel.cs b/src/Discord.Net.Rest/Entities/Channels/RestGroupChannel.cs index e3ba4e94b..93e24e05b 100644 --- a/src/Discord.Net.Rest/Entities/Channels/RestGroupChannel.cs +++ b/src/Discord.Net.Rest/Entities/Channels/RestGroupChannel.cs @@ -60,8 +60,7 @@ namespace Discord.Rest public RestUser GetUser(ulong id) { - RestGroupUser user; - if (_users.TryGetValue(id, out user)) + if (_users.TryGetValue(id, out RestGroupUser user)) return user; return null; } diff --git a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs index eff178db6..5a47fce81 100644 --- a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs +++ b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs @@ -213,8 +213,7 @@ namespace Discord.Rest //Roles public RestRole GetRole(ulong id) { - RestRole value; - if (_roles.TryGetValue(id, out value)) + if (_roles.TryGetValue(id, out RestRole value)) return value; return null; } diff --git a/src/Discord.Net.Rest/Entities/Invites/RestInvite.cs b/src/Discord.Net.Rest/Entities/Invites/RestInvite.cs index 9e2249bff..900d1f0ac 100644 --- a/src/Discord.Net.Rest/Entities/Invites/RestInvite.cs +++ b/src/Discord.Net.Rest/Entities/Invites/RestInvite.cs @@ -58,8 +58,7 @@ namespace Discord.Rest { if (Guild != null) return Guild; - var guildChannel = Channel as IGuildChannel; - if (guildChannel != null) + if (Channel is IGuildChannel guildChannel) return guildChannel.Guild; //If it fails, it'll still return this exception throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object."); } diff --git a/src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs b/src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs index 367a33be6..285cf0e74 100644 --- a/src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs +++ b/src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs @@ -80,8 +80,7 @@ namespace Discord.Rest if (endIndex == -1) break; string content = text.Substring(index, endIndex - index + 1); - ulong id; - if (MentionUtils.TryParseUser(content, out id)) + if (MentionUtils.TryParseUser(content, out ulong id)) { IUser mentionedUser = null; foreach (var mention in userMentions) diff --git a/src/Discord.Net.Rest/Net/Converters/DiscordContractResolver.cs b/src/Discord.Net.Rest/Net/Converters/DiscordContractResolver.cs index 104b913da..b465fbed2 100644 --- a/src/Discord.Net.Rest/Net/Converters/DiscordContractResolver.cs +++ b/src/Discord.Net.Rest/Net/Converters/DiscordContractResolver.cs @@ -19,8 +19,7 @@ namespace Discord.Net.Converters if (property.Ignored) return property; - var propInfo = member as PropertyInfo; - if (propInfo != null) + if (member is PropertyInfo propInfo) { var converter = GetConverter(property, propInfo, propInfo.PropertyType, 0); if (converter != null) diff --git a/src/Discord.Net.Rest/Net/Queue/RequestQueue.cs b/src/Discord.Net.Rest/Net/Queue/RequestQueue.cs index fce7e3e1b..943b76359 100644 --- a/src/Discord.Net.Rest/Net/Queue/RequestQueue.cs +++ b/src/Discord.Net.Rest/Net/Queue/RequestQueue.cs @@ -114,9 +114,8 @@ namespace Discord.Net.Queue var now = DateTimeOffset.UtcNow; foreach (var bucket in _buckets.Select(x => x.Value)) { - RequestBucket ignored; if ((now - bucket.LastAttemptAt).TotalMinutes > 1.0) - _buckets.TryRemove(bucket.Id, out ignored); + _buckets.TryRemove(bucket.Id, out RequestBucket ignored); } await Task.Delay(60000, _cancelToken.Token); //Runs each minute } diff --git a/src/Discord.Net.Rest/Net/RateLimitInfo.cs b/src/Discord.Net.Rest/Net/RateLimitInfo.cs index 79fe47dd1..9421221ed 100644 --- a/src/Discord.Net.Rest/Net/RateLimitInfo.cs +++ b/src/Discord.Net.Rest/Net/RateLimitInfo.cs @@ -14,9 +14,8 @@ namespace Discord.Net internal RateLimitInfo(Dictionary headers) { - string temp; - IsGlobal = headers.TryGetValue("X-RateLimit-Global", out temp) && - bool.TryParse(temp, out var isGlobal) ? isGlobal : false; + IsGlobal = headers.TryGetValue("X-RateLimit-Global", out string temp) && + bool.TryParse(temp, out var isGlobal) ? isGlobal : false; Limit = headers.TryGetValue("X-RateLimit-Limit", out temp) && int.TryParse(temp, out var limit) ? limit : (int?)null; Remaining = headers.TryGetValue("X-RateLimit-Remaining", out temp) && diff --git a/src/Discord.Net.Rpc/DiscordRpcApiClient.cs b/src/Discord.Net.Rpc/DiscordRpcApiClient.cs index 8c83d24d6..50d467054 100644 --- a/src/Discord.Net.Rpc/DiscordRpcApiClient.cs +++ b/src/Discord.Net.Rpc/DiscordRpcApiClient.cs @@ -378,8 +378,7 @@ namespace Discord.API private bool ProcessMessage(API.Rpc.RpcFrame msg) { - RpcRequest requestTracker; - if (_requests.TryGetValue(msg.Nonce.Value.Value, out requestTracker)) + if (_requests.TryGetValue(msg.Nonce.Value.Value, out RpcRequest requestTracker)) { if (msg.Event.GetValueOrDefault("") == "ERROR") { diff --git a/src/Discord.Net.WebSocket/Audio/AudioClient.cs b/src/Discord.Net.WebSocket/Audio/AudioClient.cs index 405ff394e..19639a418 100644 --- a/src/Discord.Net.WebSocket/Audio/AudioClient.cs +++ b/src/Discord.Net.WebSocket/Audio/AudioClient.cs @@ -131,8 +131,7 @@ namespace Discord.Audio await keepaliveTask.ConfigureAwait(false); _keepaliveTask = null; - long time; - while (_heartbeatTimes.TryDequeue(out time)) { } + while (_heartbeatTimes.TryDequeue(out long time)) { } _lastMessageTime = 0; await ClearInputStreamsAsync().ConfigureAwait(false); @@ -186,8 +185,7 @@ namespace Discord.Audio } internal AudioInStream GetInputStream(ulong id) { - StreamPair streamPair; - if (_streams.TryGetValue(id, out streamPair)) + if (_streams.TryGetValue(id, out StreamPair streamPair)) return streamPair.Reader; return null; } @@ -254,8 +252,7 @@ namespace Discord.Audio { await _audioLogger.DebugAsync("Received HeartbeatAck").ConfigureAwait(false); - long time; - if (_heartbeatTimes.TryDequeue(out time)) + if (_heartbeatTimes.TryDequeue(out long time)) { int latency = (int)(Environment.TickCount - time); int before = Latency; diff --git a/src/Discord.Net.WebSocket/Audio/Streams/BufferedWriteStream.cs b/src/Discord.Net.WebSocket/Audio/Streams/BufferedWriteStream.cs index e73eb2cc2..29586389c 100644 --- a/src/Discord.Net.WebSocket/Audio/Streams/BufferedWriteStream.cs +++ b/src/Discord.Net.WebSocket/Audio/Streams/BufferedWriteStream.cs @@ -85,8 +85,7 @@ namespace Discord.Audio.Streams long dist = nextTick - tick; if (dist <= 0) { - Frame frame; - if (_queuedFrames.TryDequeue(out frame)) + if (_queuedFrames.TryDequeue(out Frame frame)) { await _client.SetSpeakingAsync(true).ConfigureAwait(false); _next.WriteHeader(seq++, timestamp, false); @@ -100,7 +99,7 @@ namespace Discord.Audio.Streams var _ = _logger?.DebugAsync($"Sent {frame.Bytes} bytes ({_queuedFrames.Count} frames buffered)"); #endif } - else + else { while ((nextTick - tick) <= 0) { @@ -135,8 +134,7 @@ namespace Discord.Audio.Streams cancelToken = _cancelToken; await _queueLock.WaitAsync(-1, cancelToken).ConfigureAwait(false); - byte[] buffer; - if (!_bufferPool.TryDequeue(out buffer)) + if (!_bufferPool.TryDequeue(out byte[] buffer)) { #if DEBUG var _ = _logger?.DebugAsync($"Buffer overflow"); //Should never happen because of the queueLock @@ -166,10 +164,9 @@ namespace Discord.Audio.Streams } public override Task ClearAsync(CancellationToken cancelToken) { - Frame ignored; do cancelToken.ThrowIfCancellationRequested(); - while (_queuedFrames.TryDequeue(out ignored)); + while (_queuedFrames.TryDequeue(out Frame ignored)); return Task.Delay(0); } } diff --git a/src/Discord.Net.WebSocket/Audio/Streams/InputStream.cs b/src/Discord.Net.WebSocket/Audio/Streams/InputStream.cs index a46b6d3d2..b9d6157ea 100644 --- a/src/Discord.Net.WebSocket/Audio/Streams/InputStream.cs +++ b/src/Discord.Net.WebSocket/Audio/Streams/InputStream.cs @@ -55,9 +55,8 @@ namespace Discord.Audio.Streams { cancelToken.ThrowIfCancellationRequested(); - RTPFrame frame; await _signal.WaitAsync(cancelToken).ConfigureAwait(false); - _frames.TryDequeue(out frame); + _frames.TryDequeue(out RTPFrame frame); return frame; } diff --git a/src/Discord.Net.WebSocket/Audio/Streams/JitterBuffer.cs b/src/Discord.Net.WebSocket/Audio/Streams/JitterBuffer.cs index 2038e605a..a5ecdea6f 100644 --- a/src/Discord.Net.WebSocket/Audio/Streams/JitterBuffer.cs +++ b/src/Discord.Net.WebSocket/Audio/Streams/JitterBuffer.cs @@ -94,8 +94,7 @@ namespace Discord.Audio.Streams continue; } - Frame frame; - if (_queuedFrames.TryPeek(out frame)) + if (_queuedFrames.TryPeek(out Frame frame)) { silenceFrames = 0; uint distance = (uint)(frame.Timestamp - _timestamp); @@ -201,7 +200,6 @@ namespace Discord.Audio.Streams return; //This is an old frame, ignore } - byte[] buffer; if (!await _queueLock.WaitAsync(0).ConfigureAwait(false)) { #if DEBUG @@ -209,7 +207,7 @@ namespace Discord.Audio.Streams #endif return; } - _bufferPool.TryDequeue(out buffer); + _bufferPool.TryDequeue(out byte[] buffer); Buffer.BlockCopy(data, offset, buffer, 0, count); #if DEBUG @@ -239,10 +237,9 @@ namespace Discord.Audio.Streams } public override Task ClearAsync(CancellationToken cancelToken) { - Frame ignored; do cancelToken.ThrowIfCancellationRequested(); - while (_queuedFrames.TryDequeue(out ignored)); + while (_queuedFrames.TryDequeue(out Frame ignored)); return Task.Delay(0); } } diff --git a/src/Discord.Net.WebSocket/ClientState.cs b/src/Discord.Net.WebSocket/ClientState.cs index a452113e2..8bc745211 100644 --- a/src/Discord.Net.WebSocket/ClientState.cs +++ b/src/Discord.Net.WebSocket/ClientState.cs @@ -41,15 +41,13 @@ namespace Discord.WebSocket internal SocketChannel GetChannel(ulong id) { - SocketChannel channel; - if (_channels.TryGetValue(id, out channel)) + if (_channels.TryGetValue(id, out SocketChannel channel)) return channel; return null; } internal SocketDMChannel GetDMChannel(ulong userId) { - SocketDMChannel channel; - if (_dmChannels.TryGetValue(userId, out channel)) + if (_dmChannels.TryGetValue(userId, out SocketDMChannel channel)) return channel; return null; } @@ -57,31 +55,25 @@ namespace Discord.WebSocket { _channels[channel.Id] = channel; - var dmChannel = channel as SocketDMChannel; - if (dmChannel != null) + if (channel is SocketDMChannel dmChannel) _dmChannels[dmChannel.Recipient.Id] = dmChannel; else { - var groupChannel = channel as SocketGroupChannel; - if (groupChannel != null) + if (channel is SocketGroupChannel groupChannel) _groupChannels.TryAdd(groupChannel.Id); } } internal SocketChannel RemoveChannel(ulong id) { - SocketChannel channel; - if (_channels.TryRemove(id, out channel)) + if (_channels.TryRemove(id, out SocketChannel channel)) { - var dmChannel = channel as SocketDMChannel; - if (dmChannel != null) + if (channel is SocketDMChannel dmChannel) { - SocketDMChannel ignored; - _dmChannels.TryRemove(dmChannel.Recipient.Id, out ignored); + _dmChannels.TryRemove(dmChannel.Recipient.Id, out SocketDMChannel ignored); } else { - var groupChannel = channel as SocketGroupChannel; - if (groupChannel != null) + if (channel is SocketGroupChannel groupChannel) _groupChannels.TryRemove(id); } return channel; @@ -91,8 +83,7 @@ namespace Discord.WebSocket internal SocketGuild GetGuild(ulong id) { - SocketGuild guild; - if (_guilds.TryGetValue(id, out guild)) + if (_guilds.TryGetValue(id, out SocketGuild guild)) return guild; return null; } @@ -102,16 +93,14 @@ namespace Discord.WebSocket } internal SocketGuild RemoveGuild(ulong id) { - SocketGuild guild; - if (_guilds.TryRemove(id, out guild)) + if (_guilds.TryRemove(id, out SocketGuild guild)) return guild; return null; } internal SocketGlobalUser GetUser(ulong id) { - SocketGlobalUser user; - if (_users.TryGetValue(id, out user)) + if (_users.TryGetValue(id, out SocketGlobalUser user)) return user; return null; } @@ -121,8 +110,7 @@ namespace Discord.WebSocket } internal SocketGlobalUser RemoveUser(ulong id) { - SocketGlobalUser user; - if (_users.TryRemove(id, out user)) + if (_users.TryRemove(id, out SocketGlobalUser user)) return user; return null; } diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index 96eccacfe..4d79ff964 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -197,7 +197,6 @@ namespace Discord.WebSocket } private async Task OnDisconnectingAsync(Exception ex) { - ulong guildId; await _gatewayLogger.DebugAsync("Disconnecting ApiClient").ConfigureAwait(false); await ApiClient.DisconnectAsync().ConfigureAwait(false); @@ -209,8 +208,7 @@ namespace Discord.WebSocket await heartbeatTask.ConfigureAwait(false); _heartbeatTask = null; - long time; - while (_heartbeatTimes.TryDequeue(out time)) { } + while (_heartbeatTimes.TryDequeue(out long time)) { } _lastMessageTime = 0; await _gatewayLogger.DebugAsync("Waiting for guild downloader").ConfigureAwait(false); @@ -221,7 +219,7 @@ namespace Discord.WebSocket //Clear large guild queue await _gatewayLogger.DebugAsync("Clearing large guild queue").ConfigureAwait(false); - while (_largeGuilds.TryDequeue(out guildId)) { } + while (_largeGuilds.TryDequeue(out ulong guildId)) { } //Raise virtual GUILD_UNAVAILABLEs await _gatewayLogger.DebugAsync("Raising virtual GuildUnavailables").ConfigureAwait(false); @@ -298,8 +296,7 @@ namespace Discord.WebSocket /// public RestVoiceRegion GetVoiceRegion(string id) { - RestVoiceRegion region; - if (_voiceRegions.TryGetValue(id, out region)) + if (_voiceRegions.TryGetValue(id, out RestVoiceRegion region)) return region; return null; } @@ -417,8 +414,7 @@ namespace Discord.WebSocket { await _gatewayLogger.DebugAsync("Received HeartbeatAck").ConfigureAwait(false); - long time; - if (_heartbeatTimes.TryDequeue(out time)) + if (_heartbeatTimes.TryDequeue(out long time)) { int latency = (int)(Environment.TickCount - time); int before = Latency; @@ -903,8 +899,7 @@ namespace Discord.WebSocket await _gatewayLogger.DebugAsync("Received Dispatch (CHANNEL_RECIPIENT_ADD)").ConfigureAwait(false); var data = (payload as JToken).ToObject(_serializer); - var channel = State.GetChannel(data.ChannelId) as SocketGroupChannel; - if (channel != null) + if (State.GetChannel(data.ChannelId) is SocketGroupChannel channel) { var user = channel.GetOrAddUser(data.User); await TimedInvokeAsync(_recipientAddedEvent, nameof(RecipientAdded), user).ConfigureAwait(false); @@ -921,8 +916,7 @@ namespace Discord.WebSocket await _gatewayLogger.DebugAsync("Received Dispatch (CHANNEL_RECIPIENT_REMOVE)").ConfigureAwait(false); var data = (payload as JToken).ToObject(_serializer); - var channel = State.GetChannel(data.ChannelId) as SocketGroupChannel; - if (channel != null) + if (State.GetChannel(data.ChannelId) is SocketGroupChannel channel) { var user = channel.RemoveUser(data.User.Id); if (user != null) @@ -1094,12 +1088,11 @@ namespace Discord.WebSocket await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_CREATE)").ConfigureAwait(false); var data = (payload as JToken).ToObject(_serializer); - var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel; - if (channel != null) + if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) { var guild = (channel as SocketGuildChannel)?.Guild; if (guild != null && !guild.IsSynced) - { + { await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false); return; } @@ -1142,8 +1135,7 @@ namespace Discord.WebSocket await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_UPDATE)").ConfigureAwait(false); var data = (payload as JToken).ToObject(_serializer); - var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel; - if (channel != null) + if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) { var guild = (channel as SocketGuildChannel)?.Guild; if (guild != null && !guild.IsSynced) @@ -1174,9 +1166,9 @@ namespace Discord.WebSocket after = SocketMessage.Create(this, State, author, channel, data); } - var cacheableBefore = new Cacheable(before, data.Id, isCached , async () => await channel.GetMessageAsync(data.Id)); + var cacheableBefore = new Cacheable(before, data.Id, isCached, async () => await channel.GetMessageAsync(data.Id)); - await TimedInvokeAsync(_messageUpdatedEvent, nameof(MessageUpdated), cacheableBefore, after, channel).ConfigureAwait(false); + await TimedInvokeAsync(_messageUpdatedEvent, nameof(MessageUpdated), cacheableBefore, after, channel).ConfigureAwait(false); } else { @@ -1190,8 +1182,7 @@ namespace Discord.WebSocket await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_DELETE)").ConfigureAwait(false); var data = (payload as JToken).ToObject(_serializer); - var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel; - if (channel != null) + if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) { var guild = (channel as SocketGuildChannel)?.Guild; if (!(guild?.IsSynced ?? true)) @@ -1218,8 +1209,7 @@ namespace Discord.WebSocket await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_REACTION_ADD)").ConfigureAwait(false); var data = (payload as JToken).ToObject(_serializer); - var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel; - if (channel != null) + if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) { SocketUserMessage cachedMsg = channel.GetCachedMessage(data.MessageId) as SocketUserMessage; bool isCached = cachedMsg != null; @@ -1243,8 +1233,7 @@ namespace Discord.WebSocket await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_REACTION_REMOVE)").ConfigureAwait(false); var data = (payload as JToken).ToObject(_serializer); - var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel; - if (channel != null) + if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) { SocketUserMessage cachedMsg = channel.GetCachedMessage(data.MessageId) as SocketUserMessage; bool isCached = cachedMsg != null; @@ -1268,8 +1257,7 @@ namespace Discord.WebSocket await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_REACTION_REMOVE_ALL)").ConfigureAwait(false); var data = (payload as JToken).ToObject(_serializer); - var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel; - if (channel != null) + if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) { SocketUserMessage cachedMsg = channel.GetCachedMessage(data.MessageId) as SocketUserMessage; bool isCached = cachedMsg != null; @@ -1291,8 +1279,7 @@ namespace Discord.WebSocket await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_DELETE_BULK)").ConfigureAwait(false); var data = (payload as JToken).ToObject(_serializer); - var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel; - if (channel != null) + if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) { var guild = (channel as SocketGuildChannel)?.Guild; if (!(guild?.IsSynced ?? true)) @@ -1376,8 +1363,7 @@ namespace Discord.WebSocket await _gatewayLogger.DebugAsync("Received Dispatch (TYPING_START)").ConfigureAwait(false); var data = (payload as JToken).ToObject(_serializer); - var channel = State.GetChannel(data.ChannelId) as ISocketMessageChannel; - if (channel != null) + if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) { var guild = (channel as SocketGuildChannel)?.Guild; if (!(guild?.IsSynced ?? true)) diff --git a/src/Discord.Net.WebSocket/Entities/Channels/SocketGroupChannel.cs b/src/Discord.Net.WebSocket/Entities/Channels/SocketGroupChannel.cs index ff7048848..bb4ed7344 100644 --- a/src/Discord.Net.WebSocket/Entities/Channels/SocketGroupChannel.cs +++ b/src/Discord.Net.WebSocket/Entities/Channels/SocketGroupChannel.cs @@ -120,15 +120,13 @@ namespace Discord.WebSocket //Users public new SocketGroupUser GetUser(ulong id) { - SocketGroupUser user; - if (_users.TryGetValue(id, out user)) + if (_users.TryGetValue(id, out SocketGroupUser user)) return user; return null; } internal SocketGroupUser GetOrAddUser(UserModel model) { - SocketGroupUser user; - if (_users.TryGetValue(model.Id, out user)) + if (_users.TryGetValue(model.Id, out SocketGroupUser user)) return user as SocketGroupUser; else { @@ -139,8 +137,7 @@ namespace Discord.WebSocket } internal SocketGroupUser RemoveUser(ulong id) { - SocketGroupUser user; - if (_users.TryRemove(id, out user)) + if (_users.TryRemove(id, out SocketGroupUser user)) { user.GlobalUser.RemoveRef(Discord); return user as SocketGroupUser; @@ -158,15 +155,13 @@ namespace Discord.WebSocket } internal SocketVoiceState? GetVoiceState(ulong id) { - SocketVoiceState voiceState; - if (_voiceStates.TryGetValue(id, out voiceState)) + if (_voiceStates.TryGetValue(id, out SocketVoiceState voiceState)) return voiceState; return null; } internal SocketVoiceState? RemoveVoiceState(ulong id) { - SocketVoiceState voiceState; - if (_voiceStates.TryRemove(id, out voiceState)) + if (_voiceStates.TryRemove(id, out SocketVoiceState voiceState)) return voiceState; return null; } diff --git a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs index 889587921..6535ecf9a 100644 --- a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs +++ b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs @@ -82,16 +82,7 @@ namespace Discord.WebSocket => Channels.Select(x => x as SocketTextChannel).Where(x => x != null).ToImmutableArray(); public IReadOnlyCollection VoiceChannels => Channels.Select(x => x as SocketVoiceChannel).Where(x => x != null).ToImmutableArray(); - public SocketGuildUser CurrentUser - { - get - { - SocketGuildUser member; - if (_members.TryGetValue(Discord.CurrentUser.Id, out member)) - return member; - return null; - } - } + public SocketGuildUser CurrentUser => _members.TryGetValue(Discord.CurrentUser.Id, out SocketGuildUser member) ? member : null; public SocketRole EveryoneRole => GetRole(Id); public IReadOnlyCollection Channels { @@ -162,8 +153,7 @@ namespace Discord.WebSocket for (int i = 0; i < model.Presences.Length; i++) { - SocketGuildUser member; - if (members.TryGetValue(model.Presences[i].User.Id, out member)) + if (members.TryGetValue(model.Presences[i].User.Id, out SocketGuildUser member)) member.Update(state, model.Presences[i], true); else Debug.Assert(false); @@ -248,8 +238,7 @@ namespace Discord.WebSocket for (int i = 0; i < model.Presences.Length; i++) { - SocketGuildUser member; - if (members.TryGetValue(model.Presences[i].User.Id, out member)) + if (members.TryGetValue(model.Presences[i].User.Id, out SocketGuildUser member)) member.Update(state, model.Presences[i], true); else Debug.Assert(false); @@ -343,8 +332,7 @@ namespace Discord.WebSocket //Roles public SocketRole GetRole(ulong id) { - SocketRole value; - if (_roles.TryGetValue(id, out value)) + if (_roles.TryGetValue(id, out SocketRole value)) return value; return null; } @@ -359,8 +347,7 @@ namespace Discord.WebSocket } internal SocketRole RemoveRole(ulong id) { - SocketRole role; - if (_roles.TryRemove(id, out role)) + if (_roles.TryRemove(id, out SocketRole role)) return role; return null; } @@ -368,8 +355,7 @@ namespace Discord.WebSocket //Users public SocketGuildUser GetUser(ulong id) { - SocketGuildUser member; - if (_members.TryGetValue(id, out member)) + if (_members.TryGetValue(id, out SocketGuildUser member)) return member; return null; } @@ -378,8 +364,7 @@ namespace Discord.WebSocket internal SocketGuildUser AddOrUpdateUser(UserModel model) { - SocketGuildUser member; - if (_members.TryGetValue(model.Id, out member)) + if (_members.TryGetValue(model.Id, out SocketGuildUser member)) member.GlobalUser?.Update(Discord.State, model); else { @@ -391,8 +376,7 @@ namespace Discord.WebSocket } internal SocketGuildUser AddOrUpdateUser(MemberModel model) { - SocketGuildUser member; - if (_members.TryGetValue(model.User.Id, out member)) + if (_members.TryGetValue(model.User.Id, out SocketGuildUser member)) member.Update(Discord.State, model); else { @@ -404,8 +388,7 @@ namespace Discord.WebSocket } internal SocketGuildUser AddOrUpdateUser(PresenceModel model) { - SocketGuildUser member; - if (_members.TryGetValue(model.User.Id, out member)) + if (_members.TryGetValue(model.User.Id, out SocketGuildUser member)) member.Update(Discord.State, model, false); else { @@ -417,8 +400,7 @@ namespace Discord.WebSocket } internal SocketGuildUser RemoveUser(ulong id) { - SocketGuildUser member; - if (_members.TryRemove(id, out member)) + if (_members.TryRemove(id, out SocketGuildUser member)) { DownloadedMemberCount--; member.GlobalUser.RemoveRef(Discord); @@ -466,15 +448,13 @@ namespace Discord.WebSocket } internal SocketVoiceState? GetVoiceState(ulong id) { - SocketVoiceState voiceState; - if (_voiceStates.TryGetValue(id, out voiceState)) + if (_voiceStates.TryGetValue(id, out SocketVoiceState voiceState)) return voiceState; return null; } internal async Task RemoveVoiceStateAsync(ulong id) { - SocketVoiceState voiceState; - if (_voiceStates.TryRemove(id, out voiceState)) + if (_voiceStates.TryRemove(id, out SocketVoiceState voiceState)) { if (_audioClient != null) await _audioClient.RemoveInputStreamAsync(id).ConfigureAwait(false); //User changed channels, end their stream diff --git a/src/Discord.Net.WebSocket/Entities/Messages/MessageCache.cs b/src/Discord.Net.WebSocket/Entities/Messages/MessageCache.cs index 7b8d9c2cd..c2cad4d86 100644 --- a/src/Discord.Net.WebSocket/Entities/Messages/MessageCache.cs +++ b/src/Discord.Net.WebSocket/Entities/Messages/MessageCache.cs @@ -27,24 +27,20 @@ namespace Discord.WebSocket { _orderedMessages.Enqueue(message.Id); - ulong msgId; - SocketMessage msg; - while (_orderedMessages.Count > _size && _orderedMessages.TryDequeue(out msgId)) - _messages.TryRemove(msgId, out msg); + while (_orderedMessages.Count > _size && _orderedMessages.TryDequeue(out ulong msgId)) + _messages.TryRemove(msgId, out SocketMessage msg); } } public SocketMessage Remove(ulong id) { - SocketMessage msg; - _messages.TryRemove(id, out msg); + _messages.TryRemove(id, out SocketMessage msg); return msg; } public SocketMessage Get(ulong id) { - SocketMessage result; - if (_messages.TryGetValue(id, out result)) + if (_messages.TryGetValue(id, out SocketMessage result)) return result; return null; } @@ -67,8 +63,7 @@ namespace Discord.WebSocket return cachedMessageIds .Select(x => { - SocketMessage msg; - if (_messages.TryGetValue(x, out msg)) + if (_messages.TryGetValue(x, out SocketMessage msg)) return msg; return null; }) diff --git a/src/Discord.Net.WebSocket/Net/DefaultWebSocketClient.cs b/src/Discord.Net.WebSocket/Net/DefaultWebSocketClient.cs index 6f667ae41..92175a917 100644 --- a/src/Discord.Net.WebSocket/Net/DefaultWebSocketClient.cs +++ b/src/Discord.Net.WebSocket/Net/DefaultWebSocketClient.cs @@ -206,8 +206,7 @@ namespace Discord.Net.WebSockets //Use the internal buffer if we can get it resultCount = (int)stream.Length; - ArraySegment streamBuffer; - if (stream.TryGetBuffer(out streamBuffer)) + if (stream.TryGetBuffer(out ArraySegment streamBuffer)) result = streamBuffer.Array; else result = stream.ToArray();