Argument checks
See original GitHub issueSummary
Implement ability to specify argument checks (preconditions) for command arguments in CNext.
Details
This would implement a way to have arguments validated by CNext. These checks would make it so that the data passed would have to match certain criteria for a command to be considered a valid overload. Examples include:
sbyte
,byte
,short
,ushort
,int
,uint
,long
,ulong
,float
,double
,decimal
[NonNegative]
would require that the number passed is >=0[NonPositive]
would require that the number passed is <= 0[Positive]
would require that the number passed is >0[Negative]
would require that the number passed is <0[NonZero]
would require that the number passed is !=0[Min(int, [bool])]
would require that the number passed has to be greater than (or greater than or equal to, if the optional argument is set to true, signifying inclusive check) the number specified[Max(int, [bool])]
much like above, but less than instead of greater than[Even]
would require the number passed to be even[Odd]
like above, but odd
string
[Uppercase]
would require that the passed string be all uppercase[Lowercase]
would require that the passed string be all lowercase[StartsWith(string, [bool])]
would require the string passed to start with a specific string, optionally ignoring case[EndsWith(string, [bool])]
like above, but ends with instead of starts with[Contains(string, [bool])]
like above, but contains, rather than starts or ends with[MaxLength(int)]
requires that the passed string be not longer than specified number of characters[MinLength(int)]
like above, but not shorter instead of longer[Regex(string, [RegexOptions])]
would require the passed string to match specified regular expression[Url]
would require the passed string to be a valid URL
An example usage would be like this:
[Command]
public async Task SomeCommandAsync(CommandContext ctx,
[Min(0), Max(10, true)] int arg)
{
// the argument has to be 0 < arg <= 10
}
[Command]
public async Task AnotherCommandAsync(CommandContext ctx,
[Regex("^\d{3}-\d{3}-\d{3}$", RegexOptions.ECMAScript)] string arg)
{
// the argument has to consist of 3 groups of digits separated by dashes
}
This would remove the burden of validating inputs from the bot developers, since the command framework would handle all of it in a clean manner.
Notes
This could perhaps be tied into command overloads. You could make commands that have different overloads based on data passed (e.g. different overloads for negative numbers, different for non-negative ones, etc). This is currently not possible, the overloads are distinguished based on argument types alone.
Update: There’s also no way to do most of the checks in any remotely clean manner.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:11
- Comments:9 (9 by maintainers)
Top GitHub Comments
knowing emzi, he was likely already planning to do so
Closing this due to slash commands.