diff options
| author | 2020-03-08 21:30:18 +0700 | |
|---|---|---|
| committer | 2020-03-08 21:30:18 +0700 | |
| commit | dd707182b4f4f4ce98353d5c82092f48dd8fb5c2 (patch) | |
| tree | 1948d5e4534eca424c995236287e5a06dc178ee2 | |
| parent | Fixed searching for `,` returing all tags. Made it more descriptive when mult... (diff) | |
Refactored dense codes, removed obvious type hint.
- Show the process of sanitizing the List[str] `keywords_processed`.
- Show the process of finding tag for `matching_tags` ( was `founds` ).
- Refactored the logic to find boolean `is_plural`.
- Minor wording changes for docstring.
| -rw-r--r-- | bot/cogs/tags.py | 40 | 
1 files changed, 25 insertions, 15 deletions
| diff --git a/bot/cogs/tags.py b/bot/cogs/tags.py index 49ed87c92..89f3acb6d 100644 --- a/bot/cogs/tags.py +++ b/bot/cogs/tags.py @@ -1,7 +1,7 @@  import logging  import re  import time -from typing import Callable, Dict, Iterable, List, Optional, Tuple +from typing import Callable, Dict, Iterable, List, Optional  from discord import Colour, Embed  from discord.ext.commands import Cog, Context, group @@ -90,27 +90,37 @@ class Tags(Cog):          """          Search for tags via contents. -        `predicate` will be either any or all, or a custom callable to search. Must return a bool. +        `predicate` will be the built-in any, all, or a custom callable. Must return a bool.          """          await self._get_tags() -        keywords_processed: Tuple[str] = tuple(query.strip().casefold() for query in keywords.split(',') if query) -        keywords_processed = keywords_processed or (keywords,) -        founds: list = [ -            tag -            for tag in self._cache.values() -            if check(query in tag['embed']['description'] for query in keywords_processed) -        ] - -        if not founds: +        keywords_processed: List[str] = [] +        for keyword in keywords.split(','): +            keyword_sanitized = keyword.strip().casefold() +            if not keyword_sanitized: +                # this happens when there are leading / trailing / consecutive comma. +                continue +            keywords_processed.append(keyword_sanitized) + +        if not keywords_processed: +            # after sanitizing, we can end up with an empty list, for example when keywords is ',' +            # in that case, we simply want to search for such keywords directly instead. +            keywords_processed = [keywords] + +        matching_tags = [] +        for tag in self._cache.values(): +            if check(query in tag['embed']['description'].casefold() for query in keywords_processed): +                matching_tags.append(tag) + +        if not matching_tags:              return None -        elif len(founds) == 1: -            return Embed().from_dict(founds[0]['embed']) +        elif len(matching_tags) == 1: +            return Embed().from_dict(matching_tags[0]['embed'])          else: -            is_plural: bool = len(keywords_processed) > 1 or any(kw.count(' ') for kw in keywords_processed) +            is_plural = len(keywords_processed) > 1 or keywords.strip().count(' ') > 1              embed = Embed(                  title=f"Here are the tags containing the given keyword{'s' * is_plural}:", -                description='\n'.join(tag['title'] for tag in founds[:10]) +                description='\n'.join(tag['title'] for tag in matching_tags[:10])              )              embed.set_footer(text=f"Keyword{'s' * is_plural} used: {keywords}"[:1024])              return embed | 
