-
Notifications
You must be signed in to change notification settings - Fork 47
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
base: main
Are you sure you want to change the base?
Add array support for slash commands #340
Conversation
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
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
@@ -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
…ents' into velvet/feat/variadic-slash-arguments
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:
^ 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:
[Range]
controls the array's length;Min
= required,Max
= LengthMin
elements will be required andMax - Min
elements will be optional. Max will coalesce to Min when unspecified.