"And the Lord spoke to Moses, saying: Harass the Midianites" (Numbers 25:17). Why? "Because they are harassing you" (Numbers 25:18). From here the Sages said: If one comes to kill you, rise early to kill him. [Rabbi Shimon says:] For whoever causes a person to sin is worse than one who kills him, since one who kills him kills him in this world, but he still has a portion in the world to come; whereas one who causes him to sin kills him in this world and for the world to come. Two nations confronted Israel with the sword, the Egyptians and the Edomites, as it is said, "I will pursue, I will overtake, I will divide the spoil" (Exodus 15:9), and "And Edom said to him: You shall not pass through me, lest I come out against you with the sword" (Numbers 20:18). And two confronted them with transgression, Moab and the Ammonites. Concerning those who confronted them with the sword it is written, "You shall not abhor an Edomite... you shall not abhor an Egyptian" (Deuteronomy 23:8). And concerning those who confronted them with transgression, "No Ammonite or Moabite shall enter the assembly of the Lord" (Deuteronomy 23:4).
Why Israel May Welcome Edom and Egypt but Never Moab or Ammon
Midrash Tanchuma Buber, Pinchas 4:1
וידבר ה' אל משה לאמר, צרור את המדינים (שם שם ט זיז). למה כי צוררים הם לכם (שם שם יח), מיכאן אמרו חכמים בא להורגך השכם להרגו, [ר' שמעון אומר] שכל המחטיא את האדם יותר מן ההורגו, שההורגו הורגו בעולם הזה, ויש לו חלק לעולם הבא, והמחטיאו הורגו בעולם הזה ולעולם הבא. שתי אומות קדמו ישראל בחרב, מצרים ואדומים, שנאמר ארדוף אשיג אחלק שלל (שמות טו ט), ויאמר אליו אדום לא תעבור בי פן בחרב אצא לקראתך (במדבר כ יח), ושתים בעבירה מואב ועמונים, על אלו שקדמו בחרב כתיב לא תתעב אדומי וגו' לא תתעב מצרי (דברים כג ח), ועל אלה שקדמו בעבירה לא יבא עמוני ומואבי בקהל ה' (שם שם ד).