Browse Source

Fix modifying application command by generic ModifyAsync (#121)

* Fix Modify calls of application commands

* Fix modification of guild command to use optional name value

* Fix RestApplicationCommand update to set type correctly

* Add non generic ModifyAsync for application commands to determine type correctly
pull/1923/head
František Boháček GitHub 3 years ago
parent
commit
d8b2cbdd6c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 80 additions and 46 deletions
  1. +12
    -1
      src/Discord.Net.Core/Discord.Net.Core.xml
  2. +13
    -1
      src/Discord.Net.Core/Entities/Interactions/IApplicationCommand.cs
  3. +5
    -2
      src/Discord.Net.Rest/Discord.Net.Rest.xml
  4. +24
    -6
      src/Discord.Net.Rest/Entities/Interactions/InteractionHelper.cs
  5. +9
    -2
      src/Discord.Net.Rest/Entities/Interactions/RestApplicationCommand.cs
  6. +2
    -2
      src/Discord.Net.Rest/Entities/Interactions/RestGlobalCommand.cs
  7. +2
    -2
      src/Discord.Net.Rest/Entities/Interactions/RestGuildCommand.cs
  8. +4
    -9
      src/Discord.Net.WebSocket/Discord.Net.WebSocket.xml
  9. +9
    -21
      src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketApplicationCommand.cs

+ 12
- 1
src/Discord.Net.Core/Discord.Net.Core.xml View File

@@ -4686,7 +4686,7 @@
</member>
<member name="T:Discord.IApplicationCommand">
<summary>
The base command model that belongs to an application.
The base command model that belongs to an application.
</summary>
</member>
<member name="P:Discord.IApplicationCommand.ApplicationId">
@@ -4729,6 +4729,17 @@
A task that represents the asynchronous modification operation.
</returns>
</member>
<member name="M:Discord.IApplicationCommand.ModifyAsync``1(System.Action{``0},Discord.RequestOptions)">
<summary>
Modifies the current application command.
</summary>
<param name="func">The new properties to use when modifying the command.</param>
<param name="options">The options to be used when sending the request.</param>
<returns>
A task that represents the asynchronous modification operation.
</returns>
<exception cref="T:System.InvalidOperationException">Thrown when you pass in an invalid <see cref="T:Discord.ApplicationCommandProperties"/> type.</exception>
</member>
<member name="T:Discord.IApplicationCommandInteractionData">
<summary>
Represents data of an Interaction Command, see <see href="https://discord.com/developers/docs/interactions/slash-commands#interaction-applicationcommandinteractiondata"/>.


+ 13
- 1
src/Discord.Net.Core/Entities/Interactions/IApplicationCommand.cs View File

@@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Discord
{
/// <summary>
/// The base command model that belongs to an application.
/// The base command model that belongs to an application.
/// </summary>
public interface IApplicationCommand : ISnowflakeEntity, IDeletable
{
@@ -50,5 +50,17 @@ namespace Discord
/// A task that represents the asynchronous modification operation.
/// </returns>
Task ModifyAsync(Action<ApplicationCommandProperties> func, RequestOptions options = null);

/// <summary>
/// Modifies the current application command.
/// </summary>
/// <param name="func">The new properties to use when modifying the command.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous modification operation.
/// </returns>
/// <exception cref="InvalidOperationException">Thrown when you pass in an invalid <see cref="ApplicationCommandProperties"/> type.</exception>
Task ModifyAsync<TArg>(Action<TArg> func, RequestOptions options = null)
where TArg : ApplicationCommandProperties;
}
}

+ 5
- 2
src/Discord.Net.Rest/Discord.Net.Rest.xml View File

@@ -3910,6 +3910,9 @@
<inheritdoc/>
</member>
<member name="M:Discord.Rest.RestApplicationCommand.ModifyAsync(System.Action{Discord.ApplicationCommandProperties},Discord.RequestOptions)">
<inheritdoc />
</member>
<member name="M:Discord.Rest.RestApplicationCommand.ModifyAsync``1(System.Action{``0},Discord.RequestOptions)">
<inheritdoc/>
</member>
<member name="T:Discord.Rest.RestApplicationCommandChoice">
@@ -3961,7 +3964,7 @@
<member name="M:Discord.Rest.RestGlobalCommand.DeleteAsync(Discord.RequestOptions)">
<inheritdoc/>
</member>
<member name="M:Discord.Rest.RestGlobalCommand.ModifyAsync(System.Action{Discord.ApplicationCommandProperties},Discord.RequestOptions)">
<member name="M:Discord.Rest.RestGlobalCommand.ModifyAsync``1(System.Action{``0},Discord.RequestOptions)">
<summary>
Modifies this <see cref="T:Discord.Rest.RestApplicationCommand"/>.
</summary>
@@ -3984,7 +3987,7 @@
<member name="M:Discord.Rest.RestGuildCommand.DeleteAsync(Discord.RequestOptions)">
<inheritdoc/>
</member>
<member name="M:Discord.Rest.RestGuildCommand.ModifyAsync(System.Action{Discord.ApplicationCommandProperties},Discord.RequestOptions)">
<member name="M:Discord.Rest.RestGuildCommand.ModifyAsync``1(System.Action{``0},Discord.RequestOptions)">
<summary>
Modifies this <see cref="T:Discord.Rest.RestApplicationCommand"/>.
</summary>


+ 24
- 6
src/Discord.Net.Rest/Entities/Interactions/InteractionHelper.cs View File

@@ -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<TArg>(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<ApplicationCommand> ModifyGlobalCommand<TArg>(BaseDiscordClient client, IApplicationCommand command,
Action<TArg> func, RequestOptions options = null) where TArg : ApplicationCommandProperties
Action<TArg> func, RequestOptions options = null) where TArg : ApplicationCommandProperties
{
var arg = (TArg)Activator.CreateInstance(typeof(TArg));
var arg = GetApplicationCommandProperties<TArg>(command);
func(arg);
return ModifyGlobalCommand(client, command, arg, options);
}
@@ -260,9 +278,9 @@ namespace Discord.Rest
}

