Maybe a good option is to use ethereum.isConnected().

More details here:
https://docs.metamask.io/guide/ethereum-provider.html#methods