Skip to content

Commit

Permalink
refactor: remove throw helpers in X10D.DSharpPlus (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbooth committed Apr 13, 2023
1 parent 0ae3772 commit 3b5fb7b
Show file tree
Hide file tree
Showing 7 changed files with 2 additions and 101 deletions.
9 changes: 0 additions & 9 deletions X10D.DSharpPlus/src/DiscordChannelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@ public static class DiscordChannelExtensions
/// <exception cref="ArgumentNullException"><paramref name="channel" /> is <see langword="null" />.</exception>
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)
{
Expand Down Expand Up @@ -60,10 +56,6 @@ public static class DiscordChannelExtensions
/// </exception>
public static async Task<DiscordChannel> 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));
Expand All @@ -73,7 +65,6 @@ public static async Task<DiscordChannel> NormalizeClientAsync(this DiscordChanne
{
throw new ArgumentNullException(nameof(client));
}
#endif

return await client.GetChannelAsync(channel.Id).ConfigureAwait(false);
}
Expand Down
8 changes: 0 additions & 8 deletions X10D.DSharpPlus/src/DiscordClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ public static class DiscordClientExtensions
/// <exception cref="ArgumentNullException"><paramref name="client" /> is <see langword="null" />.</exception>
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();
Expand All @@ -53,14 +49,10 @@ public static void AutoJoinThreads(this DiscordClient client, bool rejoinIfRemov
/// <exception cref="ArgumentNullException"><paramref name="client" /> is <see langword="null" />.</exception>
public static async Task<DiscordUser?> 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
{
Expand Down
31 changes: 2 additions & 29 deletions X10D.DSharpPlus/src/DiscordEmbedBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,10 @@ public static DiscordEmbedBuilder AddField<T>(
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);
}
Expand All @@ -53,14 +49,10 @@ public static DiscordEmbedBuilder AddFieldIf<T>(
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)
{
Expand Down Expand Up @@ -92,10 +84,6 @@ public static DiscordEmbedBuilder AddFieldIf<T>(
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));
Expand All @@ -105,7 +93,6 @@ public static DiscordEmbedBuilder AddFieldIf<T>(
{
throw new ArgumentNullException(nameof(predicate));
}
#endif

if (predicate.Invoke())
{
Expand Down Expand Up @@ -139,11 +126,6 @@ public static DiscordEmbedBuilder AddFieldIf<T>(
Func<T?> 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));
Expand All @@ -158,7 +140,6 @@ public static DiscordEmbedBuilder AddFieldIf<T>(
{
throw new ArgumentNullException(nameof(valueFactory));
}
#endif

if (predicate.Invoke())
{
Expand Down Expand Up @@ -190,19 +171,15 @@ public static DiscordEmbedBuilder AddFieldIf<T>(
Func<T?> 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)
{
Expand All @@ -220,19 +197,15 @@ public static DiscordEmbedBuilder AddFieldIf<T>(
/// <returns>The current instance of <see cref="DiscordEmbedBuilder" />.</returns>
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);
}
Expand Down
13 changes: 0 additions & 13 deletions X10D.DSharpPlus/src/DiscordGuildExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@ public static class DiscordGuildExtensions
/// <exception cref="ArgumentNullException"><paramref name="guild" /> is <see langword="null" />.</exception>
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);
}
Expand All @@ -37,14 +33,10 @@ public static async Task JoinAllThreadsAsync(this DiscordGuild guild)
/// <exception cref="ArgumentNullException"><paramref name="guild" /> is <see langword="null" />.</exception>
public static async Task<DiscordMember?> 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
{
Expand Down Expand Up @@ -77,10 +69,6 @@ public static async Task JoinAllThreadsAsync(this DiscordGuild guild)
/// </exception>
public static async Task<DiscordGuild> 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));
Expand All @@ -90,7 +78,6 @@ public static async Task<DiscordGuild> NormalizeClientAsync(this DiscordGuild gu
{
throw new ArgumentNullException(nameof(client));
}
#endif

return await client.GetGuildAsync(guild.Id).ConfigureAwait(false);
}
Expand Down
10 changes: 0 additions & 10 deletions X10D.DSharpPlus/src/DiscordMemberExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ public static class DiscordMemberExtensions
/// </returns>
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));
Expand All @@ -31,7 +27,6 @@ public static bool HasRole(this DiscordMember member, DiscordRole role)
{
throw new ArgumentNullException(nameof(role));
}
#endif

return member.Roles.Contains(role);
}
Expand All @@ -52,10 +47,6 @@ public static bool HasRole(this DiscordMember member, DiscordRole role)
/// </exception>
public static async Task<DiscordMember> 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));
Expand All @@ -65,7 +56,6 @@ public static async Task<DiscordMember> 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);
Expand Down
13 changes: 0 additions & 13 deletions X10D.DSharpPlus/src/DiscordMessageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@ public static class DiscordMessageExtensions
/// <exception cref="ArgumentNullException"><paramref name="message" /> is <see langword="null" />.</exception>
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);
Expand All @@ -39,14 +35,10 @@ public static async Task DeleteAfterAsync(this DiscordMessage message, TimeSpan
/// <exception cref="ArgumentNullException"><paramref name="task" /> is <see langword="null" />.</exception>
public static async Task DeleteAfterAsync(this Task<DiscordMessage> 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);
Expand All @@ -68,10 +60,6 @@ public static async Task DeleteAfterAsync(this Task<DiscordMessage> task, TimeSp
/// </exception>
public static async Task<DiscordMessage> 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));
Expand All @@ -81,7 +69,6 @@ public static async Task<DiscordMessage> 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);
Expand Down
19 changes: 0 additions & 19 deletions X10D.DSharpPlus/src/DiscordUserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ public static class DiscordUserExtensions
/// </exception>
public static async Task<DiscordMember?> 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));
Expand All @@ -38,7 +34,6 @@ public static class DiscordUserExtensions
{
throw new ArgumentNullException(nameof(guild));
}
#endif

if (user is DiscordMember member && member.Guild == guild)
{
Expand Down Expand Up @@ -68,14 +63,10 @@ public static class DiscordUserExtensions
/// <exception cref="ArgumentNullException"><paramref name="user" /> is <see langword="null" />.</exception>
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}";
}
Expand All @@ -91,10 +82,6 @@ public static string GetUsernameWithDiscriminator(this DiscordUser user)
/// </returns>
public static async Task<bool> 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));
Expand All @@ -104,7 +91,6 @@ public static async Task<bool> IsInGuildAsync(this DiscordUser user, DiscordGuil
{
throw new ArgumentNullException(nameof(guild));
}
#endif

if (guild.Members.TryGetValue(user.Id, out _))
{
Expand Down Expand Up @@ -138,10 +124,6 @@ public static async Task<bool> IsInGuildAsync(this DiscordUser user, DiscordGuil
/// </exception>
public static async Task<DiscordUser> 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));
Expand All @@ -151,7 +133,6 @@ public static async Task<DiscordUser> NormalizeClientAsync(this DiscordUser user
{
throw new ArgumentNullException(nameof(client));
}
#endif

return await client.GetUserAsync(user.Id).ConfigureAwait(false);
}
Expand Down

0 comments on commit 3b5fb7b

Please sign in to comment.