From 6cd7d8d5cef55dcfeda5e081e96cb7d5ed1c58bc Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sun, 19 Feb 2023 15:27:25 +0000 Subject: [PATCH] Add DiscordClient.GetUserOrNullAsync D#+ throws an undocumented NotFoundException when a user does not exist. This method returns null instead --- .../src/DiscordClientExtensions.cs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/X10D.DSharpPlus/src/DiscordClientExtensions.cs b/X10D.DSharpPlus/src/DiscordClientExtensions.cs index c9f21f4..f3d8964 100644 --- a/X10D.DSharpPlus/src/DiscordClientExtensions.cs +++ b/X10D.DSharpPlus/src/DiscordClientExtensions.cs @@ -1,4 +1,6 @@ using DSharpPlus; +using DSharpPlus.Entities; +using DSharpPlus.Exceptions; namespace X10D.DSharpPlus; @@ -41,4 +43,37 @@ public static class DiscordClientExtensions }; } } + + /// + /// Gets a user by their ID. If the user is not found, is returned instead of + /// being thrown. + /// + /// The Discord client. + /// The ID of the user to retrieve. + /// 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 + { + // we should never use exceptions for flow control but this is D#+ we're talking about. + // NotFoundException isn't even documented, and yet it gets thrown when a user doesn't exist. + // so this method should hopefully clearly express that - and at least using exceptions for flow control *here*, + // removes the need to do the same in consumer code. + // god I hate this. + return await client.GetUserAsync(userId).ConfigureAwait(false); + } + catch (NotFoundException) + { + return null; + } + } }