Effects of stacking and twisting on the optical and electronic properties of transition metal dichalcogenide bilayers