What happens when Allah is unhappy with a particular community? And how do we know that the suffering of that community is due to Allah's unhappiness or not?

Having problem either on community level or individual doesn’t mean this is because of Allah’s wrath but it might be a kind of imtehan or test so they must be regret and ask Allah to help them.