It happened, when Joshua was in Jericho, that he raised his eyes and saw, and, behold, there stood a man opposite him with his sword drawn in his hand. Joshua went over to him and asked him, “Are you for us or for our enemies?”
In the next verse, the man with the sword identifies himself as an angel, an officer in the Heavenly army. The Talmud in Eiruvin (63b) tells us that the angel chastised Joshua for being so preoccupied with preparing for the battle of Jericho that he neglected his Torah studies. Before the battle of Ai, Joshua knew better, so he spent the night before alone, immersed in Torah study (8:13).