diff options
| author | 2022-10-06 21:57:18 +0300 | |
|---|---|---|
| committer | 2022-10-06 21:57:18 +0300 | |
| commit | f12cc721737bf0bd64ddd7605cdc51ce06020b93 (patch) | |
| tree | 11074f4426673d69d32699277709662e61926ea8 | |
| parent | Fix bug with setting domain to notify (diff) | |
domain/exact -> domain/subdomains
The original plan was to have a field which only matches subdomains and not the domain itself, I got confused when I wrote the /exact field.
This fixes a bug where the matches would be updated even if it didn't meet the criteria of the extra field.
Also fixes some issues in the UI dealing with filter overrides.
Diffstat (limited to '')
| -rw-r--r-- | bot/exts/filtering/_filters/domain.py | 17 | ||||
| -rw-r--r-- | bot/exts/filtering/_ui.py | 8 |
2 files changed, 16 insertions, 9 deletions
diff --git a/bot/exts/filtering/_filters/domain.py b/bot/exts/filtering/_filters/domain.py index 00a9a886f..eed2b6721 100644 --- a/bot/exts/filtering/_filters/domain.py +++ b/bot/exts/filtering/_filters/domain.py @@ -1,5 +1,6 @@ import re from typing import ClassVar, Optional +from urllib.parse import urlparse import tldextract from pydantic import BaseModel @@ -14,11 +15,11 @@ class ExtraDomainSettings(BaseModel): """Extra settings for how domains should be matched in a message.""" exact_description: ClassVar[str] = ( - "A boolean. If True, will match the filter content exactly, and won't trigger for subdomains and subpaths." + "A boolean. If True, will will only trigger for subdomains and subpaths, and not for the domain itself." ) - # whether to match the filter content exactly, or to trigger for subdomains and subpaths as well. - exact: Optional[bool] = False + # Whether to trigger only for subdomains and subpaths, and not the specified domain itself. + subdomains: Optional[bool] = False class DomainFilter(Filter): @@ -26,7 +27,7 @@ class DomainFilter(Filter): A filter which looks for a specific domain given by URL. The schema (http, https) does not need to be included in the filter. - Will also match subdomains unless set otherwise. + Will also match subdomains. """ name = "domain" @@ -37,10 +38,14 @@ class DomainFilter(Filter): domain = tldextract.extract(self.content).registered_domain for found_url in ctx.content: - if self.content in found_url and tldextract.extract(found_url).registered_domain == domain: + extract = tldextract.extract(found_url) + if self.content in found_url and extract.registered_domain == domain: + if self.extra_fields.subdomains: + if not extract.subdomain and not urlparse(f"https://{found_url}").path: + return False ctx.matches.append(self.content) ctx.notification_domain = self.content - return not self.extra_fields.exact or self.content == found_url + return True return False @classmethod diff --git a/bot/exts/filtering/_ui.py b/bot/exts/filtering/_ui.py index 12d1f213d..ec2051083 100644 --- a/bot/exts/filtering/_ui.py +++ b/bot/exts/filtering/_ui.py @@ -433,7 +433,9 @@ class SettingsEditView(discord.ui.View): ) self.add_item(add_select) - override_names = list(settings_overrides) + list(filter_settings_overrides) + override_names = ( + list(settings_overrides) + [f"{filter_list.name}/{setting}" for setting in filter_settings_overrides] + ) remove_select = CustomCallbackSelect( self._remove_override, placeholder="Select an override to remove", @@ -568,13 +570,13 @@ class SettingsEditView(discord.ui.View): # Find the right dictionary to update. if "/" in setting_name: filter_name, setting_name = setting_name.split("/", maxsplit=1) - dict_to_edit = self.filter_settings_overrides[filter_name] + dict_to_edit = self.filter_settings_overrides else: dict_to_edit = self.settings_overrides # Update the setting override value or remove it if setting_value is not self._REMOVE: dict_to_edit[setting_name] = setting_value - else: + elif setting_name in dict_to_edit: del dict_to_edit[setting_name] # This is inefficient, but otherwise the selects go insane if the user attempts to edit the same setting |