From 15b58edb1911ac3a4b1558fcb0da862076d483b3 Mon Sep 17 00:00:00 2001 From: Still Hsu <341464@gmail.com> Date: Fri, 15 Jun 2018 06:38:41 +0800 Subject: [PATCH] Remove/cleanup redundant variables --- src/Discord.Net.Core/Utils/Permissions.cs | 6 +++--- src/Discord.Net.Rest/BaseDiscordClient.cs | 4 ++-- src/Discord.Net.Rest/ClientHelper.cs | 2 +- src/Discord.Net.Rest/DiscordRestApiClient.cs | 18 +++++++++--------- src/Discord.Net.Rest/Net/Queue/RequestQueue.cs | 2 +- src/Discord.Net.WebSocket/Audio/AudioClient.cs | 3 +-- .../Audio/Streams/BufferedWriteStream.cs | 2 +- .../DiscordShardedClient.cs | 18 +++++++++--------- .../DiscordSocketClient.cs | 6 +++--- .../DiscordVoiceApiClient.cs | 2 +- 10 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/Discord.Net.Core/Utils/Permissions.cs b/src/Discord.Net.Core/Utils/Permissions.cs index 04e6784c3..1f25dcea9 100644 --- a/src/Discord.Net.Core/Utils/Permissions.cs +++ b/src/Discord.Net.Core/Utils/Permissions.cs @@ -1,4 +1,4 @@ -using System.Runtime.CompilerServices; +using System.Runtime.CompilerServices; namespace Discord { @@ -151,7 +151,7 @@ namespace Discord if (perms != null) resolvedPermissions = (resolvedPermissions & ~perms.Value.DenyValue) | perms.Value.AllowValue; - if (channel is ITextChannel textChannel) + if (channel is ITextChannel) { if (!GetValue(resolvedPermissions, ChannelPermission.ViewChannel)) { @@ -167,7 +167,7 @@ namespace Discord resolvedPermissions &= ~(ulong)ChannelPermission.AttachFiles; } } - resolvedPermissions &= mask; //Ensure we didnt get any permissions this channel doesnt support (from guildPerms, for example) + resolvedPermissions &= mask; //Ensure we didn't get any permissions this channel doesn't support (from guildPerms, for example) } return resolvedPermissions; diff --git a/src/Discord.Net.Rest/BaseDiscordClient.cs b/src/Discord.Net.Rest/BaseDiscordClient.cs index 44d19d018..bcb0c838b 100644 --- a/src/Discord.Net.Rest/BaseDiscordClient.cs +++ b/src/Discord.Net.Rest/BaseDiscordClient.cs @@ -58,7 +58,7 @@ namespace Discord.Rest ApiClient.SentRequest += async (method, endpoint, millis) => await _restLogger.VerboseAsync($"{method} {endpoint}: {millis} ms").ConfigureAwait(false); } - public async Task LoginAsync(TokenType tokenType, string token, bool validateToken = true) + public async Task LoginAsync(TokenType tokenType, string token) { await _stateLock.WaitAsync().ConfigureAwait(false); try @@ -85,7 +85,7 @@ namespace Discord.Rest await OnLoginAsync(tokenType, token).ConfigureAwait(false); LoginState = LoginState.LoggedIn; } - catch (Exception) + catch { await LogoutInternalAsync().ConfigureAwait(false); throw; diff --git a/src/Discord.Net.Rest/ClientHelper.cs b/src/Discord.Net.Rest/ClientHelper.cs index 24cabe4d4..976c2b773 100644 --- a/src/Discord.Net.Rest/ClientHelper.cs +++ b/src/Discord.Net.Rest/ClientHelper.cs @@ -49,7 +49,7 @@ namespace Discord.Rest public static async Task> GetConnectionsAsync(BaseDiscordClient client, RequestOptions options) { var models = await client.ApiClient.GetMyConnectionsAsync(options).ConfigureAwait(false); - return models.Select(x => RestConnection.Create(x)).ToImmutableArray(); + return models.Select(RestConnection.Create).ToImmutableArray(); } public static async Task GetInviteAsync(BaseDiscordClient client, diff --git a/src/Discord.Net.Rest/DiscordRestApiClient.cs b/src/Discord.Net.Rest/DiscordRestApiClient.cs index 70a2acd15..d9f1eddea 100644 --- a/src/Discord.Net.Rest/DiscordRestApiClient.cs +++ b/src/Discord.Net.Rest/DiscordRestApiClient.cs @@ -127,7 +127,7 @@ namespace Discord.API LoginState = LoginState.LoggedIn; } - catch (Exception) + catch { await LogoutInternalAsync().ConfigureAwait(false); throw; @@ -168,7 +168,7 @@ namespace Discord.API //Core internal Task SendAsync(string method, Expression> endpointExpr, BucketIds ids, ClientBucketType clientBucket = ClientBucketType.Unbucketed, RequestOptions options = null, [CallerMemberName] string funcName = null) - => SendAsync(method, GetEndpoint(endpointExpr), GetBucketId(ids, endpointExpr, AuthTokenType, funcName), clientBucket, options); + => SendAsync(method, GetEndpoint(endpointExpr), GetBucketId(ids, endpointExpr, funcName), clientBucket, options); public async Task SendAsync(string method, string endpoint, string bucketId = null, ClientBucketType clientBucket = ClientBucketType.Unbucketed, RequestOptions options = null) { @@ -182,7 +182,7 @@ namespace Discord.API internal Task SendJsonAsync(string method, Expression> endpointExpr, object payload, BucketIds ids, ClientBucketType clientBucket = ClientBucketType.Unbucketed, RequestOptions options = null, [CallerMemberName] string funcName = null) - => SendJsonAsync(method, GetEndpoint(endpointExpr), payload, GetBucketId(ids, endpointExpr, AuthTokenType, funcName), clientBucket, options); + => SendJsonAsync(method, GetEndpoint(endpointExpr), payload, GetBucketId(ids, endpointExpr, funcName), clientBucket, options); public async Task SendJsonAsync(string method, string endpoint, object payload, string bucketId = null, ClientBucketType clientBucket = ClientBucketType.Unbucketed, RequestOptions options = null) { @@ -197,7 +197,7 @@ namespace Discord.API internal Task SendMultipartAsync(string method, Expression> endpointExpr, IReadOnlyDictionary multipartArgs, BucketIds ids, ClientBucketType clientBucket = ClientBucketType.Unbucketed, RequestOptions options = null, [CallerMemberName] string funcName = null) - => SendMultipartAsync(method, GetEndpoint(endpointExpr), multipartArgs, GetBucketId(ids, endpointExpr, AuthTokenType, funcName), clientBucket, options); + => SendMultipartAsync(method, GetEndpoint(endpointExpr), multipartArgs, GetBucketId(ids, endpointExpr, funcName), clientBucket, options); public async Task SendMultipartAsync(string method, string endpoint, IReadOnlyDictionary multipartArgs, string bucketId = null, ClientBucketType clientBucket = ClientBucketType.Unbucketed, RequestOptions options = null) { @@ -211,7 +211,7 @@ namespace Discord.API internal Task SendAsync(string method, Expression> endpointExpr, BucketIds ids, ClientBucketType clientBucket = ClientBucketType.Unbucketed, RequestOptions options = null, [CallerMemberName] string funcName = null) where TResponse : class - => SendAsync(method, GetEndpoint(endpointExpr), GetBucketId(ids, endpointExpr, AuthTokenType, funcName), clientBucket, options); + => SendAsync(method, GetEndpoint(endpointExpr), GetBucketId(ids, endpointExpr, funcName), clientBucket, options); public async Task SendAsync(string method, string endpoint, string bucketId = null, ClientBucketType clientBucket = ClientBucketType.Unbucketed, RequestOptions options = null) where TResponse : class { @@ -224,7 +224,7 @@ namespace Discord.API internal Task SendJsonAsync(string method, Expression> endpointExpr, object payload, BucketIds ids, ClientBucketType clientBucket = ClientBucketType.Unbucketed, RequestOptions options = null, [CallerMemberName] string funcName = null) where TResponse : class - => SendJsonAsync(method, GetEndpoint(endpointExpr), payload, GetBucketId(ids, endpointExpr, AuthTokenType, funcName), clientBucket, options); + => SendJsonAsync(method, GetEndpoint(endpointExpr), payload, GetBucketId(ids, endpointExpr, funcName), clientBucket, options); public async Task SendJsonAsync(string method, string endpoint, object payload, string bucketId = null, ClientBucketType clientBucket = ClientBucketType.Unbucketed, RequestOptions options = null) where TResponse : class { @@ -238,7 +238,7 @@ namespace Discord.API internal Task SendMultipartAsync(string method, Expression> endpointExpr, IReadOnlyDictionary multipartArgs, BucketIds ids, ClientBucketType clientBucket = ClientBucketType.Unbucketed, RequestOptions options = null, [CallerMemberName] string funcName = null) - => SendMultipartAsync(method, GetEndpoint(endpointExpr), multipartArgs, GetBucketId(ids, endpointExpr, AuthTokenType, funcName), clientBucket, options); + => SendMultipartAsync(method, GetEndpoint(endpointExpr), multipartArgs, GetBucketId(ids, endpointExpr, funcName), clientBucket, options); public async Task SendMultipartAsync(string method, string endpoint, IReadOnlyDictionary multipartArgs, string bucketId = null, ClientBucketType clientBucket = ClientBucketType.Unbucketed, RequestOptions options = null) { @@ -1354,7 +1354,7 @@ namespace Discord.API { return endpointExpr.Compile()(); } - private static string GetBucketId(BucketIds ids, Expression> endpointExpr, TokenType tokenType, string callingMethod) + private static string GetBucketId(BucketIds ids, Expression> endpointExpr, string callingMethod) { return _bucketIdGenerators.GetOrAdd(callingMethod, x => CreateBucketId(endpointExpr))(ids); } @@ -1416,7 +1416,7 @@ namespace Discord.API } catch (Exception ex) { - throw new InvalidOperationException("Failed to generate the bucket id for this operation", ex); + throw new InvalidOperationException("Failed to generate the bucket id for this operation.", ex); } } diff --git a/src/Discord.Net.Rest/Net/Queue/RequestQueue.cs b/src/Discord.Net.Rest/Net/Queue/RequestQueue.cs index e3b34eb8f..05b358b1d 100644 --- a/src/Discord.Net.Rest/Net/Queue/RequestQueue.cs +++ b/src/Discord.Net.Rest/Net/Queue/RequestQueue.cs @@ -115,7 +115,7 @@ namespace Discord.Net.Queue foreach (var bucket in _buckets.Select(x => x.Value)) { if ((now - bucket.LastAttemptAt).TotalMinutes > 1.0) - _buckets.TryRemove(bucket.Id, out RequestBucket ignored); + _buckets.TryRemove(bucket.Id, out _); } await Task.Delay(60000, _cancelToken.Token).ConfigureAwait(false); //Runs each minute } diff --git a/src/Discord.Net.WebSocket/Audio/AudioClient.cs b/src/Discord.Net.WebSocket/Audio/AudioClient.cs index e6cf20312..1d44a49b4 100644 --- a/src/Discord.Net.WebSocket/Audio/AudioClient.cs +++ b/src/Discord.Net.WebSocket/Audio/AudioClient.cs @@ -131,7 +131,7 @@ namespace Discord.Audio await keepaliveTask.ConfigureAwait(false); _keepaliveTask = null; - while (_heartbeatTimes.TryDequeue(out long time)) { } + while (_heartbeatTimes.TryDequeue(out _)) { } _lastMessageTime = 0; await ClearInputStreamsAsync().ConfigureAwait(false); @@ -425,7 +425,6 @@ namespace Discord.Audio } private async Task RunKeepaliveAsync(int intervalMillis, CancellationToken cancelToken) { - var packet = new byte[8]; try { await _audioLogger.DebugAsync("Keepalive Started").ConfigureAwait(false); diff --git a/src/Discord.Net.WebSocket/Audio/Streams/BufferedWriteStream.cs b/src/Discord.Net.WebSocket/Audio/Streams/BufferedWriteStream.cs index 1365ddac2..47a7e2809 100644 --- a/src/Discord.Net.WebSocket/Audio/Streams/BufferedWriteStream.cs +++ b/src/Discord.Net.WebSocket/Audio/Streams/BufferedWriteStream.cs @@ -169,7 +169,7 @@ namespace Discord.Audio.Streams { do cancelToken.ThrowIfCancellationRequested(); - while (_queuedFrames.TryDequeue(out Frame ignored)); + while (_queuedFrames.TryDequeue(out _)); return Task.Delay(0); } } diff --git a/src/Discord.Net.WebSocket/DiscordShardedClient.cs b/src/Discord.Net.WebSocket/DiscordShardedClient.cs index 039ea2fe5..c8f0d4510 100644 --- a/src/Discord.Net.WebSocket/DiscordShardedClient.cs +++ b/src/Discord.Net.WebSocket/DiscordShardedClient.cs @@ -14,10 +14,10 @@ namespace Discord.WebSocket private readonly DiscordSocketConfig _baseConfig; private readonly SemaphoreSlim _connectionGroupLock; private int[] _shardIds; - private Dictionary _shardIdsToIndex; + private readonly Dictionary _shardIdsToIndex; private DiscordSocketClient[] _shards; private int _totalShards; - private bool _automaticShards; + private readonly bool _automaticShards; /// public override int Latency { get => GetLatency(); protected set { } } @@ -26,7 +26,7 @@ namespace Discord.WebSocket /// public override IActivity Activity { get => _shards[0].Activity; protected set { } } - internal new DiscordSocketApiClient ApiClient => base.ApiClient as DiscordSocketApiClient; + internal new DiscordSocketApiClient ApiClient => base.ApiClient; /// public override IReadOnlyCollection Guilds => GetGuilds().ToReadOnlyCollection(GetGuildCount); /// @@ -95,13 +95,13 @@ namespace Discord.WebSocket } } - //Assume threadsafe: already in a connection lock + //Assume thread safe: already in a connection lock for (int i = 0; i < _shards.Length; i++) - await _shards[i].LoginAsync(tokenType, token, false); + await _shards[i].LoginAsync(tokenType, token); } internal override async Task OnLogoutAsync() { - //Assume threadsafe: already in a connection lock + //Assume thread safe: already in a connection lock if (_shards != null) { for (int i = 0; i < _shards.Length; i++) @@ -218,11 +218,11 @@ namespace Discord.WebSocket public override RestVoiceRegion GetVoiceRegion(string id) => _shards[0].GetVoiceRegion(id); - /// - /// Downloads the users list for the provided guilds if they don't have a complete list. - /// + /// + /// is public override async Task DownloadUsersAsync(IEnumerable guilds) { + if (guilds == null) throw new ArgumentNullException(nameof(guilds)); for (int i = 0; i < _shards.Length; i++) { int id = _shardIds[i]; diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index d147f365b..a373829fb 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -217,7 +217,7 @@ namespace Discord.WebSocket await heartbeatTask.ConfigureAwait(false); _heartbeatTask = null; - while (_heartbeatTimes.TryDequeue(out long time)) { } + while (_heartbeatTimes.TryDequeue(out _)) { } _lastMessageTime = 0; await _gatewayLogger.DebugAsync("Waiting for guild downloader").ConfigureAwait(false); @@ -228,7 +228,7 @@ namespace Discord.WebSocket //Clear large guild queue await _gatewayLogger.DebugAsync("Clearing large guild queue").ConfigureAwait(false); - while (_largeGuilds.TryDequeue(out ulong guildId)) { } + while (_largeGuilds.TryDequeue(out _)) { } //Raise virtual GUILD_UNAVAILABLEs await _gatewayLogger.DebugAsync("Raising virtual GuildUnavailables").ConfigureAwait(false); @@ -1636,7 +1636,7 @@ namespace Discord.WebSocket var guild = State.RemoveGuild(id); if (guild != null) { - foreach (var channel in guild.Channels) + foreach (var _ in guild.Channels) State.RemoveChannel(id); foreach (var user in guild.Users) user.GlobalUser.RemoveRef(this); diff --git a/src/Discord.Net.WebSocket/DiscordVoiceApiClient.cs b/src/Discord.Net.WebSocket/DiscordVoiceApiClient.cs index 0eb92caed..01eb00c83 100644 --- a/src/Discord.Net.WebSocket/DiscordVoiceApiClient.cs +++ b/src/Discord.Net.WebSocket/DiscordVoiceApiClient.cs @@ -188,7 +188,7 @@ namespace Discord.Audio ConnectionState = ConnectionState.Connected; } - catch (Exception) + catch { await DisconnectInternalAsync().ConfigureAwait(false); throw;