>>213960840
Ok, given you've done none of the research, which you should - that isn't God's command but Moses.
Even just glancing at the chapter itself should yield sufficient context.
The Bible in many parts is descriptive (not prescriptive), describing things that happened: like a Chronicle.
This is btw during the war with the Midians, and mass corruption from the Midians (read earlier parts of Numbers).
The common understanding is that:
1) it wasn't sexual slavery, but taking them into the nation
2) complete elimination and punishment of the Midians for their actions
3) much of the OT's evolving moral nature (from cruder to more refined culminating in the NT) is understood as God's way of morally educating Israel over time - a thing explicitly mentioned ("hard necked people").