For most people their brain craves stimulation. When we introduce highly stimulating and rewarding activities like YouTube, video games, and social media our brains procrastinate the harder work like programming because an easier reward is achieved by watching YouTube (ie. Less energy is expended to produce the same dopamine response).
The cycle of YouTube, social media, and video games (or anything you're addicted to) can easily ruin your life because you get caught in a loop of not wanting to work (procrastination), so you watch YouTube. That creates a habit of watching YouTube and the brain optimizes itself for that. Every time you sit at the computer you have the urge to watch Youtube. Then you start to feel bad about not making progress on your work. You turn to YouTube to make yourself feel better because it's the most efficient way of producing a reward in the brain. After each video is over, you don't want to start work because you've already optimized for watching YouTube (ie. momentum). So, you tell yourself lies like "one more video", or "after the next one I'll start work", or "well it's already 4pm so the day is basically over anyways I can't start working now".
If you're really having trouble and want a solution, block all of the distracting things in your life and get back to the basics. You don't need a "dopamine detox" or anything complicated. Simply block things (using a browser extension or DNS blacklist) you don't want to be doing and let yourself either be bored or do the work.
Suddenly you will find it's easy to get started on work, and actually pretty enjoyable to be productive. Use that momentum, and keep off of distracting websites/apps.
Do it for 5 days and see how far you can get.
My brain is really an asshole and now i slack on discord servers :'(. I need to fix that now...
Continuously audit yourself, like you already are (noticing that Discord is now an issue), and block more and more things that you find distracting. But also recognize that there will always be something that is more entertaining and you can always find a new website to waste time on. Block out as much as you can so that you're not fighting yourself and use a bit of self-control for the rest. Again, once your options are sit there and be bored or code that's when you see the greatest benefit.
Keep going, any progress that you make is good. Try to make a habit of opening your computer and starting with something productive even just for 10 minutes. Usually once you're engaged it's not that hard to keep going as you make progress. Getting started is the hard part and being bored helps you want to get started.
In general the less stimulation you have in your life the more interesting work will be. Depending on what resources you need for coding it might be easier to set up a whitelist for those couple of websites instead of a blacklist where you're trying to block everything distracting manually.
As far as how you set your expectations, I'd like to propose an experiment: Draw a Punnett square, where X is each possible choice you can make with your time relevant to one variable (work/don't work) and Y is each possible result of the subject of your choice for X (meet deadline/don't meet deadline). You can imagine easily what a p.sq. of these four possible combined outcomes looks like. The point isn't that some of the intersections are impossible (ex. don't work/meet deadline). The point is to notice your ideal outcome (don't work/meet deadline) is: a) impossible, or if not impossible, then b) unlikely, given each intersection has the same general probability of occurrence. In a nutshell, stop expecting the impossible and the improbable. By investing yourself fully in just one of the possible outcomes represented in a p.sq., you will both fail to accept a reasonably good but unfavorable outcome and fail to persist when your ideal outcome appears unachievable.