Spectators only or unassigned players also?
You can set_task when they join spectators and remove_task when they leave spectators. In the task itself kick them. To detect if they joined spectators you better look for example into the "Full Server Idler Kicker" plugin source code.