Shalom, in our parasha, after Yosef הצדיק reveals his identity to his brothers, he says words of comfort to them.
״ועתה לא אתם שלחתם אותי הנה כי האלוקים״
“And now, it wasn’t you who sent me here but HaShem…”( 45,8)
There are many comments on the underrgstanding of this pasuk. Rabenu Don Yishagh Abarbanel comments, true a person has free will, however, when it comes to great deeds and events, those aren’t carried out by choice, they are set by the heavenly court.
״לב מלכים ביד ה׳ על כל אשר יחפוץ יטנו״
“ The heart of the kings are in the hands of HaShem, whichever path He wills, He will twist them “ (Proverbs 21,1)
This is what Yosef relayed to his brothers. Such an important venture as saving the family of Yaakov Avinu and the generations which will follow from the famine and to be the cause for them to come down to Egypt… could only be orchestrated by the heavenly court and you shouldn’t be depressed about it.
In the same token, Rabenu Yishagh Arama in his Sefer עקידת יצחק writes that Yosef tells the brothers, “True you sold me as a slave, but those Midyanites could have kept me as a slave or sold me elsewhere, but HaShem wanted me to end up in Egypt and become the viceroy for the benefit of Am Yisrael”
The lesson is very obvious, the Torah Hakedosha teaches us not to judge the events at face value and understand the true meaning and message relayed in the events of life to strengthen our emuna in the Almighty!