God won’t let you enrich yourself through your sin.
Through this chastening (not punishment), David was able to humble himself, accept responsibility and endure the suffering that came his way.
Chastening is difficulty permitted by God wants us to submit to His will and develop Godly character.
David learned that during this time.
Psalm 32 & 51 are David’s prayers of a person who had lost everything--Psalms that each of us has likely read at some point or another and we take comfort in them.
The take-away from this is that God is always looking to redeem each of us regardless of our sin.
He will use the consequences of those sins to get our attention and hopefully push us towards him.