Skip to content

Commit

Permalink
fix: fix app command module
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulalaby committed Dec 24, 2023
1 parent 1d6ca0d commit eacc4b3
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -765,9 +765,12 @@ private async Task RegisterCommands(List<ApplicationCommandsModuleConfiguration>
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)
Expand Down
74 changes: 37 additions & 37 deletions DisCatSharp.ApplicationCommands/Workers/RegistrationWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ internal class RegistrationWorker
/// <param name="client">The discord client.</param>
/// <param name="commands">The command list.</param>
/// <returns>A list of registered commands.</returns>
internal static async Task<List<DiscordApplicationCommand>> RegisterGlobalCommandsAsync(DiscordClient client, List<DiscordApplicationCommand> commands)
internal static async Task<List<DiscordApplicationCommand>?> RegisterGlobalCommandsAsync(DiscordClient client, List<DiscordApplicationCommand> 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");

Expand Down Expand Up @@ -60,7 +60,7 @@ internal static async Task<List<DiscordApplicationCommand>> 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");

Expand All @@ -70,7 +70,7 @@ internal static async Task<List<DiscordApplicationCommand>> RegisterGlobalComman
commands.Add(discordBackendCommand);
}

if (changedCommands.NotEmptyAndNotNull())
if (changedCommands!.NotEmptyAndNotNull())
foreach (var (key, command) in changedCommands)
{
var discordBackendCommand = await client.EditGlobalApplicationCommandAsync(key, action =>
Expand All @@ -91,10 +91,10 @@ internal static async Task<List<DiscordApplicationCommand>> 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");

Expand All @@ -120,7 +120,7 @@ internal static async Task<List<DiscordApplicationCommand>> 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");

Expand All @@ -134,22 +134,22 @@ internal static async Task<List<DiscordApplicationCommand>> 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");

commands.AddRange(unchangedCommands);
}

if (!globalCommandsDeleteList.NotEmptyAndNotNull())
return commands.NotEmptyAndNotNull() ? commands : null;
return commands.NotEmpty() ? commands : null;

client.Logger.Log(ApplicationCommandsExtension.ApplicationCommandsLogLevel, "[AC GLOBAL] Deleting missing application commands");

Expand All @@ -163,7 +163,7 @@ internal static async Task<List<DiscordApplicationCommand>> 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;
}

/// <summary>
Expand All @@ -173,13 +173,13 @@ internal static async Task<List<DiscordApplicationCommand>> RegisterGlobalComman
/// <param name="guildId">The target guild id.</param>
/// <param name="commands">The command list.</param>
/// <returns>A list of registered commands.</returns>
internal static async Task<List<DiscordApplicationCommand>> RegisterGuildCommandsAsync(DiscordClient client, ulong guildId, List<DiscordApplicationCommand> commands)
internal static async Task<List<DiscordApplicationCommand>?> RegisterGuildCommandsAsync(DiscordClient client, ulong guildId, List<DiscordApplicationCommand> 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);

Expand Down Expand Up @@ -211,7 +211,7 @@ internal static async Task<List<DiscordApplicationCommand>> 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);

Expand All @@ -221,7 +221,7 @@ internal static async Task<List<DiscordApplicationCommand>> RegisterGuildCommand
commands.Add(discordBackendCommand);
}

if (changedCommands.NotEmptyAndNotNull())
if (changedCommands!.NotEmptyAndNotNull())
foreach (var (key, command) in changedCommands)
{
var discordBackendCommand = await client.EditGuildApplicationCommandAsync(guildId, key, action =>
Expand All @@ -242,10 +242,10 @@ internal static async Task<List<DiscordApplicationCommand>> 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);

Expand Down Expand Up @@ -273,7 +273,7 @@ internal static async Task<List<DiscordApplicationCommand>> 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);

Expand All @@ -287,22 +287,22 @@ internal static async Task<List<DiscordApplicationCommand>> 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);

commands.AddRange(unchangedCommands);
}

if (!guildCommandsDeleteList.NotEmptyAndNotNull())
return commands.NotEmptyAndNotNull() ? commands : null;
return commands.NotEmpty() ? commands : null;

foreach (var cmdId in guildCommandsDeleteList)
{
Expand All @@ -317,7 +317,7 @@ internal static async Task<List<DiscordApplicationCommand>> RegisterGuildCommand
}
}

return commands.NotEmptyAndNotNull() ? commands : null;
return commands.NotEmpty() ? commands : null;
}

