From 3b5fb7b2be66d881c4a92ce2827110ffdb16329c Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Thu, 13 Apr 2023 22:35:43 +0100 Subject: [PATCH] refactor: remove throw helpers in X10D.DSharpPlus (#80) --- .../src/DiscordChannelExtensions.cs | 9 ------ .../src/DiscordClientExtensions.cs | 8 ----- .../src/DiscordEmbedBuilderExtensions.cs | 31 ++----------------- X10D.DSharpPlus/src/DiscordGuildExtensions.cs | 13 -------- .../src/DiscordMemberExtensions.cs | 10 ------ .../src/DiscordMessageExtensions.cs | 13 -------- X10D.DSharpPlus/src/DiscordUserExtensions.cs | 19 ------------ 7 files changed, 2 insertions(+), 101 deletions(-) diff --git a/X10D.DSharpPlus/src/DiscordChannelExtensions.cs b/X10D.DSharpPlus/src/DiscordChannelExtensions.cs index 2734af986..820ced0ee 100644 --- a/X10D.DSharpPlus/src/DiscordChannelExtensions.cs +++ b/X10D.DSharpPlus/src/DiscordChannelExtensions.cs @@ -19,14 +19,10 @@ public static class DiscordChannelExtensions /// is . public static DiscordChannel? GetCategory(this DiscordChannel channel) { -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(channel); -#else if (channel is null) { throw new ArgumentNullException(nameof(channel)); } -#endif while (true) { @@ -60,10 +56,6 @@ public static class DiscordChannelExtensions /// public static async Task NormalizeClientAsync(this DiscordChannel channel, DiscordClient client) { -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(channel); - ArgumentNullException.ThrowIfNull(client); -#else if (channel is null) { throw new ArgumentNullException(nameof(channel)); @@ -73,7 +65,6 @@ public static async Task NormalizeClientAsync(this DiscordChanne { throw new ArgumentNullException(nameof(client)); } -#endif return await client.GetChannelAsync(channel.Id).ConfigureAwait(false); } diff --git a/X10D.DSharpPlus/src/DiscordClientExtensions.cs b/X10D.DSharpPlus/src/DiscordClientExtensions.cs index f3d8964d4..5dfd41573 100644 --- a/X10D.DSharpPlus/src/DiscordClientExtensions.cs +++ b/X10D.DSharpPlus/src/DiscordClientExtensions.cs @@ -20,14 +20,10 @@ public static class DiscordClientExtensions /// is . public static void AutoJoinThreads(this DiscordClient client, bool rejoinIfRemoved = true) { -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(client); -#else if (client is null) { throw new ArgumentNullException(nameof(client)); } -#endif client.GuildAvailable += (_, args) => args.Guild.JoinAllThreadsAsync(); client.ThreadCreated += (_, args) => args.Thread.JoinThreadAsync(); @@ -53,14 +49,10 @@ public static void AutoJoinThreads(this DiscordClient client, bool rejoinIfRemov /// is . public static async Task GetUserOrNullAsync(this DiscordClient client, ulong userId) { -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(client); -#else if (client is null) { throw new ArgumentNullException(nameof(client)); } -#endif try { diff --git a/X10D.DSharpPlus/src/DiscordEmbedBuilderExtensions.cs b/X10D.DSharpPlus/src/DiscordEmbedBuilderExtensions.cs index 4a23a34ff..1c2c479c9 100644 --- a/X10D.DSharpPlus/src/DiscordEmbedBuilderExtensions.cs +++ b/X10D.DSharpPlus/src/DiscordEmbedBuilderExtensions.cs @@ -23,14 +23,10 @@ public static DiscordEmbedBuilder AddField( T? value, bool inline = false) { -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(builder); -#else if (builder is null) { throw new ArgumentNullException(nameof(builder)); } -#endif return builder.AddField(name, value?.ToString(), inline); } @@ -53,14 +49,10 @@ public static DiscordEmbedBuilder AddFieldIf( T? value, bool inline = false) { -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(builder); -#else if (builder is null) { throw new ArgumentNullException(nameof(builder)); } -#endif if (condition) { @@ -92,10 +84,6 @@ public static DiscordEmbedBuilder AddFieldIf( T? value, bool inline = false) { -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(builder); - ArgumentNullException.ThrowIfNull(predicate); -#else if (builder is null) { throw new ArgumentNullException(nameof(builder)); @@ -105,7 +93,6 @@ public static DiscordEmbedBuilder AddFieldIf( { throw new ArgumentNullException(nameof(predicate)); } -#endif if (predicate.Invoke()) { @@ -139,11 +126,6 @@ public static DiscordEmbedBuilder AddFieldIf( Func valueFactory, bool inline = false) { -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(builder); - ArgumentNullException.ThrowIfNull(predicate); - ArgumentNullException.ThrowIfNull(valueFactory); -#else if (builder is null) { throw new ArgumentNullException(nameof(builder)); @@ -158,7 +140,6 @@ public static DiscordEmbedBuilder AddFieldIf( { throw new ArgumentNullException(nameof(valueFactory)); } -#endif if (predicate.Invoke()) { @@ -190,19 +171,15 @@ public static DiscordEmbedBuilder AddFieldIf( Func valueFactory, bool inline = false) { -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(builder); - ArgumentNullException.ThrowIfNull(valueFactory); -#else if (builder is null) { throw new ArgumentNullException(nameof(builder)); } + if (valueFactory is null) { throw new ArgumentNullException(nameof(valueFactory)); } -#endif if (condition) { @@ -220,19 +197,15 @@ public static DiscordEmbedBuilder AddFieldIf( /// The current instance of . public static DiscordEmbedBuilder WithAuthor(this DiscordEmbedBuilder builder, DiscordUser user) { -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(builder); - ArgumentNullException.ThrowIfNull(user); -#else if (builder is null) { throw new ArgumentNullException(nameof(builder)); } + if (user is null) { throw new ArgumentNullException(nameof(user)); } -#endif return builder.WithAuthor(user.GetUsernameWithDiscriminator(), iconUrl: user.AvatarUrl); } diff --git a/X10D.DSharpPlus/src/DiscordGuildExtensions.cs b/X10D.DSharpPlus/src/DiscordGuildExtensions.cs index c03016bc3..ff4636e33 100644 --- a/X10D.DSharpPlus/src/DiscordGuildExtensions.cs +++ b/X10D.DSharpPlus/src/DiscordGuildExtensions.cs @@ -16,14 +16,10 @@ public static class DiscordGuildExtensions /// is . public static async Task JoinAllThreadsAsync(this DiscordGuild guild) { -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(guild); -#else if (guild is null) { throw new ArgumentNullException(nameof(guild)); } -#endif await Task.WhenAll(guild.Threads.Values.Select(t => t.JoinThreadAsync())).ConfigureAwait(false); } @@ -37,14 +33,10 @@ public static async Task JoinAllThreadsAsync(this DiscordGuild guild) /// is . public static async Task GetMemberOrNullAsync(this DiscordGuild guild, ulong userId) { -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(guild); -#else if (guild is null) { throw new ArgumentNullException(nameof(guild)); } -#endif try { @@ -77,10 +69,6 @@ public static async Task JoinAllThreadsAsync(this DiscordGuild guild) /// public static async Task NormalizeClientAsync(this DiscordGuild guild, DiscordClient client) { -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(guild); - ArgumentNullException.ThrowIfNull(client); -#else if (guild is null) { throw new ArgumentNullException(nameof(guild)); @@ -90,7 +78,6 @@ public static async Task NormalizeClientAsync(this DiscordGuild gu { throw new ArgumentNullException(nameof(client)); } -#endif return await client.GetGuildAsync(guild.Id).ConfigureAwait(false); } diff --git a/X10D.DSharpPlus/src/DiscordMemberExtensions.cs b/X10D.DSharpPlus/src/DiscordMemberExtensions.cs index 3fd202048..7ec99256b 100644 --- a/X10D.DSharpPlus/src/DiscordMemberExtensions.cs +++ b/X10D.DSharpPlus/src/DiscordMemberExtensions.cs @@ -18,10 +18,6 @@ public static class DiscordMemberExtensions /// public static bool HasRole(this DiscordMember member, DiscordRole role) { -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(member); - ArgumentNullException.ThrowIfNull(role); -#else if (member is null) { throw new ArgumentNullException(nameof(member)); @@ -31,7 +27,6 @@ public static bool HasRole(this DiscordMember member, DiscordRole role) { throw new ArgumentNullException(nameof(role)); } -#endif return member.Roles.Contains(role); } @@ -52,10 +47,6 @@ public static bool HasRole(this DiscordMember member, DiscordRole role) /// public static async Task NormalizeClientAsync(this DiscordMember member, DiscordClient client) { -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(member); - ArgumentNullException.ThrowIfNull(client); -#else if (member is null) { throw new ArgumentNullException(nameof(member)); @@ -65,7 +56,6 @@ public static async Task NormalizeClientAsync(this DiscordMember { throw new ArgumentNullException(nameof(client)); } -#endif DiscordGuild guild = await member.Guild.NormalizeClientAsync(client).ConfigureAwait(false); return await guild.GetMemberAsync(member.Id).ConfigureAwait(false); diff --git a/X10D.DSharpPlus/src/DiscordMessageExtensions.cs b/X10D.DSharpPlus/src/DiscordMessageExtensions.cs index f8317c71f..f43116d4c 100644 --- a/X10D.DSharpPlus/src/DiscordMessageExtensions.cs +++ b/X10D.DSharpPlus/src/DiscordMessageExtensions.cs @@ -17,14 +17,10 @@ public static class DiscordMessageExtensions /// is . public static async Task DeleteAfterAsync(this DiscordMessage message, TimeSpan delay, string? reason = null) { -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(message); -#else if (message is null) { throw new ArgumentNullException(nameof(message)); } -#endif await Task.Delay(delay).ConfigureAwait(false); await message.DeleteAsync(reason).ConfigureAwait(false); @@ -39,14 +35,10 @@ public static async Task DeleteAfterAsync(this DiscordMessage message, TimeSpan /// is . public static async Task DeleteAfterAsync(this Task task, TimeSpan delay, string? reason = null) { -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(task); -#else if (task is null) { throw new ArgumentNullException(nameof(task)); } -#endif DiscordMessage message = await task.ConfigureAwait(false); await message.DeleteAfterAsync(delay, reason).ConfigureAwait(false); @@ -68,10 +60,6 @@ public static async Task DeleteAfterAsync(this Task task, TimeSp /// public static async Task NormalizeClientAsync(this DiscordMessage message, DiscordClient client) { -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(message); - ArgumentNullException.ThrowIfNull(client); -#else if (message is null) { throw new ArgumentNullException(nameof(message)); @@ -81,7 +69,6 @@ public static async Task NormalizeClientAsync(this DiscordMessag { throw new ArgumentNullException(nameof(client)); } -#endif DiscordChannel channel = await message.Channel.NormalizeClientAsync(client).ConfigureAwait(false); return await channel.GetMessageAsync(message.Id).ConfigureAwait(false); diff --git a/X10D.DSharpPlus/src/DiscordUserExtensions.cs b/X10D.DSharpPlus/src/DiscordUserExtensions.cs index 6ab831458..adfd477d3 100644 --- a/X10D.DSharpPlus/src/DiscordUserExtensions.cs +++ b/X10D.DSharpPlus/src/DiscordUserExtensions.cs @@ -25,10 +25,6 @@ public static class DiscordUserExtensions /// public static async Task GetAsMemberOfAsync(this DiscordUser user, DiscordGuild guild) { -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(user); - ArgumentNullException.ThrowIfNull(guild); -#else if (user is null) { throw new ArgumentNullException(nameof(user)); @@ -38,7 +34,6 @@ public static class DiscordUserExtensions { throw new ArgumentNullException(nameof(guild)); } -#endif if (user is DiscordMember member && member.Guild == guild) { @@ -68,14 +63,10 @@ public static class DiscordUserExtensions /// is . public static string GetUsernameWithDiscriminator(this DiscordUser user) { -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(user); -#else if (user is null) { throw new ArgumentNullException(nameof(user)); } -#endif return $"{user.Username}#{user.Discriminator}"; } @@ -91,10 +82,6 @@ public static string GetUsernameWithDiscriminator(this DiscordUser user) /// public static async Task IsInGuildAsync(this DiscordUser user, DiscordGuild guild) { -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(user); - ArgumentNullException.ThrowIfNull(guild); -#else if (user is null) { throw new ArgumentNullException(nameof(user)); @@ -104,7 +91,6 @@ public static async Task IsInGuildAsync(this DiscordUser user, DiscordGuil { throw new ArgumentNullException(nameof(guild)); } -#endif if (guild.Members.TryGetValue(user.Id, out _)) { @@ -138,10 +124,6 @@ public static async Task IsInGuildAsync(this DiscordUser user, DiscordGuil /// public static async Task NormalizeClientAsync(this DiscordUser user, DiscordClient client) { -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(user); - ArgumentNullException.ThrowIfNull(client); -#else if (user is null) { throw new ArgumentNullException(nameof(user)); @@ -151,7 +133,6 @@ public static async Task NormalizeClientAsync(this DiscordUser user { throw new ArgumentNullException(nameof(client)); } -#endif return await client.GetUserAsync(user.Id).ConfigureAwait(false); }