From d6c62335eda529d45b9ed379ea7bd4bb0c431a0e Mon Sep 17 00:00:00 2001 From: Cenk Ergen <57065323+Cenngo@users.noreply.github.com> Date: Sat, 18 Sep 2021 13:20:50 +0300 Subject: [PATCH] added deafult permission option to context commands (#155) --- .../ApplicationCommandProperties.cs | 5 +++ .../Context Menus/MessageCommandBuilder.cs | 19 ++++++++- .../Context Menus/UserCommandBuilder.cs | 19 ++++++++- .../Interactions/SlashCommandProperties.cs | 5 --- .../Interactions/InteractionHelper.cs | 42 ++++++++----------- 5 files changed, 59 insertions(+), 31 deletions(-) diff --git a/src/Discord.Net.Core/Entities/Interactions/ApplicationCommandProperties.cs b/src/Discord.Net.Core/Entities/Interactions/ApplicationCommandProperties.cs index 2ccb0148b..55c91bc64 100644 --- a/src/Discord.Net.Core/Entities/Interactions/ApplicationCommandProperties.cs +++ b/src/Discord.Net.Core/Entities/Interactions/ApplicationCommandProperties.cs @@ -18,6 +18,11 @@ namespace Discord /// public Optional Name { get; set; } + /// + /// Whether the command is enabled by default when the app is added to a guild. Default is + /// + public Optional DefaultPermission { get; set; } + internal ApplicationCommandProperties() { } } } diff --git a/src/Discord.Net.Core/Entities/Interactions/Context Menus/MessageCommandBuilder.cs b/src/Discord.Net.Core/Entities/Interactions/Context Menus/MessageCommandBuilder.cs index 801aca302..ce9c75452 100644 --- a/src/Discord.Net.Core/Entities/Interactions/Context Menus/MessageCommandBuilder.cs +++ b/src/Discord.Net.Core/Entities/Interactions/Context Menus/MessageCommandBuilder.cs @@ -36,6 +36,11 @@ namespace Discord } } + /// + /// Whether the command is enabled by default when the app is added to a guild + /// + public bool DefaultPermission { get; set; } = true; + private string _name { get; set; } /// @@ -49,6 +54,7 @@ namespace Discord MessageCommandProperties props = new MessageCommandProperties() { Name = this.Name, + DefaultPermission = this.DefaultPermission }; return props; @@ -66,6 +72,17 @@ namespace Discord { this.Name = name; return this; - } + } + + /// + /// Sets the default permission of the current command. + /// + /// The default permission value to set. + /// The current builder. + public MessageCommandBuilder WithDefaultPermission (bool value) + { + this.DefaultPermission = value; + return this; + } } } diff --git a/src/Discord.Net.Core/Entities/Interactions/Context Menus/UserCommandBuilder.cs b/src/Discord.Net.Core/Entities/Interactions/Context Menus/UserCommandBuilder.cs index 5629a7014..a49250d08 100644 --- a/src/Discord.Net.Core/Entities/Interactions/Context Menus/UserCommandBuilder.cs +++ b/src/Discord.Net.Core/Entities/Interactions/Context Menus/UserCommandBuilder.cs @@ -36,6 +36,11 @@ namespace Discord } } + /// + /// Whether the command is enabled by default when the app is added to a guild + /// + public bool DefaultPermission { get; set; } = true; + private string _name { get; set; } /// @@ -47,6 +52,7 @@ namespace Discord UserCommandProperties props = new UserCommandProperties() { Name = this.Name, + DefaultPermission = this.DefaultPermission }; return props; @@ -64,6 +70,17 @@ namespace Discord { this.Name = name; return this; - } + } + + /// + /// Sets the default permission of the current command. + /// + /// The default permission value to set. + /// The current builder. + public UserCommandBuilder WithDefaultPermission (bool value) + { + this.DefaultPermission = value; + return this; + } } } diff --git a/src/Discord.Net.Core/Entities/Interactions/SlashCommandProperties.cs b/src/Discord.Net.Core/Entities/Interactions/SlashCommandProperties.cs index 72c7c322b..e9b7222d4 100644 --- a/src/Discord.Net.Core/Entities/Interactions/SlashCommandProperties.cs +++ b/src/Discord.Net.Core/Entities/Interactions/SlashCommandProperties.cs @@ -22,11 +22,6 @@ namespace Discord /// public Optional> Options { get; set; } - /// - /// Whether the command is enabled by default when the app is added to a guild. Default is - /// - public Optional DefaultPermission { get; set; } - internal SlashCommandProperties() { } } } diff --git a/src/Discord.Net.Rest/Entities/Interactions/InteractionHelper.cs b/src/Discord.Net.Rest/Entities/Interactions/InteractionHelper.cs index a6d8c524b..8bc87339e 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/InteractionHelper.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/InteractionHelper.cs @@ -68,6 +68,9 @@ namespace Discord.Rest { Name = arg.Name.Value, Type = arg.Type, + DefaultPermission = arg.DefaultPermission.IsSpecified + ? arg.DefaultPermission.Value + : Optional.Unspecified }; if (arg is SlashCommandProperties slashProps) @@ -79,10 +82,6 @@ namespace Discord.Rest model.Options = slashProps.Options.IsSpecified ? slashProps.Options.Value.Select(x => new Discord.API.ApplicationCommandOption(x)).ToArray() : Optional.Unspecified; - - model.DefaultPermission = slashProps.DefaultPermission.IsSpecified - ? slashProps.DefaultPermission.Value - : Optional.Unspecified; } return await client.ApiClient.CreateGlobalApplicationCommandAsync(model, options).ConfigureAwait(false); @@ -103,6 +102,9 @@ namespace Discord.Rest { Name = arg.Name.Value, Type = arg.Type, + DefaultPermission = arg.DefaultPermission.IsSpecified + ? arg.DefaultPermission.Value + : Optional.Unspecified }; if (arg is SlashCommandProperties slashProps) @@ -114,10 +116,6 @@ namespace Discord.Rest model.Options = slashProps.Options.IsSpecified ? slashProps.Options.Value.Select(x => new Discord.API.ApplicationCommandOption(x)).ToArray() : Optional.Unspecified; - - model.DefaultPermission = slashProps.DefaultPermission.IsSpecified - ? slashProps.DefaultPermission.Value - : Optional.Unspecified; } models.Add(model); @@ -141,6 +139,9 @@ namespace Discord.Rest { Name = arg.Name.Value, Type = arg.Type, + DefaultPermission = arg.DefaultPermission.IsSpecified + ? arg.DefaultPermission.Value + : Optional.Unspecified }; if (arg is SlashCommandProperties slashProps) @@ -152,10 +153,6 @@ namespace Discord.Rest model.Options = slashProps.Options.IsSpecified ? slashProps.Options.Value.Select(x => new Discord.API.ApplicationCommandOption(x)).ToArray() : Optional.Unspecified; - - model.DefaultPermission = slashProps.DefaultPermission.IsSpecified - ? slashProps.DefaultPermission.Value - : Optional.Unspecified; } models.Add(model); @@ -202,6 +199,9 @@ namespace Discord.Rest var model = new Discord.API.Rest.ModifyApplicationCommandParams() { Name = args.Name, + DefaultPermission = args.DefaultPermission.IsSpecified + ? args.DefaultPermission.Value + : Optional.Unspecified }; if(args is SlashCommandProperties slashProps) @@ -223,10 +223,6 @@ namespace Discord.Rest model.Options = slashProps.Options.IsSpecified ? slashProps.Options.Value.Select(x => new Discord.API.ApplicationCommandOption(x)).ToArray() : Optional.Unspecified; - - model.DefaultPermission = slashProps.DefaultPermission.IsSpecified - ? slashProps.DefaultPermission.Value - : Optional.Unspecified; } return await client.ApiClient.ModifyGlobalApplicationCommandAsync(model, command.Id, options).ConfigureAwait(false); @@ -257,6 +253,9 @@ namespace Discord.Rest { Name = arg.Name.Value, Type = arg.Type, + DefaultPermission = arg.DefaultPermission.IsSpecified + ? arg.DefaultPermission.Value + : Optional.Unspecified }; if (arg is SlashCommandProperties slashProps) @@ -268,10 +267,6 @@ namespace Discord.Rest model.Options = slashProps.Options.IsSpecified ? slashProps.Options.Value.Select(x => new Discord.API.ApplicationCommandOption(x)).ToArray() : Optional.Unspecified; - - model.DefaultPermission = slashProps.DefaultPermission.IsSpecified - ? slashProps.DefaultPermission.Value - : Optional.Unspecified; } return await client.ApiClient.CreateGuildApplicationCommandAsync(model, guildId, options).ConfigureAwait(false); @@ -291,6 +286,9 @@ namespace Discord.Rest var model = new ModifyApplicationCommandParams() { Name = arg.Name, + DefaultPermission = arg.DefaultPermission.IsSpecified + ? arg.DefaultPermission.Value + : Optional.Unspecified }; if (arg is SlashCommandProperties slashProps) @@ -302,10 +300,6 @@ namespace Discord.Rest model.Options = slashProps.Options.IsSpecified ? slashProps.Options.Value.Select(x => new Discord.API.ApplicationCommandOption(x)).ToArray() : Optional.Unspecified; - - model.DefaultPermission = slashProps.DefaultPermission.IsSpecified - ? slashProps.DefaultPermission.Value - : Optional.Unspecified; } return await client.ApiClient.ModifyGuildApplicationCommandAsync(model, guildId, command.Id, options).ConfigureAwait(false);