Sure you can query database state and filter out the unwanted:
select name, state_desc
from sys.databases
where not state_desc in ('Offline', 'Recovering')
See https://learn.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-databases-transact-sql?view=sql-server-ver15
for all existing states