i understand it is forbidden to take revenge (which seems to me to mean: to exact punishment for something bad done to you so that it will not be done again)
but how does that fit with not forgiving someone for their own good
Shulchan Aruch Harav 606.4
[One who is asked to] forgive should not be callous and withhold forgiveness. On the contrary, he should forgive immediately, unless his intent is for the benefit of the person asking forgiveness, to humble his brazen heart.
Alternatively, [one may withhold forgiveness] if he fears that forgiving will cause harm to himself. In such a situation he is not required to forgive, because his own life takes precedence over the life of his fellow.
see Tamlud Yoma 87b (and Rashi)
is revenge with the intention to better the person, permitted?
When is revenge permitted
Shulchan Aruch O"H 606.1, Mishna Berurah 606.9-10 and Magen Avraham 606.4