It is true that sunroofs and moonroofs essentially have the same function in an automobile. They are for letting in natural light to the vehicle, and if they are retractable or able to tilt, they can also let in fresh air without the need to roll down your windows. The differences come in what the roofs are actually made out of. When it comes to sunroofs, Motor Trend details how they are typically made of an opaque material, such as metal, and the only way to let in light or air into a car is by either retracting or tilting the metal panel.
In contrast, a moonroof is made of glass. Because it is a clear material, it will always let in light unless covered up by some sort of sunshade. Many moonroofs can retract or tilt to let in air, but that is not always a given, with many moonroofs just being fixed glass panels on the roofs of cars. The distinction is credited to John Atkinson, who worked in marketing for Ford in the 1970s.
When you describe them like this, the semantic difference makes sense. The sunroof means you are actively looking to let sunlight into your car. Meanwhile, a moonroof passively lets in light, and the use of a sunshade to block out sunlight is the active element. No one will reprimand you if you use the words interchangeably, but it’s nice to know that there’s actually a reason there are two words.