When optional and mandatory effects conflict, the mandatory effect takes precedence.
Example: Mazahaedron gains 2$ from Worldheart Shard (Gain 2$ that can only be used to gain a card. Place the next card you gain this turn on top of any ally's discard pile.). He also gains 2$ from Frozen Magmite (Gain 2$. You may place the next card you gain this turn on top of your deck.). The next card gained this turn by Mazahaedron must be placed on top of any ally's discard pile. He may not choose to place the card on top of his deck.