command property

String? get command

Gets the command from the message, if any.

Returns the command without the leading '/' and bot username, or null if no command. The result is cached after the first access for better performance.

Example: "/start@mybot arg1 arg2" returns "start"

Implementation

String? get command {
  if (!_commandCached) {
    if (!hasCommand) {
      _cachedCommand = null;
    } else {
      final messageText = text!;
      final firstEntity = entities!.first;
      final commandWithSlash = messageText.substring(
        firstEntity.offset,
        firstEntity.offset + firstEntity.length,
      );

      // Remove leading '/' and potential bot username
      String cmd = commandWithSlash.substring(1);
      if (cmd.contains('@')) {
        cmd = cmd.split('@').first;
      }

      _cachedCommand = cmd;
    }
    _commandCached = true;
  }
  return _cachedCommand;
}