Creating custom badge with custom rules

The easiest way is to install the discourse-data-explorer plugin, which will provide an interface like this.