From eacc4b3896308911dfff5b4b63926c2c1be682d6 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Sun, 24 Dec 2023 05:36:15 +0100 Subject: [PATCH] fix: fix app command module --- .../ApplicationCommandsExtension.cs | 9 ++- .../Workers/RegistrationWorker.cs | 74 +++++++++---------- DisCatSharp.Common/Types/LinqMethods.cs | 5 +- .../Utilities/EnsureObjectStates.cs | 38 ++++++++++ 4 files changed, 84 insertions(+), 42 deletions(-) diff --git a/DisCatSharp.ApplicationCommands/ApplicationCommandsExtension.cs b/DisCatSharp.ApplicationCommands/ApplicationCommandsExtension.cs index f89f30f3f1..dbd87870e8 100644 --- a/DisCatSharp.ApplicationCommands/ApplicationCommandsExtension.cs +++ b/DisCatSharp.ApplicationCommands/ApplicationCommandsExtension.cs @@ -765,9 +765,12 @@ private async Task RegisterCommands(List if (updateList != null && updateList.Count != 0) { var regCommands = await RegistrationWorker.RegisterGlobalCommandsAsync(this.Client, updateList).ConfigureAwait(false); - var actualCommands = regCommands.Distinct().ToList(); - commands.AddRange(actualCommands); - GlobalCommandsInternal.AddRange(actualCommands); +if (regCommands is not null) +{ + var actualCommands = regCommands.Distinct().ToList(); + commands.AddRange(actualCommands); + GlobalCommandsInternal.AddRange(actualCommands); +} } else foreach (var cmd in GlobalDiscordCommands) diff --git a/DisCatSharp.ApplicationCommands/Workers/RegistrationWorker.cs b/DisCatSharp.ApplicationCommands/Workers/RegistrationWorker.cs index 51032f3b33..91687126b5 100644 --- a/DisCatSharp.ApplicationCommands/Workers/RegistrationWorker.cs +++ b/DisCatSharp.ApplicationCommands/Workers/RegistrationWorker.cs @@ -22,13 +22,13 @@ internal class RegistrationWorker /// The discord client. /// The command list. /// A list of registered commands. - internal static async Task> RegisterGlobalCommandsAsync(DiscordClient client, List commands) + internal static async Task?> RegisterGlobalCommandsAsync(DiscordClient client, List commands) { var (changedCommands, unchangedCommands) = BuildGlobalOverwriteList(client, commands); var globalCommandsCreateList = BuildGlobalCreateList(client, commands); var globalCommandsDeleteList = BuildGlobalDeleteList(client, commands); - if (globalCommandsCreateList.NotEmptyAndNotNull() && unchangedCommands.NotEmptyAndNotNull() && changedCommands.NotEmptyAndNotNull()) + if (globalCommandsCreateList!.NotEmptyAndNotNull() && unchangedCommands!.NotEmptyAndNotNull() && changedCommands!.NotEmptyAndNotNull()) { client.Logger.Log(ApplicationCommandsExtension.ApplicationCommandsLogLevel, "[AC GLOBAL] Creating, re-using and overwriting application commands"); @@ -60,7 +60,7 @@ internal static async Task> RegisterGlobalComman commands.AddRange(unchangedCommands); } - else if (globalCommandsCreateList.NotEmptyAndNotNull() && (unchangedCommands.NotEmptyAndNotNull() || changedCommands.NotEmptyAndNotNull())) + else if (globalCommandsCreateList!.NotEmptyAndNotNull() && (unchangedCommands!.NotEmptyAndNotNull() || changedCommands!.NotEmptyAndNotNull())) { client.Logger.Log(ApplicationCommandsExtension.ApplicationCommandsLogLevel, "[AC GLOBAL] Creating, re-using and overwriting application commands"); @@ -70,7 +70,7 @@ internal static async Task> RegisterGlobalComman commands.Add(discordBackendCommand); } - if (changedCommands.NotEmptyAndNotNull()) + if (changedCommands!.NotEmptyAndNotNull()) foreach (var (key, command) in changedCommands) { var discordBackendCommand = await client.EditGlobalApplicationCommandAsync(key, action => @@ -91,10 +91,10 @@ internal static async Task> RegisterGlobalComman commands.Add(discordBackendCommand); } - if (unchangedCommands.NotEmptyAndNotNull()) + if (unchangedCommands!.NotEmptyAndNotNull()) commands.AddRange(unchangedCommands); } - else if (globalCommandsCreateList.EmptyOrNull() && unchangedCommands.NotEmptyAndNotNull() && changedCommands.NotEmptyAndNotNull()) + else if (globalCommandsCreateList!.EmptyOrNull() && unchangedCommands!.NotEmptyAndNotNull() && changedCommands!.NotEmptyAndNotNull()) { client.Logger.Log(ApplicationCommandsExtension.ApplicationCommandsLogLevel, "[AC GLOBAL] Editing & re-using application commands"); @@ -120,7 +120,7 @@ internal static async Task> RegisterGlobalComman commands.AddRange(unchangedCommands); } - else if (globalCommandsCreateList.EmptyOrNull() && changedCommands.NotEmptyAndNotNull() && unchangedCommands.EmptyOrNull()) + else if (globalCommandsCreateList!.EmptyOrNull() && changedCommands!.NotEmptyAndNotNull() && unchangedCommands!.EmptyOrNull()) { client.Logger.Log(ApplicationCommandsExtension.ApplicationCommandsLogLevel, "[AC GLOBAL] Overwriting all application commands"); @@ -134,14 +134,14 @@ internal static async Task> RegisterGlobalComman var discordBackendCommands = await client.BulkOverwriteGlobalApplicationCommandsAsync(overwriteList).ConfigureAwait(false); commands.AddRange(discordBackendCommands); } - else if (globalCommandsCreateList.NotEmptyAndNotNull() && changedCommands.EmptyOrNull() && unchangedCommands.EmptyOrNull()) + else if (globalCommandsCreateList!.NotEmptyAndNotNull() && changedCommands!.EmptyOrNull() && unchangedCommands!.EmptyOrNull()) { client.Logger.Log(ApplicationCommandsExtension.ApplicationCommandsLogLevel, "[AC GLOBAL] Creating all application commands"); var cmds = await client.BulkOverwriteGlobalApplicationCommandsAsync(globalCommandsCreateList).ConfigureAwait(false); commands.AddRange(cmds); } - else if (globalCommandsCreateList.EmptyOrNull() && changedCommands.EmptyOrNull() && unchangedCommands.NotEmptyAndNotNull()) + else if (globalCommandsCreateList!.EmptyOrNull() && changedCommands!.EmptyOrNull() && unchangedCommands!.NotEmptyAndNotNull()) { client.Logger.Log(ApplicationCommandsExtension.ApplicationCommandsLogLevel, "[AC GLOBAL] Re-using all application commands"); @@ -149,7 +149,7 @@ internal static async Task> RegisterGlobalComman } if (!globalCommandsDeleteList.NotEmptyAndNotNull()) - return commands.NotEmptyAndNotNull() ? commands : null; + return commands.NotEmpty() ? commands : null; client.Logger.Log(ApplicationCommandsExtension.ApplicationCommandsLogLevel, "[AC GLOBAL] Deleting missing application commands"); @@ -163,7 +163,7 @@ internal static async Task> RegisterGlobalComman client.Logger.LogError("Could not delete global command {cmd}. Please clean up manually", cmdId); } - return commands.NotEmptyAndNotNull() ? commands : null; + return commands.NotEmpty() ? commands : null; } /// @@ -173,13 +173,13 @@ internal static async Task> RegisterGlobalComman /// The target guild id. /// The command list. /// A list of registered commands. - internal static async Task> RegisterGuildCommandsAsync(DiscordClient client, ulong guildId, List commands) + internal static async Task?> RegisterGuildCommandsAsync(DiscordClient client, ulong guildId, List commands) { var (changedCommands, unchangedCommands) = BuildGuildOverwriteList(client, guildId, commands); var guildCommandsCreateList = BuildGuildCreateList(client, guildId, commands); var guildCommandsDeleteList = BuildGuildDeleteList(client, guildId, commands); - if (guildCommandsCreateList.NotEmptyAndNotNull() && unchangedCommands.NotEmptyAndNotNull() && changedCommands.NotEmptyAndNotNull()) + if (guildCommandsCreateList!.NotEmptyAndNotNull() && unchangedCommands!.NotEmptyAndNotNull() && changedCommands!.NotEmptyAndNotNull()) { client.Logger.Log(ApplicationCommandsExtension.ApplicationCommandsLogLevel, "[AC GUILD] Creating, re-using and overwriting application commands. Guild ID: {guild}", guildId); @@ -211,7 +211,7 @@ internal static async Task> RegisterGuildCommand commands.AddRange(unchangedCommands); } - else if (guildCommandsCreateList.NotEmptyAndNotNull() && (unchangedCommands.NotEmptyAndNotNull() || changedCommands.NotEmptyAndNotNull())) + else if (guildCommandsCreateList!.NotEmptyAndNotNull() && (unchangedCommands!.NotEmptyAndNotNull() || changedCommands!.NotEmptyAndNotNull())) { client.Logger.Log(ApplicationCommandsExtension.ApplicationCommandsLogLevel, "[AC GUILD] Creating, re-using and overwriting application commands. Guild ID: {guild}", guildId); @@ -221,7 +221,7 @@ internal static async Task> RegisterGuildCommand commands.Add(discordBackendCommand); } - if (changedCommands.NotEmptyAndNotNull()) + if (changedCommands!.NotEmptyAndNotNull()) foreach (var (key, command) in changedCommands) { var discordBackendCommand = await client.EditGuildApplicationCommandAsync(guildId, key, action => @@ -242,10 +242,10 @@ internal static async Task> RegisterGuildCommand commands.Add(discordBackendCommand); } - if (unchangedCommands.NotEmptyAndNotNull()) + if (unchangedCommands!.NotEmptyAndNotNull()) commands.AddRange(unchangedCommands); } - else if (guildCommandsCreateList.EmptyOrNull() && unchangedCommands.NotEmptyAndNotNull() && changedCommands.NotEmptyAndNotNull()) + else if (guildCommandsCreateList!.EmptyOrNull() && unchangedCommands!.NotEmptyAndNotNull() && changedCommands!.NotEmptyAndNotNull()) { client.Logger.Log(ApplicationCommandsExtension.ApplicationCommandsLogLevel, "[AC GUILD] Editing & re-using application commands. Guild ID: {guild}", guildId); @@ -273,7 +273,7 @@ internal static async Task> RegisterGuildCommand commands.AddRange(unchangedCommands); } - else if (guildCommandsCreateList.EmptyOrNull() && changedCommands.NotEmptyAndNotNull() && unchangedCommands.EmptyOrNull()) + else if (guildCommandsCreateList!.EmptyOrNull() && changedCommands!.NotEmptyAndNotNull() && unchangedCommands!.EmptyOrNull()) { client.Logger.Log(ApplicationCommandsExtension.ApplicationCommandsLogLevel, "[AC GUILD] Overwriting all application commands. Guild ID: {guild}", guildId); @@ -287,14 +287,14 @@ internal static async Task> RegisterGuildCommand var discordBackendCommands = await client.BulkOverwriteGuildApplicationCommandsAsync(guildId, overwriteList).ConfigureAwait(false); commands.AddRange(discordBackendCommands); } - else if (guildCommandsCreateList.NotEmptyAndNotNull() && changedCommands.EmptyOrNull() && unchangedCommands.EmptyOrNull()) + else if (guildCommandsCreateList!.NotEmptyAndNotNull() && changedCommands!.EmptyOrNull() && unchangedCommands!.EmptyOrNull()) { client.Logger.Log(ApplicationCommandsExtension.ApplicationCommandsLogLevel, "[AC GUILD] Creating all application commands. Guild ID: {guild}", guildId); var cmds = await client.BulkOverwriteGuildApplicationCommandsAsync(guildId, guildCommandsCreateList).ConfigureAwait(false); commands.AddRange(cmds); } - else if (guildCommandsCreateList.EmptyOrNull() && changedCommands.EmptyOrNull() && unchangedCommands.NotEmptyAndNotNull()) + else if (guildCommandsCreateList!.EmptyOrNull() && changedCommands!.EmptyOrNull() && unchangedCommands!.NotEmptyAndNotNull()) { client.Logger.Log(ApplicationCommandsExtension.ApplicationCommandsLogLevel, "[AC GUILD] Re-using all application commands Guild ID: {guild}", guildId); @@ -302,7 +302,7 @@ internal static async Task> RegisterGuildCommand } if (!guildCommandsDeleteList.NotEmptyAndNotNull()) - return commands.NotEmptyAndNotNull() ? commands : null; + return commands.NotEmpty() ? commands : null; foreach (var cmdId in guildCommandsDeleteList) { @@ -317,7 +317,7 @@ internal static async Task> RegisterGuildCommand } } - return commands.NotEmptyAndNotNull() ? commands : null; + return commands.NotEmpty() ? commands : null; } /// @@ -329,8 +329,8 @@ internal static async Task> RegisterGuildCommand /// A list of command ids. private static List? BuildGuildDeleteList(DiscordClient client, ulong guildId, List? updateList = null) { - if (ApplicationCommandsExtension.GuildDiscordCommands is null || ApplicationCommandsExtension.GuildDiscordCommands.Count is 0 - || !ApplicationCommandsExtension.GuildDiscordCommands.TryGetFirstValueByKey(guildId, out var discord) + if (ApplicationCommandsExtension.GuildDiscordCommands.Count is 0 + || !ApplicationCommandsExtension.GuildDiscordCommands!.TryGetFirstValueByKey(guildId, out var discord) ) return null; @@ -356,8 +356,8 @@ internal static async Task> RegisterGuildCommand /// private static List? BuildGuildCreateList(DiscordClient client, ulong guildId, List? updateList = null) { - if (ApplicationCommandsExtension.GuildDiscordCommands is null || ApplicationCommandsExtension.GuildDiscordCommands.Count is 0 - || updateList is null || !ApplicationCommandsExtension.GuildDiscordCommands.TryGetFirstValueByKey(guildId, out var discord) + if (ApplicationCommandsExtension.GuildDiscordCommands.Count is 0 + || updateList is null || !ApplicationCommandsExtension.GuildDiscordCommands!.TryGetFirstValueByKey(guildId, out var discord) ) return null; @@ -383,9 +383,9 @@ private static ( List? unchangedCommands ) BuildGuildOverwriteList(DiscordClient client, ulong guildId, List? updateList = null) { - if (ApplicationCommandsExtension.GuildDiscordCommands is null || ApplicationCommandsExtension.GuildDiscordCommands.Count is 0 - || ApplicationCommandsExtension.GuildDiscordCommands.All(l => l.Key != guildId) || updateList is null - || !ApplicationCommandsExtension.GuildDiscordCommands.TryGetFirstValueByKey(guildId, out var discord) + if (ApplicationCommandsExtension.GuildDiscordCommands.Count is 0 + || ApplicationCommandsExtension.GuildDiscordCommands.All(l => l.Key != guildId) || updateList is null + || !ApplicationCommandsExtension.GuildDiscordCommands!.TryGetFirstValueByKey(guildId, out var discord) ) return (null, null); @@ -396,7 +396,7 @@ private static ( return (null, null); foreach (var cmd in updateList) - if (discord.TryGetFirstValueWhere(d => d!.Name == cmd.Name, out var command)) + if (discord!.TryGetFirstValueWhere(d => d?.Name == cmd.Name, out var command)) if (command.IsEqualTo(cmd, client, true)) { if (ApplicationCommandsExtension.DebugEnabled) @@ -424,7 +424,7 @@ private static ( /// A list of command ids. private static List? BuildGlobalDeleteList(DiscordClient client, List? updateList = null) { - if (ApplicationCommandsExtension.GlobalDiscordCommands is null || ApplicationCommandsExtension.GlobalDiscordCommands.Count is 0) + if (ApplicationCommandsExtension.GlobalDiscordCommands.Count is 0) return null; var discord = ApplicationCommandsExtension.GlobalDiscordCommands; @@ -450,7 +450,7 @@ private static ( /// A list of commands. private static List? BuildGlobalCreateList(DiscordClient client, List? updateList = null) { - if (ApplicationCommandsExtension.GlobalDiscordCommands is null || ApplicationCommandsExtension.GlobalDiscordCommands.Count is 0 || updateList is null) + if (ApplicationCommandsExtension.GlobalDiscordCommands.Count is 0 || updateList is null) return updateList; var discord = ApplicationCommandsExtension.GlobalDiscordCommands; @@ -476,7 +476,7 @@ private static ( List? unchangedCommands ) BuildGlobalOverwriteList(DiscordClient client, List? updateList = null) { - if (ApplicationCommandsExtension.GlobalDiscordCommands is null || ApplicationCommandsExtension.GlobalDiscordCommands.Count is 0 || updateList is null) + if (ApplicationCommandsExtension.GlobalDiscordCommands.Count is 0 || updateList is null) return (null, null); var discord = ApplicationCommandsExtension.GlobalDiscordCommands; @@ -484,10 +484,10 @@ private static ( if (discord is null) return (null, null); - Dictionary updateCommands = []; + Dictionary changedCommands = []; List unchangedCommands = []; foreach (var cmd in updateList) - if (discord.TryGetFirstValueWhere(d => d.Name == cmd.Name, out var command)) + if (discord!.TryGetFirstValueWhere(d => d?.Name == cmd.Name, out var command)) if (command.IsEqualTo(cmd, client, false)) { if (ApplicationCommandsExtension.DebugEnabled) @@ -501,9 +501,9 @@ private static ( { if (ApplicationCommandsExtension.DebugEnabled) client.Logger.LogDebug("[AC] Command {cmdName} changed", cmd.Name); - updateCommands.Add(command.Id, cmd); + changedCommands.Add(command.Id, cmd); } - return (updateCommands, unchangedCommands); + return (changedCommands, unchangedCommands); } } diff --git a/DisCatSharp.Common/Types/LinqMethods.cs b/DisCatSharp.Common/Types/LinqMethods.cs index 094e74b6e8..467eacd9da 100644 --- a/DisCatSharp.Common/Types/LinqMethods.cs +++ b/DisCatSharp.Common/Types/LinqMethods.cs @@ -18,8 +18,9 @@ public static class LinqMethods /// The predicate. /// The value to get if succeeded /// Whether a value was found. - public static bool TryGetFirstValueWhere(this List list, Func predicate, [NotNullWhen(true)] out TSource? value) + public static bool TryGetFirstValueWhere(this List? list, Func predicate, [NotNullWhen(true)] out TSource? value) { + ArgumentNullException.ThrowIfNull(predicate); if (list.EmptyOrNull()) { value = default; @@ -43,7 +44,7 @@ public static bool TryGetFirstValueWhere(this List list, Func public static bool TryGetFirstValueByKey(this Dictionary? dict, TKey key, [NotNullWhen(true)] out TValue? value) where TKey : notnull { - if (dict != null) + if (dict is not null) return dict.TryGetValue(key, out value); value = default; diff --git a/DisCatSharp.Common/Utilities/EnsureObjectStates.cs b/DisCatSharp.Common/Utilities/EnsureObjectStates.cs index a21ccf35d7..9d1d3a990d 100644 --- a/DisCatSharp.Common/Utilities/EnsureObjectStates.cs +++ b/DisCatSharp.Common/Utilities/EnsureObjectStates.cs @@ -46,4 +46,42 @@ public static bool EmptyOrNull([NotNullWhen(false)] this List? list) /// True if satisfied, false otherwise. public static bool NotEmptyAndNotNull([NotNullWhen(true)] this List? list) => list is not null && list.Count is not 0; + + /// + /// Checks whether the dictionary is null or empty. + /// + /// Any key type. + /// Any value type. + /// The dictionary to check on. + /// True if satisfied, false otherwise. + public static bool Empty(this Dictionary dictionary) where T1 : notnull + => dictionary.Count is 0 || dictionary.Keys.Count is 0; + + /// + /// Checks whether the dictionary is not null and not empty. + /// + /// Any key type. + /// Any value type. + /// The dictionary to check on. + /// True if satisfied, false otherwise. + public static bool NotEmpty(this Dictionary dictionary) where T1 : notnull + => dictionary.Count is not 0 && dictionary.Keys.Count is not 0; + + /// + /// Checks whether the list is null or empty. + /// + /// Any value type. + /// The list to check on. + /// True if satisfied, false otherwise. + public static bool Empty(this List list) where T : notnull + => list.Count is 0; + + /// + /// Checks whether the list is not null and not empty. + /// + /// Any value type. + /// The list to check on. + /// True if satisfied, false otherwise. + public static bool NotEmpty(this List list) where T : notnull + => list.Count is not 0; }