diff options
| author | 2022-11-04 16:24:00 +0200 | |
|---|---|---|
| committer | 2022-11-04 16:24:00 +0200 | |
| commit | 5cecb3be45dbd2488c75f12b0f587ddf0db0b767 (patch) | |
| tree | f9aec1c7268171ac07b92a68f9677cac5986ab4a | |
| parent | Filter match now shows all triggers instead of messages (diff) | |
Edit description on filter add/edit, bug fixes
`process_content` was changed to `process_input` to also edit the description when necessary.
Fixes unhandled filter_type being None on filter add/edit.
Moves the missing filter type warning to a more appropriate location to not trigger during filter add/edit.
Diffstat (limited to '')
| -rw-r--r-- | bot/exts/filtering/_filter_lists/filter_list.py | 13 | ||||
| -rw-r--r-- | bot/exts/filtering/_filter_lists/unique.py | 3 | ||||
| -rw-r--r-- | bot/exts/filtering/_filters/domain.py | 8 | ||||
| -rw-r--r-- | bot/exts/filtering/_filters/extension.py | 8 | ||||
| -rw-r--r-- | bot/exts/filtering/_filters/filter.py | 6 | ||||
| -rw-r--r-- | bot/exts/filtering/_filters/invite.py | 12 | ||||
| -rw-r--r-- | bot/exts/filtering/_filters/token.py | 8 | ||||
| -rw-r--r-- | bot/exts/filtering/_ui/filter.py | 9 | ||||
| -rw-r--r-- | bot/exts/filtering/filtering.py | 7 | 
9 files changed, 45 insertions, 29 deletions
diff --git a/bot/exts/filtering/_filter_lists/filter_list.py b/bot/exts/filtering/_filter_lists/filter_list.py index f3761fbf9..9ae45bfaf 100644 --- a/bot/exts/filtering/_filter_lists/filter_list.py +++ b/bot/exts/filtering/_filter_lists/filter_list.py @@ -152,6 +152,8 @@ class FilterList(dict[ListType, AtomicList], typing.Generic[T], FieldRequiring):      # Names must be unique across all filter lists.      name = FieldRequiring.MUST_SET_UNIQUE +    _already_warned = set() +      def add_list(self, list_data: dict) -> AtomicList:          """Add a new type of list (such as a whitelist or a blacklist) this filter list."""          actions, validations = create_settings(list_data["settings"], keep_empty=True) @@ -192,11 +194,14 @@ class FilterList(dict[ListType, AtomicList], typing.Generic[T], FieldRequiring):      def _create_filter(self, filter_data: dict, defaults: Defaults) -> T | None:          """Create a filter from the given data."""          try: -            filter_type = self.get_filter_type(filter_data["content"]) +            content = filter_data["content"] +            filter_type = self.get_filter_type(content)              if filter_type:                  return filter_type(filter_data, defaults) -            else: -                return None +            elif content not in self._already_warned: +                log.warn(f"A filter named {content} was supplied, but no matching implementation found.") +                self._already_warned.add(content) +            return None          except TypeError as e:              log.warning(e) @@ -240,8 +245,6 @@ class UniquesListBase(FilterList[UniqueFilter]):      Each unique filter subscribes to a subset of events to respond to.      """ -    _already_warned = set() -      def __init__(self, filtering_cog: 'Filtering'):          super().__init__()          self.filtering_cog = filtering_cog diff --git a/bot/exts/filtering/_filter_lists/unique.py b/bot/exts/filtering/_filter_lists/unique.py index 2cc1b78b2..fdf22e3b6 100644 --- a/bot/exts/filtering/_filter_lists/unique.py +++ b/bot/exts/filtering/_filter_lists/unique.py @@ -24,9 +24,6 @@ class UniquesList(UniquesListBase):          try:              return unique_filter_types[content]          except KeyError: -            if content not in self._already_warned: -                log.warn(f"A unique filter named {content} was supplied, but no matching implementation found.") -                self._already_warned.add(content)              return None      async def actions_for( diff --git a/bot/exts/filtering/_filters/domain.py b/bot/exts/filtering/_filters/domain.py index 4cc3a6f5a..7c229fdcb 100644 --- a/bot/exts/filtering/_filters/domain.py +++ b/bot/exts/filtering/_filters/domain.py @@ -50,13 +50,13 @@ class DomainFilter(Filter):          return False      @classmethod -    async def process_content(cls, content: str) -> str: +    async def process_input(cls, content: str, description: str) -> tuple[str, str]:          """ -        Process the content into a form which will work with the filtering. +        Process the content and description into a form which will work with the filtering. -        A ValueError should be raised if the content can't be used. +        A BadArgument should be raised if the content can't be used.          """          match = URL_RE.fullmatch(content)          if not match or not match.group(1):              raise BadArgument(f"`{content}` is not a URL.") -        return match.group(1) +        return match.group(1), description diff --git a/bot/exts/filtering/_filters/extension.py b/bot/exts/filtering/_filters/extension.py index f3f64532f..97eddc406 100644 --- a/bot/exts/filtering/_filters/extension.py +++ b/bot/exts/filtering/_filters/extension.py @@ -16,12 +16,12 @@ class ExtensionFilter(Filter):          return self.content in ctx.content      @classmethod -    async def process_content(cls, content: str) -> str: +    async def process_input(cls, content: str, description: str) -> tuple[str, str]:          """ -        Process the content into a form which will work with the filtering. +        Process the content and description into a form which will work with the filtering. -        A ValueError should be raised if the content can't be used. +        A BadArgument should be raised if the content can't be used.          """          if not content.startswith("."):              content = f".{content}" -        return content +        return content, description diff --git a/bot/exts/filtering/_filters/filter.py b/bot/exts/filtering/_filters/filter.py index 4ae7ec45f..45b571b54 100644 --- a/bot/exts/filtering/_filters/filter.py +++ b/bot/exts/filtering/_filters/filter.py @@ -65,13 +65,13 @@ class Filter(FieldRequiring):              return True, None      @classmethod -    async def process_content(cls, content: str) -> str: +    async def process_input(cls, content: str, description: str) -> tuple[str, str]:          """ -        Process the content into a form which will work with the filtering. +        Process the content and description into a form which will work with the filtering.          A BadArgument should be raised if the content can't be used.          """ -        return content +        return content, description      def __str__(self) -> str:          """A string representation of the filter.""" diff --git a/bot/exts/filtering/_filters/invite.py b/bot/exts/filtering/_filters/invite.py index e8f3e9851..f68adbb23 100644 --- a/bot/exts/filtering/_filters/invite.py +++ b/bot/exts/filtering/_filters/invite.py @@ -25,11 +25,11 @@ class InviteFilter(Filter):          return self.content in ctx.content      @classmethod -    async def process_content(cls, content: str) -> str: +    async def process_input(cls, content: str, description: str) -> tuple[str, str]:          """ -        Process the content into a form which will work with the filtering. +        Process the content and description into a form which will work with the filtering. -        A ValueError should be raised if the content can't be used. +        A BadArgument should be raised if the content can't be used.          """          match = DISCORD_INVITE.fullmatch(content)          if not match or not match.group("invite"): @@ -41,4 +41,8 @@ class InviteFilter(Filter):              raise BadArgument(f"`{invite_code}` is not a valid Discord invite code.")          if not invite.guild:              raise BadArgument("Did you just try to add a group DM?") -        return str(invite.guild.id) + +        guild_name = invite.guild.name if hasattr(invite.guild, "name") else "" +        if guild_name.lower() not in description.lower(): +            description = " - ".join(part for part in (f'Guild "{guild_name}"', description) if part) +        return str(invite.guild.id), description diff --git a/bot/exts/filtering/_filters/token.py b/bot/exts/filtering/_filters/token.py index f61d38846..3cd9b909d 100644 --- a/bot/exts/filtering/_filters/token.py +++ b/bot/exts/filtering/_filters/token.py @@ -22,14 +22,14 @@ class TokenFilter(Filter):          return False      @classmethod -    async def process_content(cls, content: str) -> str: +    async def process_input(cls, content: str, description: str) -> tuple[str, str]:          """ -        Process the content into a form which will work with the filtering. +        Process the content and description into a form which will work with the filtering. -        A ValueError should be raised if the content can't be used. +        A BadArgument should be raised if the content can't be used.          """          try:              re.compile(content)          except re.error as e:              raise BadArgument(str(e)) -        return content +        return content, description diff --git a/bot/exts/filtering/_ui/filter.py b/bot/exts/filtering/_ui/filter.py index 37584e9fd..e41c663c5 100644 --- a/bot/exts/filtering/_ui/filter.py +++ b/bot/exts/filtering/_ui/filter.py @@ -255,7 +255,16 @@ class FilterEditView(EditBaseView):          """          if content is not None or description is not None:              if content is not None: +                filter_type = self.filter_list.get_filter_type(content) +                if not filter_type: +                    if isinstance(interaction_or_msg, discord.Message): +                        send_method = interaction_or_msg.channel.send +                    else: +                        send_method = interaction_or_msg.response.send_message +                    await send_method(f":x: Could not find a filter type appropriate for `{content}`.") +                    return                  self.content = content +                self.filter_type = filter_type              else:                  content = self.content  # If there's no content or description, use the existing values.              if description is self._REMOVE: diff --git a/bot/exts/filtering/filtering.py b/bot/exts/filtering/filtering.py index bd1345bc0..a204e53f3 100644 --- a/bot/exts/filtering/filtering.py +++ b/bot/exts/filtering/filtering.py @@ -857,6 +857,9 @@ class Filtering(Cog):      ) -> None:          """Add a filter to the database."""          filter_type = filter_list.get_filter_type(content) +        if not filter_type: +            await ctx.reply(f":x: Could not find a filter type appropriate for `{content}`.") +            return          description, settings, filter_settings = description_and_settings_converter(              filter_list,              list_type, @@ -937,7 +940,7 @@ class Filtering(Cog):          if not valid:              raise BadArgument(f"Error while validating filter-specific settings: {error_msg}") -        content = await filter_type.process_content(content) +        content, description = await filter_type.process_input(content, description)          list_id = filter_list[list_type].id          description = description or None @@ -971,7 +974,7 @@ class Filtering(Cog):              raise BadArgument(f"Error while validating filter-specific settings: {error_msg}")          if content != filter_.content: -            content = await filter_type.process_content(content) +            content, description = await filter_type.process_input(content, description)          # If the setting is not in `settings`, the override was either removed, or there wasn't one in the first place.          for current_settings in (filter_.actions, filter_.validations):  |