/// <summary>
Expand All @@ -329,8 +329,8 @@ internal static async Task<List<DiscordApplicationCommand>> RegisterGuildCommand
/// <returns>A list of command ids.</returns>
private static List<ulong>? BuildGuildDeleteList(DiscordClient client, ulong guildId, List<DiscordApplicationCommand>? 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;

Expand All @@ -356,8 +356,8 @@ internal static async Task<List<DiscordApplicationCommand>> RegisterGuildCommand
/// <returns></returns>
private static List<DiscordApplicationCommand>? BuildGuildCreateList(DiscordClient client, ulong guildId, List<DiscordApplicationCommand>? 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;

Expand All @@ -383,9 +383,9 @@ private static (
List<DiscordApplicationCommand>? unchangedCommands
) BuildGuildOverwriteList(DiscordClient client, ulong guildId, List<DiscordApplicationCommand>? 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);

Expand All @@ -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)
Expand Down Expand Up @@ -424,7 +424,7 @@ private static (
/// <returns>A list of command ids.</returns>
private static List<ulong>? BuildGlobalDeleteList(DiscordClient client, List<DiscordApplicationCommand>? updateList = null)
{
if (ApplicationCommandsExtension.GlobalDiscordCommands is null || ApplicationCommandsExtension.GlobalDiscordCommands.Count is 0)
if (ApplicationCommandsExtension.GlobalDiscordCommands.Count is 0)
return null;

var discord = ApplicationCommandsExtension.GlobalDiscordCommands;
Expand All @@ -450,7 +450,7 @@ private static (
/// <returns>A list of commands.</returns>
private static List<DiscordApplicationCommand>? BuildGlobalCreateList(DiscordClient client, List<DiscordApplicationCommand>? 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;
Expand All @@ -476,18 +476,18 @@ private static (
List<DiscordApplicationCommand>? unchangedCommands
) BuildGlobalOverwriteList(DiscordClient client, List<DiscordApplicationCommand>? 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;

if (discord is null)
return (null, null);

Dictionary<ulong, DiscordApplicationCommand> updateCommands = [];
Dictionary<ulong, DiscordApplicationCommand> changedCommands = [];
List<DiscordApplicationCommand> 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)
Expand All @@ -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);
}
}
5 changes: 3 additions & 2 deletions DisCatSharp.Common/Types/LinqMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ public static class LinqMethods
/// <param name="predicate">The predicate.</param>
/// <param name="value">The value to get if succeeded</param>
/// <returns>Whether a value was found.</returns>
public static bool TryGetFirstValueWhere<TSource>(this List<TSource?> list, Func<TSource?, bool> predicate, [NotNullWhen(true)] out TSource? value)
public static bool TryGetFirstValueWhere<TSource>(this List<TSource?>? list, Func<TSource?, bool> predicate, [NotNullWhen(true)] out TSource? value)
{
ArgumentNullException.ThrowIfNull(predicate);
if (list.EmptyOrNull())
{
value = default;
Expand All @@ -43,7 +44,7 @@ public static bool TryGetFirstValueWhere<TSource>(this List<TSource?> list, Func
public static bool TryGetFirstValueByKey<TKey, TValue>(this Dictionary<TKey, TValue?>? 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;
Expand Down
38 changes: 38 additions & 0 deletions DisCatSharp.Common/Utilities/EnsureObjectStates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,42 @@ public static bool EmptyOrNull<T>([NotNullWhen(false)] this List<T?>? list)
/// <returns>True if satisfied, false otherwise.</returns>
public static bool NotEmptyAndNotNull<T>([NotNullWhen(true)] this List<T?>? list)
=> list is not null && list.Count is not 0;

/// <summary>
/// Checks whether the dictionary is null or empty.
/// </summary>
/// <typeparam name="T1">Any key type.</typeparam>
/// <typeparam name="T2">Any value type.</typeparam>
/// <param name="dictionary">The dictionary to check on.</param>
/// <returns>True if satisfied, false otherwise.</returns>
public static bool Empty<T1, T2>(this Dictionary<T1, T2?> dictionary) where T1 : notnull
=> dictionary.Count is 0 || dictionary.Keys.Count is 0;

/// <summary>
/// Checks whether the dictionary is not null and not empty.
/// </summary>
/// <typeparam name="T1">Any key type.</typeparam>
/// <typeparam name="T2">Any value type.</typeparam>
/// <param name="dictionary">The dictionary to check on.</param>
/// <returns>True if satisfied, false otherwise.</returns>
public static bool NotEmpty<T1, T2>(this Dictionary<T1, T2?> dictionary) where T1 : notnull
=> dictionary.Count is not 0 && dictionary.Keys.Count is not 0;

/// <summary>
/// Checks whether the list is null or empty.
/// </summary>
/// <typeparam name="T">Any value type.</typeparam>
/// <param name="list">The list to check on.</param>
/// <returns>True if satisfied, false otherwise.</returns>
public static bool Empty<T>(this List<T> list) where T : notnull
=> list.Count is 0;

/// <summary>
/// Checks whether the list is not null and not empty.
/// </summary>
/// <typeparam name="T">Any value type.</typeparam>
/// <param name="list">The list to check on.</param>
/// <returns>True if satisfied, false otherwise.</returns>
public static bool NotEmpty<T>(this List<T> list) where T : notnull
=> list.Count is not 0;
}

0 comments on commit eacc4b3

Please sign in to comment.