From 1c1ff8c085de5f2e269455cf7ce969d58776a487 Mon Sep 17 00:00:00 2001 From: Misha133 Date: Wed, 14 Dec 2022 13:28:31 +0300 Subject: [PATCH] add `NsfwCommandAttribute` to Interaction Framework --- .../Attributes/NsfwCommandAttribute.cs | 25 +++++++++++++++++++ .../Commands/ContextCommandBuilder.cs | 18 +++++++++++++ .../Builders/Commands/SlashCommandBuilder.cs | 18 +++++++++++++ .../Builders/ModuleBuilder.cs | 5 ++++ .../Builders/ModuleClassBuilder.cs | 9 +++++++ .../ContextCommands/ContextCommandInfo.cs | 4 +++ .../Info/Commands/SlashCommandInfo.cs | 4 +++ .../Info/IApplicationCommandInfo.cs | 5 ++++ .../Info/ModuleInfo.cs | 6 +++++ .../Utilities/ApplicationCommandRestUtil.cs | 8 +++++- 10 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 src/Discord.Net.Interactions/Attributes/NsfwCommandAttribute.cs diff --git a/src/Discord.Net.Interactions/Attributes/NsfwCommandAttribute.cs b/src/Discord.Net.Interactions/Attributes/NsfwCommandAttribute.cs new file mode 100644 index 000000000..b218edb9b --- /dev/null +++ b/src/Discord.Net.Interactions/Attributes/NsfwCommandAttribute.cs @@ -0,0 +1,25 @@ +using System; + +namespace Discord.Interactions +{ + /// + /// Sets the property of an application command or module. + /// + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] + public class NsfwCommandAttribute : Attribute + { + /// + /// Gets whether or not this command is age restricted. + /// + public bool IsNsfw { get; } + + /// + /// Sets the property of an application command or module. + /// + /// Whether or not this command is age restricted. + public NsfwCommandAttribute(bool isNsfw) + { + IsNsfw = isNsfw; + } + } +} diff --git a/src/Discord.Net.Interactions/Builders/Commands/ContextCommandBuilder.cs b/src/Discord.Net.Interactions/Builders/Commands/ContextCommandBuilder.cs index be0e5eb70..ce6d8b504 100644 --- a/src/Discord.Net.Interactions/Builders/Commands/ContextCommandBuilder.cs +++ b/src/Discord.Net.Interactions/Builders/Commands/ContextCommandBuilder.cs @@ -25,6 +25,11 @@ namespace Discord.Interactions.Builders /// public bool IsEnabledInDm { get; set; } = true; + /// + /// Gets whether this command is age restricted. + /// + public bool IsNsfw { get; set; } = false; + /// /// Gets the default permissions needed for executing this command. /// @@ -95,6 +100,19 @@ namespace Discord.Interactions.Builders return this; } + /// + /// Sets . + /// + /// New value of the . + /// + /// The builder instance. + /// + public ContextCommandBuilder SetNsfw(bool isNsfw) + { + IsNsfw = isNsfw; + return this; + } + /// /// Sets . /// diff --git a/src/Discord.Net.Interactions/Builders/Commands/SlashCommandBuilder.cs b/src/Discord.Net.Interactions/Builders/Commands/SlashCommandBuilder.cs index c21fd5ae8..ead6db3cf 100644 --- a/src/Discord.Net.Interactions/Builders/Commands/SlashCommandBuilder.cs +++ b/src/Discord.Net.Interactions/Builders/Commands/SlashCommandBuilder.cs @@ -25,6 +25,11 @@ namespace Discord.Interactions.Builders /// public bool IsEnabledInDm { get; set; } = true; + /// + /// Gets whether this command is age restricted. + /// + public bool IsNsfw { get; set; } = false; + /// /// Gets the default permissions needed for executing this command. /// @@ -95,6 +100,19 @@ namespace Discord.Interactions.Builders return this; } + /// + /// Sets . + /// + /// New value of the . + /// + /// The builder instance. + /// + public SlashCommandBuilder SetNsfw(bool isNsfw) + { + IsNsfw = isNsfw; + return this; + } + /// /// Sets . /// diff --git a/src/Discord.Net.Interactions/Builders/ModuleBuilder.cs b/src/Discord.Net.Interactions/Builders/ModuleBuilder.cs index 0eb91ee6a..306960834 100644 --- a/src/Discord.Net.Interactions/Builders/ModuleBuilder.cs +++ b/src/Discord.Net.Interactions/Builders/ModuleBuilder.cs @@ -59,6 +59,11 @@ namespace Discord.Interactions.Builders /// public bool IsEnabledInDm { get; set; } = true; + /// + /// Gets whether this command is age restricted. + /// + public bool IsNsfw { get; set; } = false; + /// /// Gets the default permissions needed for executing this command. /// diff --git a/src/Discord.Net.Interactions/Builders/ModuleClassBuilder.cs b/src/Discord.Net.Interactions/Builders/ModuleClassBuilder.cs index 35126a674..26c07fa3b 100644 --- a/src/Discord.Net.Interactions/Builders/ModuleClassBuilder.cs +++ b/src/Discord.Net.Interactions/Builders/ModuleClassBuilder.cs @@ -101,6 +101,9 @@ namespace Discord.Interactions.Builders case DontAutoRegisterAttribute dontAutoRegister: builder.DontAutoRegister = true; break; + case NsfwCommandAttribute nsfwCommand: + builder.IsNsfw = nsfwCommand.IsNsfw; + break; default: builder.AddAttributes(attribute); break; @@ -192,6 +195,9 @@ namespace Discord.Interactions.Builders case PreconditionAttribute precondition: builder.WithPreconditions(precondition); break; + case NsfwCommandAttribute nsfwCommand: + builder.SetNsfw(nsfwCommand.IsNsfw); + break; default: builder.WithAttributes(attribute); break; @@ -244,6 +250,9 @@ namespace Discord.Interactions.Builders case PreconditionAttribute precondition: builder.WithPreconditions(precondition); break; + case NsfwCommandAttribute nsfwCommand: + builder.SetNsfw(nsfwCommand.IsNsfw); + break; default: builder.WithAttributes(attribute); break; diff --git a/src/Discord.Net.Interactions/Info/Commands/ContextCommands/ContextCommandInfo.cs b/src/Discord.Net.Interactions/Info/Commands/ContextCommands/ContextCommandInfo.cs index 33b82b127..61f79453c 100644 --- a/src/Discord.Net.Interactions/Info/Commands/ContextCommands/ContextCommandInfo.cs +++ b/src/Discord.Net.Interactions/Info/Commands/ContextCommands/ContextCommandInfo.cs @@ -20,6 +20,9 @@ namespace Discord.Interactions /// public bool IsEnabledInDm { get; } + /// + public bool IsNsfw { get; } + /// public GuildPermission? DefaultMemberPermissions { get; } @@ -37,6 +40,7 @@ namespace Discord.Interactions { CommandType = builder.CommandType; DefaultPermission = builder.DefaultPermission; + IsNsfw = builder.IsNsfw; IsEnabledInDm = builder.IsEnabledInDm; DefaultMemberPermissions = builder.DefaultMemberPermissions; Parameters = builder.Parameters.Select(x => x.Build(this)).ToImmutableArray(); diff --git a/src/Discord.Net.Interactions/Info/Commands/SlashCommandInfo.cs b/src/Discord.Net.Interactions/Info/Commands/SlashCommandInfo.cs index 634fd9643..ee3939702 100644 --- a/src/Discord.Net.Interactions/Info/Commands/SlashCommandInfo.cs +++ b/src/Discord.Net.Interactions/Info/Commands/SlashCommandInfo.cs @@ -29,6 +29,9 @@ namespace Discord.Interactions /// public bool IsEnabledInDm { get; } + /// + public bool IsNsfw { get; } + /// public GuildPermission? DefaultMemberPermissions { get; } @@ -48,6 +51,7 @@ namespace Discord.Interactions Description = builder.Description; DefaultPermission = builder.DefaultPermission; IsEnabledInDm = builder.IsEnabledInDm; + IsNsfw = builder.IsNsfw; DefaultMemberPermissions = builder.DefaultMemberPermissions; Parameters = builder.Parameters.Select(x => x.Build(this)).ToImmutableArray(); FlattenedParameters = FlattenParameters(Parameters).ToImmutableArray(); diff --git a/src/Discord.Net.Interactions/Info/IApplicationCommandInfo.cs b/src/Discord.Net.Interactions/Info/IApplicationCommandInfo.cs index dd1b97899..d5bcdd3a3 100644 --- a/src/Discord.Net.Interactions/Info/IApplicationCommandInfo.cs +++ b/src/Discord.Net.Interactions/Info/IApplicationCommandInfo.cs @@ -28,6 +28,11 @@ namespace Discord.Interactions /// public bool IsEnabledInDm { get; } + /// + /// Gets whether this command can is age restricted. + /// + public bool IsNsfw { get; } + /// /// Gets the default permissions needed for executing this command. /// diff --git a/src/Discord.Net.Interactions/Info/ModuleInfo.cs b/src/Discord.Net.Interactions/Info/ModuleInfo.cs index 4f40f1607..5c4cac587 100644 --- a/src/Discord.Net.Interactions/Info/ModuleInfo.cs +++ b/src/Discord.Net.Interactions/Info/ModuleInfo.cs @@ -49,6 +49,11 @@ namespace Discord.Interactions /// public bool IsEnabledInDm { get; } + /// + /// Gets whether this command is age restricted. + /// + public bool IsNsfw { get; } + /// /// Gets the default permissions needed for executing this command. /// @@ -121,6 +126,7 @@ namespace Discord.Interactions Description = builder.Description; Parent = parent; DefaultPermission = builder.DefaultPermission; + IsNsfw = builder.IsNsfw; IsEnabledInDm = builder.IsEnabledInDm; DefaultMemberPermissions = BuildDefaultMemberPermissions(builder); SlashCommands = BuildSlashCommands(builder).ToImmutableArray(); diff --git a/src/Discord.Net.Interactions/Utilities/ApplicationCommandRestUtil.cs b/src/Discord.Net.Interactions/Utilities/ApplicationCommandRestUtil.cs index 9b507f1bb..ebee5813a 100644 --- a/src/Discord.Net.Interactions/Utilities/ApplicationCommandRestUtil.cs +++ b/src/Discord.Net.Interactions/Utilities/ApplicationCommandRestUtil.cs @@ -54,6 +54,7 @@ namespace Discord.Interactions Description = commandInfo.Description, IsDefaultPermission = commandInfo.DefaultPermission, IsDMEnabled = commandInfo.IsEnabledInDm, + IsNsfw = commandInfo.IsNsfw, DefaultMemberPermissions = ((commandInfo.DefaultMemberPermissions ?? 0) | (commandInfo.Module.DefaultMemberPermissions ?? 0)).SanitizeGuildPermissions(), }.WithNameLocalizations(localizationManager?.GetAllNames(commandPath, LocalizationTarget.Command) ?? ImmutableDictionary.Empty) .WithDescriptionLocalizations(localizationManager?.GetAllDescriptions(commandPath, LocalizationTarget.Command) ?? ImmutableDictionary.Empty) @@ -97,7 +98,8 @@ namespace Discord.Interactions Name = commandInfo.Name, IsDefaultPermission = commandInfo.DefaultPermission, DefaultMemberPermissions = ((commandInfo.DefaultMemberPermissions ?? 0) | (commandInfo.Module.DefaultMemberPermissions ?? 0)).SanitizeGuildPermissions(), - IsDMEnabled = commandInfo.IsEnabledInDm + IsDMEnabled = commandInfo.IsEnabledInDm, + IsNsfw = commandInfo.IsNsfw, } .WithNameLocalizations(localizationManager?.GetAllNames(commandPath, LocalizationTarget.Command) ?? ImmutableDictionary.Empty) .Build(), @@ -106,6 +108,7 @@ namespace Discord.Interactions Name = commandInfo.Name, IsDefaultPermission = commandInfo.DefaultPermission, DefaultMemberPermissions = ((commandInfo.DefaultMemberPermissions ?? 0) | (commandInfo.Module.DefaultMemberPermissions ?? 0)).SanitizeGuildPermissions(), + IsNsfw = commandInfo.IsNsfw, IsDMEnabled = commandInfo.IsEnabledInDm } .WithNameLocalizations(localizationManager?.GetAllNames(commandPath, LocalizationTarget.Command) ?? ImmutableDictionary.Empty) @@ -225,6 +228,7 @@ namespace Discord.Interactions IsDefaultPermission = command.IsDefaultPermission, DefaultMemberPermissions = (GuildPermission)command.DefaultMemberPermissions.RawValue, IsDMEnabled = command.IsEnabledInDm, + IsNsfw = command.IsNsfw, Options = command.Options?.Select(x => x.ToApplicationCommandOptionProps())?.ToList() ?? Optional>.Unspecified, NameLocalizations = command.NameLocalizations?.ToImmutableDictionary() ?? ImmutableDictionary.Empty, DescriptionLocalizations = command.DescriptionLocalizations?.ToImmutableDictionary() ?? ImmutableDictionary.Empty, @@ -234,6 +238,7 @@ namespace Discord.Interactions Name = command.Name, IsDefaultPermission = command.IsDefaultPermission, DefaultMemberPermissions = (GuildPermission)command.DefaultMemberPermissions.RawValue, + IsNsfw = command.IsNsfw, IsDMEnabled = command.IsEnabledInDm, NameLocalizations = command.NameLocalizations?.ToImmutableDictionary() ?? ImmutableDictionary.Empty, DescriptionLocalizations = command.DescriptionLocalizations?.ToImmutableDictionary() ?? ImmutableDictionary.Empty @@ -243,6 +248,7 @@ namespace Discord.Interactions Name = command.Name, IsDefaultPermission = command.IsDefaultPermission, DefaultMemberPermissions = (GuildPermission)command.DefaultMemberPermissions.RawValue, + IsNsfw = command.IsNsfw, IsDMEnabled = command.IsEnabledInDm, NameLocalizations = command.NameLocalizations?.ToImmutableDictionary() ?? ImmutableDictionary.Empty, DescriptionLocalizations = command.DescriptionLocalizations?.ToImmutableDictionary() ?? ImmutableDictionary.Empty