Skip to content

Commit

Permalink
Implement minimum and maximum values for number converters (#47)
Browse files Browse the repository at this point in the history
* Implement minimum and maximum values for number converters

* Add note on limitations of number and channel converters
  • Loading branch information
abitofevrything authored Apr 10, 2022
1 parent 92689bf commit 56600f4
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 9 deletions.
3 changes: 3 additions & 0 deletions lib/nyxx_commands.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ export 'src/converters/converter.dart'
show
CombineConverter,
Converter,
DoubleConverter,
FallbackConverter,
GuildChannelConverter,
IntConverter,
NumConverter,
attachmentConverter,
boolConverter,
categoryGuildChannelConverter,
Expand Down
89 changes: 81 additions & 8 deletions lib/src/converters/converter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -314,29 +314,97 @@ const Converter<String> stringConverter = Converter<String>(

int? convertInt(StringView view, IChatContext context) => int.tryParse(view.getQuotedWord());

/// A converter that converts input to various types of numbers, possibly with a minimum or maximum
/// value.
///
/// Note: this converter does not ensure that all values will be in the range `min..max`. [min] and
/// [max] offer purely client-side validation and input from text commands is not validated beyond
/// being a valid number.
///
/// You might also be interested in:
/// - [IntConverter], for converting [int]s;
/// - [DoubleConverter], for converting [double]s.
class NumConverter<T extends num> extends Converter<T> {
/// The smallest value the user will be allowed to input in the Discord Client.
final T? min;

/// The biggest value the user will be allows to input in the Discord Client.
final T? max;

/// Create a new [NumConverter].
const NumConverter(
T? Function(StringView, IChatContext) convert, {
required CommandOptionType type,
this.min,
this.max,
}) : super(convert, type: type);

@override
void Function(CommandOptionBuilder)? get processOptionCallback => (builder) {
builder.min = min;
builder.max = max;
};
}

/// A converter that converts input to [int]s, possibly with a minimum or maximum value.
///
/// Note: this converter does not ensure that all values will be in the range `min..max`. [min] and
/// [max] offer purely client-side validation and input from text commands is not validated beyond
/// being a valid integer.
///
/// You might also be interested in:
/// - [intConverter], the default [IntConverter].
class IntConverter extends NumConverter<int> {
/// Create a new [IntConverter].
const IntConverter({
int? min,
int? max,
}) : super(
convertInt,
type: CommandOptionType.integer,
min: min,
max: max,
);
}

/// A [Converter] that converts input to an [int].
///
/// This converter attempts to parse the next word or quoted section of the input with [int.parse].
///
/// This converter has a Discord Slash Command Argument Type of [CommandOptionType.integer].
const Converter<int> intConverter = Converter<int>(
convertInt,
type: CommandOptionType.integer,
);
const Converter<int> intConverter = IntConverter();

double? convertDouble(StringView view, IChatContext context) =>
double.tryParse(view.getQuotedWord());

/// A converter that converts input to [double]s, possibly with a minimum or maximum value.
///
/// Note: this converter does not ensure that all values will be in the range `min..max`. [min] and
/// [max] offer purely client-side validation and input from text commands is not validated beyond
/// being a valid double.
///
/// You might also be interested in:
/// - [doubleConverter], the default [DoubleConverter].
class DoubleConverter extends NumConverter<double> {
/// Create a new [DoubleConverter].
const DoubleConverter({
double? min,
double? max,
}) : super(
convertDouble,
type: CommandOptionType.integer,
min: min,
max: max,
);
}

/// A [Converter] that converts input to a [double].
///
/// This converter attempts to parse the next word or quoted section of the input with
/// [double.parse].
///
/// This converter has a Discord Slash Command Argument Type of [CommandOptionType.number].
const Converter<double> doubleConverter = Converter<double>(
convertDouble,
type: CommandOptionType.number,
);
const Converter<double> doubleConverter = DoubleConverter();

bool? convertBool(StringView view, IChatContext context) {
String word = view.getQuotedWord();
Expand Down Expand Up @@ -592,6 +660,11 @@ IGuildChannel? convertGuildChannel(StringView view, IChatContext context) {
/// This converter will only allow users to select channels of one of the types in [channelTypes],
/// and then will further only accept channels of type `T`.
///
///
/// Note: this converter does not ensure that all values will conform to [channelTypes].
/// [channelTypes] offers purely client-side validation and input from text commands will not be
/// validated beyond being assignable to `T`.
///
/// You might also be interested in:
/// - [guildChannelConverter], a converter for all [IGuildChannel]s;
/// - [textGuildChannelConverter], a converter for [ITextGuildChannel]s;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies:
logging: ^1.0.2
meta: ^1.7.0
nyxx: ^3.0.0
nyxx_interactions: ^4.0.0
nyxx_interactions: ^4.1.0
random_string: ^2.3.1

dev_dependencies:
Expand Down

0 comments on commit 56600f4

Please sign in to comment.