Looking for a script to block bad blocks using workers

Looking for a script to block bad blocks using workers and allow bots like google,yahoo,twitter,facebook

Can worker perform this?

I am not sure what workers do?

There’s a recipe on this page to deny a spider or crawler:
https://developers.cloudflare.com/workers/recipes/return-403/
I’m not a Javascript guy, but I suspect it’s not too difficult to invert the match and permit good bots.