convert property

  1. @override
Future<T?> Function(StringView view, ContextData context) convert
override

The function called to perform the conversion.

view is a view on the current argument string. For text commands, this will contain the entire content of the message. For Slash Commands, this will contain the content provided by the user for the current argument.

This function should not throw if parsing fails, it should instead return null to indicate failure. A BadInputException will then be added to CommandsPlugin.onCommandError where it can be handled appropriately.

Implementation

@override
Future<T?> Function(StringView view, ContextData context) get convert => (view, context) async {
      try {
        return fuzzy
            .extractOne(
              query: view.getQuotedWord(),
              choices: (await provider(context)).toList(),
              getter: stringify,
              cutoff: sensitivity,
            )
            .choice;
      } on StateError {
        // No elements matched query, try to revive the input.
        // Make sure to undo the call to `getQuotedWord()`.
        return reviver?.call(view..undo(), context);
      }
    };