Skip to content

Commit

Permalink
chore: improving app command module - fun ™️
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulalaby committed Dec 22, 2023
1 parent 77844bd commit aacb173
Show file tree
Hide file tree
Showing 25 changed files with 539 additions and 528 deletions.
533 changes: 282 additions & 251 deletions DisCatSharp.ApplicationCommands/ApplicationCommandsExtension.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

namespace DisCatSharp.ApplicationCommands;

/// <summary>
/// Provides a set of utility methods for application commands.
/// </summary>
public static class ApplicationCommandsUtilities
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,8 @@ bool isGuild
}
else
{
sourceApplicationCommand.IntegrationTypes ??=
[
ApplicationCommandIntegrationTypes.GuildInstall
];
targetApplicationCommand.IntegrationTypes ??=
[
ApplicationCommandIntegrationTypes.GuildInstall
];
sourceApplicationCommand.IntegrationTypes ??= [ApplicationCommandIntegrationTypes.GuildInstall];
targetApplicationCommand.IntegrationTypes ??= [ApplicationCommandIntegrationTypes.GuildInstall];
}

client.Logger.Log(ApplicationCommandsExtension.ApplicationCommandsLogLevel,
Expand Down Expand Up @@ -149,11 +143,9 @@ internal static bool SoftEqual(
/// <param name="target">The target enumerable.</param>
/// <returns>Whether both nullable enumerable are equal.</returns>
internal static bool NullableSequenceEqual<T>(this List<T>? source, List<T>? target)
{
return source is not null && target is not null
=> source is not null && target is not null
? source.All(target.Contains) && source.Count == target.Count
: source is null && target is null;
}

/// <summary>
/// Checks whether two dictionaries are equal.
Expand All @@ -169,6 +161,9 @@ internal static bool AreDictionariesEqual(this Dictionary<string, string> source
if (sourceDictionary is null && targetDictionary is null)
return true;

if (sourceDictionary is null || targetDictionary is null)
return false;

foreach (var kvp in sourceDictionary)
if (!targetDictionary.TryGetValue(kvp.Key, out var value) || value != kvp.Value)
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace DisCatSharp.ApplicationCommands.Context;
/// <summary>
/// The application commands translation context.
/// </summary>
public class ApplicationCommandsTranslationContext
public sealed class ApplicationCommandsTranslationContext
{
/// <summary>
/// Gets the type.
Expand Down Expand Up @@ -38,9 +38,17 @@ internal ApplicationCommandsTranslationContext(Type type, string name)
this.Name = name;
}

/// <summary>
/// Adds the group translation.
/// </summary>
/// <param name="translationJson"></param>
public void AddGroupTranslation(string translationJson)
=> this.GroupTranslations = translationJson;

/// <summary>
/// Adds the single translation.
/// </summary>
/// <param name="translationJson"></param>
public void AddSingleTranslation(string translationJson)
=> this.SingleTranslations = translationJson;
}
10 changes: 5 additions & 5 deletions DisCatSharp.ApplicationCommands/Context/AutocompleteContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace DisCatSharp.ApplicationCommands.Context;
/// <summary>
/// Represents a context for an autocomplete interaction.
/// </summary>
public class AutocompleteContext
public sealed class AutocompleteContext
{
/// <summary>
/// The interaction created.
Expand All @@ -22,12 +22,12 @@ public class AutocompleteContext
/// <summary>
/// Gets the client for this interaction.
/// </summary>
public DiscordClient Client { get; internal set; }
public DiscordClient Client { get; internal init; }

/// <summary>
/// Gets the guild this interaction was executed in.
/// </summary>
public DiscordGuild Guild { get; internal set; }
public DiscordGuild Guild { get; internal init; }

/// <summary>
/// Gets the channel this interaction was executed in.
Expand All @@ -37,7 +37,7 @@ public class AutocompleteContext
/// <summary>
/// Gets the user which executed this interaction.
/// </summary>
public DiscordUser User { get; internal set; }
public DiscordUser User { get; internal init; }

/// <summary>
/// Gets the member which executed this interaction, or null if the command is in a DM.
Expand Down Expand Up @@ -91,7 +91,7 @@ public DiscordMember Member
/// <summary>
/// The options already provided.
/// </summary>
public IReadOnlyList<DiscordInteractionDataOption> Options { get; internal set; }
public IReadOnlyList<DiscordInteractionDataOption> Options { get; internal init; }

/// <summary>
/// The option to autocomplete.
Expand Down
12 changes: 6 additions & 6 deletions DisCatSharp.ApplicationCommands/Context/BaseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ public class BaseContext
/// <summary>
/// Gets the interaction that was created.
/// </summary>
public DiscordInteraction Interaction { get; internal set; }
public DiscordInteraction Interaction { get; internal init; }

/// <summary>
/// Gets the client for this interaction.
/// </summary>
public DiscordClient Client { get; internal set; }
public DiscordClient Client { get; internal init; }

/// <summary>
/// Gets the guild this interaction was executed in.
/// </summary>
public DiscordGuild Guild { get; internal set; }
public DiscordGuild Guild { get; internal init; }

/// <summary>
/// Gets the channel this interaction was executed in.
/// </summary>
public DiscordChannel Channel { get; internal set; }
public DiscordChannel Channel { get; internal init; }

/// <summary>
/// Gets the user which executed this interaction.
/// </summary>
public DiscordUser User { get; internal set; }
public DiscordUser User { get; internal init; }

/// <summary>
/// Gets the member which executed this interaction, or null if the command is in a DM.
Expand All @@ -64,7 +64,7 @@ public DiscordMember Member
/// <summary>
/// Gets the name of the command.
/// </summary>
public string CommandName { get; internal set; }
public string CommandName { get; internal init; }

/// <summary>
/// Gets the name of the sub command.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public sealed class ContextMenuContext : BaseContext
/// <summary>
/// The user this command targets, if applicable.
/// </summary>
public DiscordUser TargetUser { get; internal set; }
public DiscordUser TargetUser { get; internal init; }

/// <summary>
/// The member this command targets, if applicable.
Expand Down
8 changes: 4 additions & 4 deletions DisCatSharp.ApplicationCommands/Context/InteractionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ public sealed class InteractionContext : BaseContext
/// <summary>
/// Gets the users mentioned in the command parameters.
/// </summary>
public IReadOnlyList<DiscordUser> ResolvedUserMentions { get; internal set; }
public IReadOnlyList<DiscordUser> ResolvedUserMentions { get; internal set; } = [];

/// <summary>
/// Gets the roles mentioned in the command parameters.
/// </summary>
public IReadOnlyList<DiscordRole> ResolvedRoleMentions { get; internal set; }
public IReadOnlyList<DiscordRole> ResolvedRoleMentions { get; internal set; } = [];

/// <summary>
/// Gets the channels mentioned in the command parameters.
/// </summary>
public IReadOnlyList<DiscordChannel> ResolvedChannelMentions { get; internal set; }
public IReadOnlyList<DiscordChannel> ResolvedChannelMentions { get; internal set; } = [];

/// <summary>
/// Gets the attachments in the command parameters, if applicable.
/// </summary>
public IReadOnlyList<DiscordAttachment> ResolvedAttachments { get; internal set; }
public IReadOnlyList<DiscordAttachment> ResolvedAttachments { get; internal set; } = [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace DisCatSharp.ApplicationCommands.Entities;
/// <summary>
/// Represents a choice translator.
/// </summary>
internal class ChoiceTranslator
internal sealed class ChoiceTranslator
{
/// <summary>
/// Gets the choice name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace DisCatSharp.ApplicationCommands.Entities;
/// <summary>
/// Represents a command translator.
/// </summary>
internal class CommandTranslator
internal sealed class CommandTranslator
{
/// <summary>
/// Gets the command name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace DisCatSharp.ApplicationCommands.Entities;

internal class CommandGroupWithSubGroups : BaseCommand
internal sealed class CommandGroupWithSubGroups : BaseCommand
{
[JsonProperty("groups")]
internal List<CommandGroup> SubGroups { get; set; }
Expand All @@ -19,7 +19,7 @@ internal CommandGroupWithSubGroups(string name, string description, List<Command
}
}

internal class CommandGroup : BaseCommand
internal sealed class CommandGroup : BaseCommand
{
[JsonProperty("commands")]
internal List<Command> Commands { get; set; }
Expand All @@ -31,12 +31,12 @@ internal CommandGroup(string name, string description, List<Command> commands, A
}
}

internal class Command : BaseCommand
internal sealed class Command : BaseCommand
{
[JsonProperty("options")]
internal List<DiscordApplicationCommandOption> Options { get; set; }
internal List<DiscordApplicationCommandOption>? Options { get; set; }

internal Command(string name, string? description = null, List<DiscordApplicationCommandOption> options = null, ApplicationCommandType? type = null)
internal Command(string name, string? description = null, List<DiscordApplicationCommandOption>? options = null, ApplicationCommandType? type = null)
: base(name, description, type)
{
this.Options = options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace DisCatSharp.ApplicationCommands.Entities;
/// <summary>
/// Represents a group translator.
/// </summary>
internal class GroupTranslator
internal sealed class GroupTranslator
{
/// <summary>
/// Gets the group name.
Expand Down
9 changes: 8 additions & 1 deletion DisCatSharp.ApplicationCommands/Entities/OptionTranslator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;

using DisCatSharp.Entities;
using DisCatSharp.Enums;

using Newtonsoft.Json;

Expand All @@ -9,7 +10,7 @@ namespace DisCatSharp.ApplicationCommands.Entities;
/// <summary>
/// Represents a option translator.
/// </summary>
internal class OptionTranslator
internal sealed class OptionTranslator
{
/// <summary>
/// Gets the option name.
Expand All @@ -23,6 +24,12 @@ internal class OptionTranslator
[JsonProperty("description")]
public string Description { get; set; }

/// <summary>
/// Gets the option type
/// </summary>
[JsonProperty("type")]
public ApplicationCommandOptionType? Type { get; set; }

/// <summary>
/// Gets the option name translations.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace DisCatSharp.ApplicationCommands.Entities;
/// <summary>
/// Represents a sub group translator.
/// </summary>
internal class SubGroupTranslator
internal sealed class SubGroupTranslator
{
/// <summary>
/// Gets the sub group name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ namespace DisCatSharp.ApplicationCommands.EventArgs;
/// <summary>
/// Represents arguments for a <see cref="ApplicationCommandsExtension.ApplicationCommandsModuleReady"/> event.
/// </summary>
public class ApplicationCommandsModuleReadyEventArgs : DiscordEventArgs
public sealed class ApplicationCommandsModuleReadyEventArgs : DiscordEventArgs
{
/// <summary>
/// Gets a list of all guild ids missing the application commands scope.
/// </summary>
public IReadOnlyList<ulong> GuildsWithoutScope { get; internal set; }
public IReadOnlyList<ulong> GuildsWithoutScope { get; internal set; } = [];

/// <summary>
/// Initializes a new instance of the <see cref="ApplicationCommandsModuleReadyEventArgs"/> class.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

using DisCatSharp.Entities;
using DisCatSharp.EventArgs;
Expand All @@ -9,7 +10,7 @@ namespace DisCatSharp.ApplicationCommands.EventArgs;
/// <summary>
/// Represents arguments for a <see cref="ApplicationCommandsExtension.ApplicationCommandsModuleStartupFinished"/> event.
/// </summary>
public class ApplicationCommandsModuleStartupFinishedEventArgs : DiscordEventArgs
public sealed class ApplicationCommandsModuleStartupFinishedEventArgs : DiscordEventArgs
{
/// <summary>
/// Gets a list of all guild ids missing the application commands scope.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace DisCatSharp.ApplicationCommands.EventArgs;
/// <summary>
/// Represents arguments for a <see cref="ApplicationCommandsExtension.GlobalApplicationCommandsRegistered"/> event.
/// </summary>
public class GlobalApplicationCommandsRegisteredEventArgs : DiscordEventArgs
public sealed class GlobalApplicationCommandsRegisteredEventArgs : DiscordEventArgs
{
/// <summary>
/// Gets all registered global commands.
/// </summary>
public IReadOnlyList<DiscordApplicationCommand> RegisteredCommands { get; internal set; }
public IReadOnlyList<DiscordApplicationCommand> RegisteredCommands { get; internal set; } = [];

/// <summary>
/// Initializes a new instance of the <see cref="GlobalApplicationCommandsRegisteredEventArgs"/> class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace DisCatSharp.ApplicationCommands.EventArgs;
/// <summary>
/// Represents arguments for a <see cref="ApplicationCommandsExtension.GuildApplicationCommandsRegistered"/> event.
/// </summary>
public class GuildApplicationCommandsRegisteredEventArgs : DiscordEventArgs
public sealed class GuildApplicationCommandsRegisteredEventArgs : DiscordEventArgs
{
/// <summary>
/// Gets the target guild id.
Expand All @@ -19,7 +19,7 @@ public class GuildApplicationCommandsRegisteredEventArgs : DiscordEventArgs
/// <summary>
/// Gets all registered guild commands.
/// </summary>
public IReadOnlyList<DiscordApplicationCommand> RegisteredCommands { get; internal set; }
public IReadOnlyList<DiscordApplicationCommand> RegisteredCommands { get; internal set; } = [];

/// <summary>
/// Initializes a new instance of the <see cref="GuildApplicationCommandsRegisteredEventArgs"/> class.
Expand Down
Loading

0 comments on commit aacb173

Please sign in to comment.