You need to buy or borrow a mech alignment tool.
Remove the rear mech and screw the rear mech alignment tool into the rear mech dropout.
There are a few videos online, have a look and see how to straighten the rear mech dropout.
You might break the dropout if you are a bit hamfisted, always good to have a spare rear dropout in your spares.
Once this is straight, refit the rear mech.
If it still is troublesome, then the mech is the problem , have to be a new one to fix this.
Alternatively, your lbs can fix this, but it’s worth being able to do this yourself really.