using DSharpPlus; using DSharpPlus.Entities; using DSharpPlus.Exceptions; namespace X10D.DSharpPlus; /// /// Extension methods for . /// public static class DiscordClientExtensions { /// /// Instructs the client to automatically join all existing threads, and any newly-created threads. /// /// The whose events should be subscribed. /// /// to automatically rejoin a thread if this client was removed; otherwise, /// . /// /// 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(); if (rejoinIfRemoved) { client.ThreadMembersUpdated += (_, args) => { if (args.RemovedMembers.Any(m => m.Id == client.CurrentUser.Id)) return args.Thread.JoinThreadAsync(); return Task.CompletedTask; }; } } /// /// 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; } } }