In my experience, the only emotion that can counteract temptation is faith. Faith in what? I don’t know. Faith in something. Faith in a higher power, faith in the universe, faith in yourself. What doesn’t matter. It is the feeling that is able to counteract temptation.
People of every time and civilization knew the cost of giving in to temptation. And they must have come across the feeling of faith that can counteract it. That’s why faith always comes up in history. That could also be a reason why religion is a big part of human history.
Ego can help too but it has to be carefully directed. We all have it, so might as well use it for something useful.