Hey, I feel the need to comment on this because I relate to this on a spiritual level.
I'm in my sixth year of my undergrad because I've failed 8 classes. I'm gonna have to go to my community college and to prerequisite classes and do well to prove to the grad school I want to go to that I've improved and am serious about taking on their program.
Failing sucks because we look at what's happening and know that we probably could've done better but didn't and now here we are, looking at the result of all the minutes and hours spent on not investing our time into doing homework or studying.
But now you have something that some people don't have: the opportunity to start over. You can retake it like you said. It won't erase the failure from your mind, but fall seven times, get up eight. You may feel like you've failed yourself, but don't you dare give up. Learn from what you've done, think long and hard about what you want to do or could've done better, and write it down. Make a plan and stick to it now. If you could make the most productive day you could, what would it look like? Write that down. What do you like to do? Write that down. Then make yourself work for a set amount of minutes or hours at a time before you allow yourself to do the things you enjoy. Try to study for 30 minutes, do something fun or step away from it for 5 minutes, study for another 30 minutes, rinse and repeat until you've completed an assignment or finished studying, and then do something you enjoy doing. Setting up a reward system to knock out assignments you put on a list is the most helpful thing you could do for yourself.
At the end of each day, Keep track of all you've done, helpful or not helpful throughout the day, and write it down. Think of the things you accomplished and the things you got distracted by, remind yourself what you're doing this all for, and then readjust. Want that job you've dreamed of? Don't quit. Want to live on your own, afford your own place, have freedom? Keep working. Want to be seen for the hard work you put in to make yourself indispensable? Work smarter.
I've been trying to readjust myself after every failure. Sometimes I've even failed at that. If all else fails, don't give up. Take it day by day, hour by hour, minute by minute if you have to. Hit me up, find a good support system to stay accountable, study with people if you can. You can and will do this. Stay up.