Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add array support for slash commands #340

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

VelvetToroyashi
Copy link
Contributor

This commit changes how Remora.Discord.Commands maps slash commands, enabling support for arrays to be parsed and accepted, despite Discord STILL not having support for this.

For better or worse, a snippet of code had to be removed:

var collectionType = parameter.GetActualParameterType();

if (!collectionType.IsCollection() && !collectionType.IsArray)
{
    // Sanity check
    throw new InvalidOperationException("Collection parameter was not a collection.");
}

^ For some reason, .IsCollection() returns...false? I gave up on trying to figure out why that was the case - the code still works fine and there's other validity checks elsewhere.

BREAKING-CHANGE: Commands with the structure of name__numbers (e.g. param__1) are no longer valid and will get erased by the pre-parsing step that is a requisite to map arrays. This should be an absolutely niche case, but is still technically a breaking change, and should be documented as such.

Some other things of note:

  • Attributes applied to the array apply to all elements in the array (this should go without saying, but hey)
  • [Range] controls the array's length; Min = required, Max = Length
  • If Min < Max, Min elements will be required and Max - Min elements will be optional. Max will coalesce to Min when unspecified.
  • Due to Discord's..questionable limitations, optional array parameters are placed at the end of the argument list for slash commands, but do preserve the order they're declared in relative to each other.

image

This commit changes how Remora.Discord.Commands maps slash commands,
enabling support for arrays to be parsed and accepted, despite Discord
STILL not having support for this.

For better or worse, a snippet of code had to be removed:

```cs
var collectionType = parameter.GetActualParameterType();

if (!collectionType.IsCollection() && !collectionType.IsArray)
{
    // Sanity check
    throw new InvalidOperationException("Collection parameter was not a collection.");
}
```
^ For some reason, `.IsCollection()` returns...false? I gave up on
trying to figure out why that was the case - the code still works fine
and there's other validity checks elsewhere.

BREAKING-CHANGE: Commands with the structure of `name__numbers`
(e.g. `param__1`) are *no longer valid* and will get erased by the
pre-parsing step that is a requisite to map arrays. This should be an
absolutely niche case, but is still technically a breaking change, and
should be documented as such.

Some other things of note:

- Attributes applied to the array apply to all elements in the array
 (this should go without saying, but hey)
- `[Range]` controls the array's length; `Min` = required, `Max` = Length
 - If Min < Max, `Min` elements will be required and `Max - Min` elements
   will be optional. Max will coallecse to Min when unspecified.
- Due to Discord's..questionable limitations, optional array parameters
  are placed at the end of the argument list for slash commands, but do
  preserve the order they're declared in relative to each other.
}
else
{
tempParameters[name] = (List<string>)[..existingValues, ..values];

Check warning

Code scanning / InspectCode

Redundant cast Warning

Type cast is redundant
return (option.Name, values);
#pragma warning disable SA1010 // Stylecop doesn't handle collection expressions correctly here
// Casting to string[] is optional, but absolves reliance on Roslyn making an inline array here.
return (option.Name, (string[])[value]);

Check warning

Code scanning / InspectCode

Redundant cast Warning

Type cast is redundant
@@ -37,7 +37,9 @@
using Remora.Discord.Commands.Attributes;
using Remora.Discord.Commands.Services;
using Remora.Rest.Core;
using Remora.Rest.Extensions;

Check warning

Code scanning / InspectCode

Redundant using directive Warning

Using directive is not required by the code and can be safely removed
@Nihlus Nihlus added the waiting for author The PR is awaiting an update from the author label Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for author The PR is awaiting an update from the author
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants