Minecraft wiki entry for Thorns, associated text: Guardians and elder guardians have a Thorns-like effect which inflicts damage when their spikes are out. However, this only affects melee damage, whereas the enchantment applies to all direct attacks, including ranged.