Is there a way to have my macbook set its brightness based on it running from battery or from line power?
While macOS does not support the exact behavior you are looking for (setting custom brightness when switching between power states), you can configure your Mac portable to slightly dim the brightness automatically when on battery power.
To do that, go to System Preferences → Energy Saver and enable Slightly dim the display while on battery power.