public static Task<ApplicationCommand> ModifyGuildCommand<TArg>(BaseDiscordClient client, IApplicationCommand command, ulong guildId,
Action<TArg> func, RequestOptions options = null) where TArg : ApplicationCommandProperties
Action<TArg> func, RequestOptions options = null) where TArg : ApplicationCommandProperties
{
var arg = (TArg)Activator.CreateInstance(typeof(TArg));
var arg = GetApplicationCommandProperties<TArg>(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)


+ 9
- 2
src/Discord.Net.Rest/Entities/Interactions/RestApplicationCommand.cs View File

@@ -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;
}


/// <inheritdoc/>
public abstract Task DeleteAsync(RequestOptions options = null);

/// <inheritdoc />
public Task ModifyAsync(Action<ApplicationCommandProperties> func, RequestOptions options = null)
{
return ModifyAsync<ApplicationCommandProperties>(func, options);
}
/// <inheritdoc/>
public abstract Task ModifyAsync(Action<ApplicationCommandProperties> func, RequestOptions options = null);
public abstract Task ModifyAsync<TArg>(Action<TArg> func, RequestOptions options = null)
where TArg : ApplicationCommandProperties;

IReadOnlyCollection<IApplicationCommandOption> IApplicationCommand.Options => Options;
}


+ 2
- 2
src/Discord.Net.Rest/Entities/Interactions/RestGlobalCommand.cs View File

