Is there a way to use the same script both in attack and defense, without actually writing it twice?
No, there isn't. Copy/pasting a response to another player with the same question (not here on itch.io though):
Part of the reason this isn't possible is because the plan is to eventually have attack- or defense-only items and APIs. Another reason is that scripts will rarely work as both attack and defense strategies. And the final reason is that there are other mechanisms planned for sharing snippets of scripts as a user, but I can't say whether it will make they will make it into the game or how long they would take. 😢