One position would be that everyone is selfish, as anything we do is ultimately to sate some desire within ourselves. Someone adhering to this position would say that helping others makes us feel good, and so that is selfish. Helping ourselves sates other desires, and so that is also selfish. Regardless of the choices we make in life, those choices are made according to our own priorities, and are therefore centered on the self - i.e. selfish.
The negative connotations that go along with this generally stem from people's own selfish natures, and how from their perspective 'selfish' people are going against their internal desires.
From this perspective, the best you can do is to do what you feel is right, and disregard any claim that you are being selfish, as anyone accusing you of this is being selfish themselves.
The fact that you yourself are questioning whether you're being selfish means that you feel guilt for not helping, which implies some sort of empathy - which is good. I wouldn't beat yourself up over it though, you prioritised your time for yourself, which is not unreasonable, otherwise where do you draw the line? At what point does it go from selfishness to pragmatism?
If you had agreed to write the code, it would probably have been to ensure that your friend did not get upset with you, and that would also have been selfish as you would have been acting in your best interests once again, so no matter what you picked you would be considered selfish - by me, at least.