@@ -37,9 +37,9 @@ namespace Discord.Rest
/// <returns>
/// The modified command.
/// </returns>
public override async Task ModifyAsync(Action<ApplicationCommandProperties> func, RequestOptions options = null)
public override async Task ModifyAsync<TArg>(Action<TArg> func, RequestOptions options = null)
{
var cmd = await InteractionHelper.ModifyGlobalCommand(Discord, this, func, options).ConfigureAwait(false);
var cmd = await InteractionHelper.ModifyGlobalCommand<TArg>(Discord, this, func, options).ConfigureAwait(false);
this.Update(cmd);
}
}


+ 2
- 2
src/Discord.Net.Rest/Entities/Interactions/RestGuildCommand.cs View File

@@ -42,9 +42,9 @@ namespace Discord.Rest
/// <returns>
/// The modified command
/// </returns>
public override async Task ModifyAsync(Action<ApplicationCommandProperties> func, RequestOptions options = null)
public override async Task ModifyAsync<TArg>(Action<TArg> func, RequestOptions options = null)
{
var model = await InteractionHelper.ModifyGuildCommand(Discord, this, GuildId, func, options).ConfigureAwait(false);
var model = await InteractionHelper.ModifyGuildCommand<TArg>(Discord, this, GuildId, func, options).ConfigureAwait(false);
this.Update(model);
}



+ 4
- 9
src/Discord.Net.WebSocket/Discord.Net.WebSocket.xml View File

@@ -4028,16 +4028,11 @@
<member name="M:Discord.WebSocket.SocketApplicationCommand.DeleteAsync(Discord.RequestOptions)">
<inheritdoc/>
</member>
<member name="M:Discord.WebSocket.SocketApplicationCommand.ModifyAsync(System.Action{Discord.ApplicationCommandProperties},Discord.RequestOptions)">
<inheritdoc />
</member>
<member name="M:Discord.WebSocket.SocketApplicationCommand.ModifyAsync``1(System.Action{``0},Discord.RequestOptions)">
<summary>
Modifies the current application command.
</summary>
<param name="func">The new properties to use when modifying the command.</param>
<param name="options">The options to be used when sending the request.</param>
<returns>
A task that represents the asynchronous modification operation.
</returns>
<exception cref="T:System.InvalidOperationException">Thrown when you pass in an invalid <see cref="T:Discord.ApplicationCommandProperties"/> type.</exception>
<inheritdoc />
</member>
<member name="T:Discord.WebSocket.SocketApplicationCommandChoice">
<summary>


+ 9
- 21
src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketApplicationCommand.cs View File

@@ -91,34 +91,24 @@ namespace Discord.WebSocket
public Task DeleteAsync(RequestOptions options = null)
=> InteractionHelper.DeleteUnknownApplicationCommand(Discord, this.GuildId, this, options);

/// <summary>
/// Modifies the current application command.
/// </summary>
/// <param name="func">The new properties to use when modifying the command.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous modification operation.
/// </returns>
/// <exception cref="InvalidOperationException">Thrown when you pass in an invalid <see cref="ApplicationCommandProperties"/> type.</exception>
/// <inheritdoc />
public Task ModifyAsync(Action<ApplicationCommandProperties> func, RequestOptions options = null)
{
return ModifyAsync<ApplicationCommandProperties>(func, options);
}
/// <inheritdoc />
public async Task ModifyAsync<TArg>(Action<TArg> 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<TArg>(Discord, this, func, options).ConfigureAwait(false);
}
else
{
command = await InteractionHelper.ModifyGuildCommand(Discord, this, this.GuildId.Value, func, options);
command = await InteractionHelper.ModifyGuildCommand<TArg>(Discord, this, this.GuildId.Value, func, options);
}

this.Update(command);
@@ -126,7 +116,5 @@ namespace Discord.WebSocket

// IApplicationCommand
IReadOnlyCollection<IApplicationCommandOption> IApplicationCommand.Options => Options;
Task IApplicationCommand.ModifyAsync(Action<ApplicationCommandProperties> func, RequestOptions options)
=> ModifyAsync(func, options);
}
}

Loading…
Cancel
Save