1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-22 09:28:48 +00:00

refactor: remove throw helpers in X10D.DSharpPlus (#80)

This commit is contained in:
Oliver Booth 2023-04-13 22:35:43 +01:00
parent 0ae377250c
commit 3b5fb7b2be
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
7 changed files with 2 additions and 101 deletions

View File

@ -19,14 +19,10 @@ public static class DiscordChannelExtensions
/// <exception cref="ArgumentNullException"><paramref name="channel" /> is <see langword="null" />.</exception> /// <exception cref="ArgumentNullException"><paramref name="channel" /> is <see langword="null" />.</exception>
public static DiscordChannel? GetCategory(this DiscordChannel channel) public static DiscordChannel? GetCategory(this DiscordChannel channel)
{ {
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(channel);
#else
if (channel is null) if (channel is null)
{ {
throw new ArgumentNullException(nameof(channel)); throw new ArgumentNullException(nameof(channel));
} }
#endif
while (true) while (true)
{ {
@ -60,10 +56,6 @@ public static class DiscordChannelExtensions
/// </exception> /// </exception>
public static async Task<DiscordChannel> NormalizeClientAsync(this DiscordChannel channel, DiscordClient client) 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) if (channel is null)
{ {
throw new ArgumentNullException(nameof(channel)); throw new ArgumentNullException(nameof(channel));
@ -73,7 +65,6 @@ public static class DiscordChannelExtensions
{ {
throw new ArgumentNullException(nameof(client)); throw new ArgumentNullException(nameof(client));
} }
#endif
return await client.GetChannelAsync(channel.Id).ConfigureAwait(false); return await client.GetChannelAsync(channel.Id).ConfigureAwait(false);
} }

View File

@ -20,14 +20,10 @@ public static class DiscordClientExtensions
/// <exception cref="ArgumentNullException"><paramref name="client" /> is <see langword="null" />.</exception> /// <exception cref="ArgumentNullException"><paramref name="client" /> is <see langword="null" />.</exception>
public static void AutoJoinThreads(this DiscordClient client, bool rejoinIfRemoved = true) public static void AutoJoinThreads(this DiscordClient client, bool rejoinIfRemoved = true)
{ {
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(client);
#else
if (client is null) if (client is null)
{ {
throw new ArgumentNullException(nameof(client)); throw new ArgumentNullException(nameof(client));
} }
#endif
client.GuildAvailable += (_, args) => args.Guild.JoinAllThreadsAsync(); client.GuildAvailable += (_, args) => args.Guild.JoinAllThreadsAsync();
client.ThreadCreated += (_, args) => args.Thread.JoinThreadAsync(); client.ThreadCreated += (_, args) => args.Thread.JoinThreadAsync();
@ -53,14 +49,10 @@ public static class DiscordClientExtensions
/// <exception cref="ArgumentNullException"><paramref name="client" /> is <see langword="null" />.</exception> /// <exception cref="ArgumentNullException"><paramref name="client" /> is <see langword="null" />.</exception>
public static async Task<DiscordUser?> GetUserOrNullAsync(this DiscordClient client, ulong userId) public static async Task<DiscordUser?> GetUserOrNullAsync(this DiscordClient client, ulong userId)
{ {
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(client);
#else
if (client is null) if (client is null)
{ {
throw new ArgumentNullException(nameof(client)); throw new ArgumentNullException(nameof(client));
} }
#endif
try try
{ {

View File

@ -23,14 +23,10 @@ public static class DiscordEmbedBuilderExtensions
T? value, T? value,
bool inline = false) bool inline = false)
{ {
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(builder);
#else
if (builder is null) if (builder is null)
{ {
throw new ArgumentNullException(nameof(builder)); throw new ArgumentNullException(nameof(builder));
} }
#endif
return builder.AddField(name, value?.ToString(), inline); return builder.AddField(name, value?.ToString(), inline);
} }
@ -53,14 +49,10 @@ public static class DiscordEmbedBuilderExtensions
T? value, T? value,
bool inline = false) bool inline = false)
{ {
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(builder);
#else
if (builder is null) if (builder is null)
{ {
throw new ArgumentNullException(nameof(builder)); throw new ArgumentNullException(nameof(builder));
} }
#endif
if (condition) if (condition)
{ {
@ -92,10 +84,6 @@ public static class DiscordEmbedBuilderExtensions
T? value, T? value,
bool inline = false) bool inline = false)
{ {
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(predicate);
#else
if (builder is null) if (builder is null)
{ {
throw new ArgumentNullException(nameof(builder)); throw new ArgumentNullException(nameof(builder));
@ -105,7 +93,6 @@ public static class DiscordEmbedBuilderExtensions
{ {
throw new ArgumentNullException(nameof(predicate)); throw new ArgumentNullException(nameof(predicate));
} }
#endif
if (predicate.Invoke()) if (predicate.Invoke())
{ {
@ -139,11 +126,6 @@ public static class DiscordEmbedBuilderExtensions
Func<T?> valueFactory, Func<T?> valueFactory,
bool inline = false) bool inline = false)
{ {
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(predicate);
ArgumentNullException.ThrowIfNull(valueFactory);
#else
if (builder is null) if (builder is null)
{ {
throw new ArgumentNullException(nameof(builder)); throw new ArgumentNullException(nameof(builder));
@ -158,7 +140,6 @@ public static class DiscordEmbedBuilderExtensions
{ {
throw new ArgumentNullException(nameof(valueFactory)); throw new ArgumentNullException(nameof(valueFactory));
} }
#endif
if (predicate.Invoke()) if (predicate.Invoke())
{ {
@ -190,19 +171,15 @@ public static class DiscordEmbedBuilderExtensions
Func<T?> valueFactory, Func<T?> valueFactory,
bool inline = false) bool inline = false)
{ {
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(valueFactory);
#else
if (builder is null) if (builder is null)
{ {
throw new ArgumentNullException(nameof(builder)); throw new ArgumentNullException(nameof(builder));
} }
if (valueFactory is null) if (valueFactory is null)
{ {
throw new ArgumentNullException(nameof(valueFactory)); throw new ArgumentNullException(nameof(valueFactory));
} }
#endif
if (condition) if (condition)
{ {
@ -220,19 +197,15 @@ public static class DiscordEmbedBuilderExtensions
/// <returns>The current instance of <see cref="DiscordEmbedBuilder" />.</returns> /// <returns>The current instance of <see cref="DiscordEmbedBuilder" />.</returns>
public static DiscordEmbedBuilder WithAuthor(this DiscordEmbedBuilder builder, DiscordUser user) 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) if (builder is null)
{ {
throw new ArgumentNullException(nameof(builder)); throw new ArgumentNullException(nameof(builder));
} }
if (user is null) if (user is null)
{ {
throw new ArgumentNullException(nameof(user)); throw new ArgumentNullException(nameof(user));
} }
#endif
return builder.WithAuthor(user.GetUsernameWithDiscriminator(), iconUrl: user.AvatarUrl); return builder.WithAuthor(user.GetUsernameWithDiscriminator(), iconUrl: user.AvatarUrl);
} }

View File

@ -16,14 +16,10 @@ public static class DiscordGuildExtensions
/// <exception cref="ArgumentNullException"><paramref name="guild" /> is <see langword="null" />.</exception> /// <exception cref="ArgumentNullException"><paramref name="guild" /> is <see langword="null" />.</exception>
public static async Task JoinAllThreadsAsync(this DiscordGuild guild) public static async Task JoinAllThreadsAsync(this DiscordGuild guild)
{ {
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(guild);
#else
if (guild is null) if (guild is null)
{ {
throw new ArgumentNullException(nameof(guild)); throw new ArgumentNullException(nameof(guild));
} }
#endif
await Task.WhenAll(guild.Threads.Values.Select(t => t.JoinThreadAsync())).ConfigureAwait(false); await Task.WhenAll(guild.Threads.Values.Select(t => t.JoinThreadAsync())).ConfigureAwait(false);
} }
@ -37,14 +33,10 @@ public static class DiscordGuildExtensions
/// <exception cref="ArgumentNullException"><paramref name="guild" /> is <see langword="null" />.</exception> /// <exception cref="ArgumentNullException"><paramref name="guild" /> is <see langword="null" />.</exception>
public static async Task<DiscordMember?> GetMemberOrNullAsync(this DiscordGuild guild, ulong userId) public static async Task<DiscordMember?> GetMemberOrNullAsync(this DiscordGuild guild, ulong userId)
{ {
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(guild);
#else
if (guild is null) if (guild is null)
{ {
throw new ArgumentNullException(nameof(guild)); throw new ArgumentNullException(nameof(guild));
} }
#endif
try try
{ {
@ -77,10 +69,6 @@ public static class DiscordGuildExtensions
/// </exception> /// </exception>
public static async Task<DiscordGuild> NormalizeClientAsync(this DiscordGuild guild, DiscordClient client) 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) if (guild is null)
{ {
throw new ArgumentNullException(nameof(guild)); throw new ArgumentNullException(nameof(guild));
@ -90,7 +78,6 @@ public static class DiscordGuildExtensions
{ {
throw new ArgumentNullException(nameof(client)); throw new ArgumentNullException(nameof(client));
} }
#endif
return await client.GetGuildAsync(guild.Id).ConfigureAwait(false); return await client.GetGuildAsync(guild.Id).ConfigureAwait(false);
} }

View File

@ -18,10 +18,6 @@ public static class DiscordMemberExtensions
/// </returns> /// </returns>
public static bool HasRole(this DiscordMember member, DiscordRole role) 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) if (member is null)
{ {
throw new ArgumentNullException(nameof(member)); throw new ArgumentNullException(nameof(member));
@ -31,7 +27,6 @@ public static class DiscordMemberExtensions
{ {
throw new ArgumentNullException(nameof(role)); throw new ArgumentNullException(nameof(role));
} }
#endif
return member.Roles.Contains(role); return member.Roles.Contains(role);
} }
@ -52,10 +47,6 @@ public static class DiscordMemberExtensions
/// </exception> /// </exception>
public static async Task<DiscordMember> NormalizeClientAsync(this DiscordMember member, DiscordClient client) 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) if (member is null)
{ {
throw new ArgumentNullException(nameof(member)); throw new ArgumentNullException(nameof(member));
@ -65,7 +56,6 @@ public static class DiscordMemberExtensions
{ {
throw new ArgumentNullException(nameof(client)); throw new ArgumentNullException(nameof(client));
} }
#endif
DiscordGuild guild = await member.Guild.NormalizeClientAsync(client).ConfigureAwait(false); DiscordGuild guild = await member.Guild.NormalizeClientAsync(client).ConfigureAwait(false);
return await guild.GetMemberAsync(member.Id).ConfigureAwait(false); return await guild.GetMemberAsync(member.Id).ConfigureAwait(false);

View File

@ -17,14 +17,10 @@ public static class DiscordMessageExtensions
/// <exception cref="ArgumentNullException"><paramref name="message" /> is <see langword="null" />.</exception> /// <exception cref="ArgumentNullException"><paramref name="message" /> is <see langword="null" />.</exception>
public static async Task DeleteAfterAsync(this DiscordMessage message, TimeSpan delay, string? reason = null) 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) if (message is null)
{ {
throw new ArgumentNullException(nameof(message)); throw new ArgumentNullException(nameof(message));
} }
#endif
await Task.Delay(delay).ConfigureAwait(false); await Task.Delay(delay).ConfigureAwait(false);
await message.DeleteAsync(reason).ConfigureAwait(false); await message.DeleteAsync(reason).ConfigureAwait(false);
@ -39,14 +35,10 @@ public static class DiscordMessageExtensions
/// <exception cref="ArgumentNullException"><paramref name="task" /> is <see langword="null" />.</exception> /// <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) 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) if (task is null)
{ {
throw new ArgumentNullException(nameof(task)); throw new ArgumentNullException(nameof(task));
} }
#endif
DiscordMessage message = await task.ConfigureAwait(false); DiscordMessage message = await task.ConfigureAwait(false);
await message.DeleteAfterAsync(delay, reason).ConfigureAwait(false); await message.DeleteAfterAsync(delay, reason).ConfigureAwait(false);
@ -68,10 +60,6 @@ public static class DiscordMessageExtensions
/// </exception> /// </exception>
public static async Task<DiscordMessage> NormalizeClientAsync(this DiscordMessage message, DiscordClient client) 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) if (message is null)
{ {
throw new ArgumentNullException(nameof(message)); throw new ArgumentNullException(nameof(message));
@ -81,7 +69,6 @@ public static class DiscordMessageExtensions
{ {
throw new ArgumentNullException(nameof(client)); throw new ArgumentNullException(nameof(client));
} }
#endif
DiscordChannel channel = await message.Channel.NormalizeClientAsync(client).ConfigureAwait(false); DiscordChannel channel = await message.Channel.NormalizeClientAsync(client).ConfigureAwait(false);
return await channel.GetMessageAsync(message.Id).ConfigureAwait(false); return await channel.GetMessageAsync(message.Id).ConfigureAwait(false);

View File

@ -25,10 +25,6 @@ public static class DiscordUserExtensions
/// </exception> /// </exception>
public static async Task<DiscordMember?> GetAsMemberOfAsync(this DiscordUser user, DiscordGuild guild) 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) if (user is null)
{ {
throw new ArgumentNullException(nameof(user)); throw new ArgumentNullException(nameof(user));
@ -38,7 +34,6 @@ public static class DiscordUserExtensions
{ {
throw new ArgumentNullException(nameof(guild)); throw new ArgumentNullException(nameof(guild));
} }
#endif
if (user is DiscordMember member && member.Guild == guild) if (user is DiscordMember member && member.Guild == guild)
{ {
@ -68,14 +63,10 @@ public static class DiscordUserExtensions
/// <exception cref="ArgumentNullException"><paramref name="user" /> is <see langword="null" />.</exception> /// <exception cref="ArgumentNullException"><paramref name="user" /> is <see langword="null" />.</exception>
public static string GetUsernameWithDiscriminator(this DiscordUser user) public static string GetUsernameWithDiscriminator(this DiscordUser user)
{ {
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(user);
#else
if (user is null) if (user is null)
{ {
throw new ArgumentNullException(nameof(user)); throw new ArgumentNullException(nameof(user));
} }
#endif
return $"{user.Username}#{user.Discriminator}"; return $"{user.Username}#{user.Discriminator}";
} }
@ -91,10 +82,6 @@ public static class DiscordUserExtensions
/// </returns> /// </returns>
public static async Task<bool> IsInGuildAsync(this DiscordUser user, DiscordGuild guild) 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) if (user is null)
{ {
throw new ArgumentNullException(nameof(user)); throw new ArgumentNullException(nameof(user));
@ -104,7 +91,6 @@ public static class DiscordUserExtensions
{ {
throw new ArgumentNullException(nameof(guild)); throw new ArgumentNullException(nameof(guild));
} }
#endif
if (guild.Members.TryGetValue(user.Id, out _)) if (guild.Members.TryGetValue(user.Id, out _))
{ {
@ -138,10 +124,6 @@ public static class DiscordUserExtensions
/// </exception> /// </exception>
public static async Task<DiscordUser> NormalizeClientAsync(this DiscordUser user, DiscordClient client) 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) if (user is null)
{ {
throw new ArgumentNullException(nameof(user)); throw new ArgumentNullException(nameof(user));
@ -151,7 +133,6 @@ public static class DiscordUserExtensions
{ {
throw new ArgumentNullException(nameof(client)); throw new ArgumentNullException(nameof(client));
} }
#endif
return await client.GetUserAsync(user.Id).ConfigureAwait(false); return await client.GetUserAsync(user.Id).ConfigureAwait(false);
} }