diff --git a/src/Discord.Net.Core/Discord.Net.Core.xml b/src/Discord.Net.Core/Discord.Net.Core.xml index 24ced574c..99148bace 100644 --- a/src/Discord.Net.Core/Discord.Net.Core.xml +++ b/src/Discord.Net.Core/Discord.Net.Core.xml @@ -4686,7 +4686,7 @@ - The base command model that belongs to an application. + The base command model that belongs to an application. @@ -4729,6 +4729,17 @@ A task that represents the asynchronous modification operation. + + + Modifies the current application command. + + The new properties to use when modifying the command. + The options to be used when sending the request. + + A task that represents the asynchronous modification operation. + + Thrown when you pass in an invalid type. + Represents data of an Interaction Command, see . diff --git a/src/Discord.Net.Core/Entities/Interactions/IApplicationCommand.cs b/src/Discord.Net.Core/Entities/Interactions/IApplicationCommand.cs index e0cf605d2..c5f669755 100644 --- a/src/Discord.Net.Core/Entities/Interactions/IApplicationCommand.cs +++ b/src/Discord.Net.Core/Entities/Interactions/IApplicationCommand.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Discord { /// - /// The base command model that belongs to an application. + /// The base command model that belongs to an application. /// public interface IApplicationCommand : ISnowflakeEntity, IDeletable { @@ -50,5 +50,17 @@ namespace Discord /// A task that represents the asynchronous modification operation. /// Task ModifyAsync(Action func, RequestOptions options = null); + + /// + /// Modifies the current application command. + /// + /// The new properties to use when modifying the command. + /// The options to be used when sending the request. + /// + /// A task that represents the asynchronous modification operation. + /// + /// Thrown when you pass in an invalid type. + Task ModifyAsync(Action func, RequestOptions options = null) + where TArg : ApplicationCommandProperties; } } diff --git a/src/Discord.Net.Rest/Discord.Net.Rest.xml b/src/Discord.Net.Rest/Discord.Net.Rest.xml index 6e4d93991..9ffed332c 100644 --- a/src/Discord.Net.Rest/Discord.Net.Rest.xml +++ b/src/Discord.Net.Rest/Discord.Net.Rest.xml @@ -3910,6 +3910,9 @@ + + + @@ -3961,7 +3964,7 @@ - + Modifies this . @@ -3984,7 +3987,7 @@ - + Modifies this . diff --git a/src/Discord.Net.Rest/Entities/Interactions/InteractionHelper.cs b/src/Discord.Net.Rest/Entities/Interactions/InteractionHelper.cs index 59d5c4f2b..9b15ace17 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/InteractionHelper.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/InteractionHelper.cs @@ -48,7 +48,7 @@ namespace Discord.Rest RequestOptions options = null) { var model = await client.ApiClient.GetGlobalApplicationCommandAsync(id, options).ConfigureAwait(false); - + return RestGlobalCommand.Create(client, model); } @@ -164,10 +164,28 @@ namespace Discord.Rest return await client.ApiClient.BulkOverwriteGuildApplicationCommands(guildId, models.ToArray(), options).ConfigureAwait(false); } + private static TArg GetApplicationCommandProperties(IApplicationCommand command) + where TArg : ApplicationCommandProperties + { + bool isBaseClass = typeof(TArg) == typeof(ApplicationCommandProperties); + + switch (true) + { + case true when (typeof(TArg) == typeof(SlashCommandProperties) || isBaseClass) && command.Type == ApplicationCommandType.Slash: + return new SlashCommandProperties() as TArg; + case true when (typeof(TArg) == typeof(MessageCommandProperties) || isBaseClass) && command.Type == ApplicationCommandType.Message: + return new MessageCommandProperties() as TArg; + case true when (typeof(TArg) == typeof(UserCommandProperties) || isBaseClass) && command.Type == ApplicationCommandType.User: + return new UserCommandProperties() as TArg; + default: + throw new InvalidOperationException($"Cannot modify application command of type {command.Type} with the parameter type {typeof(TArg).FullName}"); + } + } + public static Task ModifyGlobalCommand(BaseDiscordClient client, IApplicationCommand command, - Action func, RequestOptions options = null) where TArg : ApplicationCommandProperties + Action func, RequestOptions options = null) where TArg : ApplicationCommandProperties { - var arg = (TArg)Activator.CreateInstance(typeof(TArg)); + var arg = GetApplicationCommandProperties(command); func(arg); return ModifyGlobalCommand(client, command, arg, options); } @@ -260,9 +278,9 @@ namespace Discord.Rest } public static Task ModifyGuildCommand(BaseDiscordClient client, IApplicationCommand command, ulong guildId, - Action func, RequestOptions options = null) where TArg : ApplicationCommandProperties + Action func, RequestOptions options = null) where TArg : ApplicationCommandProperties { - var arg = (TArg)Activator.CreateInstance(typeof(TArg)); + var arg = GetApplicationCommandProperties(command); func(arg); return ModifyGuildCommand(client, command, guildId, arg, options); } @@ -272,7 +290,7 @@ namespace Discord.Rest { var model = new ModifyApplicationCommandParams() { - Name = arg.Name.Value, + Name = arg.Name, }; if (arg is SlashCommandProperties slashProps) diff --git a/src/Discord.Net.Rest/Entities/Interactions/RestApplicationCommand.cs b/src/Discord.Net.Rest/Entities/Interactions/RestApplicationCommand.cs index dcba1ee87..439790f26 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/RestApplicationCommand.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/RestApplicationCommand.cs @@ -57,6 +57,7 @@ namespace Discord.Rest internal virtual void Update(Model model) { + this.Type = model.Type; this.ApplicationId = model.ApplicationId; this.Name = model.Name; this.Description = model.Description; @@ -67,12 +68,18 @@ namespace Discord.Rest : null; } - /// public abstract Task DeleteAsync(RequestOptions options = null); + /// + public Task ModifyAsync(Action func, RequestOptions options = null) + { + return ModifyAsync(func, options); + } + /// - public abstract Task ModifyAsync(Action func, RequestOptions options = null); + public abstract Task ModifyAsync(Action func, RequestOptions options = null) + where TArg : ApplicationCommandProperties; IReadOnlyCollection IApplicationCommand.Options => Options; } diff --git a/src/Discord.Net.Rest/Entities/Interactions/RestGlobalCommand.cs b/src/Discord.Net.Rest/Entities/Interactions/RestGlobalCommand.cs index 7e3ca0a4e..8d8ae5983 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/RestGlobalCommand.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/RestGlobalCommand.cs @@ -37,9 +37,9 @@ namespace Discord.Rest /// /// The modified command. /// - public override async Task ModifyAsync(Action func, RequestOptions options = null) + public override async Task ModifyAsync(Action func, RequestOptions options = null) { - var cmd = await InteractionHelper.ModifyGlobalCommand(Discord, this, func, options).ConfigureAwait(false); + var cmd = await InteractionHelper.ModifyGlobalCommand(Discord, this, func, options).ConfigureAwait(false); this.Update(cmd); } } diff --git a/src/Discord.Net.Rest/Entities/Interactions/RestGuildCommand.cs b/src/Discord.Net.Rest/Entities/Interactions/RestGuildCommand.cs index aa236d4b1..48f402297 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/RestGuildCommand.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/RestGuildCommand.cs @@ -42,9 +42,9 @@ namespace Discord.Rest /// /// The modified command /// - public override async Task ModifyAsync(Action func, RequestOptions options = null) + public override async Task ModifyAsync(Action func, RequestOptions options = null) { - var model = await InteractionHelper.ModifyGuildCommand(Discord, this, GuildId, func, options).ConfigureAwait(false); + var model = await InteractionHelper.ModifyGuildCommand(Discord, this, GuildId, func, options).ConfigureAwait(false); this.Update(model); } diff --git a/src/Discord.Net.WebSocket/Discord.Net.WebSocket.xml b/src/Discord.Net.WebSocket/Discord.Net.WebSocket.xml index e253692db..cfdd0e89c 100644 --- a/src/Discord.Net.WebSocket/Discord.Net.WebSocket.xml +++ b/src/Discord.Net.WebSocket/Discord.Net.WebSocket.xml @@ -4028,16 +4028,11 @@ + + + - - Modifies the current application command. - - The new properties to use when modifying the command. - The options to be used when sending the request. - - A task that represents the asynchronous modification operation. - - Thrown when you pass in an invalid type. + diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketApplicationCommand.cs b/src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketApplicationCommand.cs index 77a43a1e3..f0b8929e8 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketApplicationCommand.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketApplicationCommand.cs @@ -91,34 +91,24 @@ namespace Discord.WebSocket public Task DeleteAsync(RequestOptions options = null) => InteractionHelper.DeleteUnknownApplicationCommand(Discord, this.GuildId, this, options); - /// - /// Modifies the current application command. - /// - /// The new properties to use when modifying the command. - /// The options to be used when sending the request. - /// - /// A task that represents the asynchronous modification operation. - /// - /// Thrown when you pass in an invalid type. + /// + public Task ModifyAsync(Action func, RequestOptions options = null) + { + return ModifyAsync(func, options); + } + + /// public async Task ModifyAsync(Action func, RequestOptions options = null) where TArg : ApplicationCommandProperties { - switch (typeof(TArg)) - { - case Type messageCommand when messageCommand == typeof(MessageCommandProperties) && this.Type != ApplicationCommandType.Message: - case Type slashCommand when slashCommand == typeof(SlashCommandProperties) && this.Type != ApplicationCommandType.Slash: - case Type userCommand when userCommand == typeof(UserCommandProperties) && this.Type != ApplicationCommandType.User: - throw new InvalidOperationException($"Cannot modify this application command with the parameter type {nameof(TArg)}"); - } - Model command = null; if (this.IsGlobalCommand) { - command = await InteractionHelper.ModifyGlobalCommand(Discord, this, func, options).ConfigureAwait(false); + command = await InteractionHelper.ModifyGlobalCommand(Discord, this, func, options).ConfigureAwait(false); } else { - command = await InteractionHelper.ModifyGuildCommand(Discord, this, this.GuildId.Value, func, options); + command = await InteractionHelper.ModifyGuildCommand(Discord, this, this.GuildId.Value, func, options); } this.Update(command); @@ -126,7 +116,5 @@ namespace Discord.WebSocket // IApplicationCommand IReadOnlyCollection IApplicationCommand.Options => Options; - Task IApplicationCommand.ModifyAsync(Action func, RequestOptions options) - => ModifyAsync(func, options); } }