diff options
| author | 2018-04-09 08:56:58 +0100 | |
|---|---|---|
| committer | 2018-04-09 09:56:58 +0200 | |
| commit | 1af13364d954363ec7a4d6acc2247797de535f8a (patch) | |
| tree | f712b9e0b0b9dfc7df912aadaea3531c01613f7b | |
| parent | Bump safety from 1.8.0 to 1.8.1 (#45) (diff) | |
Sort out argument parsing (#41)
* Sort out tag name literal eval
* Fixed typo >.>
* Validate all tag commands
* Clean up validation
* Add default embed colours
* Sorted a docstring
* Typehinted validate method
* Explicitly return None
* Squashed some parsing bugs
* Cleaned up tag name ascii checking
* Removed unused import >.>
* More validation fixes
* Uncommented permission decorators <.<
* Tidied up a bit
* Tidied up a bit
Diffstat (limited to '')
| -rw-r--r-- | bot/__init__.py | 2 | ||||
| -rw-r--r-- | bot/cogs/tags.py | 70 | ||||
| -rw-r--r-- | tox.ini | 2 | 
3 files changed, 41 insertions, 33 deletions
diff --git a/bot/__init__.py b/bot/__init__.py index df76215e6..071ec6c98 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -137,7 +137,7 @@ def _get_word(self) -> str:          args = ast.literal_eval(args)          # Force args into container -        if isinstance(args, str): +        if not isinstance(args, tuple):              args = (args,)          # Type validate and format diff --git a/bot/cogs/tags.py b/bot/cogs/tags.py index 3e08778b1..645835f53 100644 --- a/bot/cogs/tags.py +++ b/bot/cogs/tags.py @@ -1,4 +1,5 @@  import logging +import random  import time  from typing import Optional @@ -17,6 +18,18 @@ class Tags:      Save new tags and fetch existing tags.      """ +    FAIL_TITLES = [ +        "Please don't do that.", +        "You have to stop.", +        "Do you mind?", +        "In the future, don't do that.", +        "That was a mistake.", +        "You blew it.", +        "You're bad at computers.", +        "Are you trying to kill me?", +        "Noooooo!!" +    ] +      def __init__(self, bot: AutoShardedBot):          self.bot = bot          self.tag_cooldowns = {} @@ -96,9 +109,9 @@ class Tags:          :return: A validation embed if invalid, otherwise None          """ -        def is_number(string): +        def is_number(value):              try: -                float(string) +                float(value)              except ValueError:                  return False              else: @@ -107,30 +120,16 @@ class Tags:          embed = Embed()          embed.colour = Colour.red() -        # Replace any special characters -        raw_name = tag_name.translate( -            { -                0x8: "\\b",  # Backspace -                0x9: "\\t",  # Horizontal tab -                0xA: "\\n",  # Linefeed -                0xB: "\\v",  # Vertical tab -                0xC: "\\f",  # Form feed -                0xD: "\\r"   # Carriage return -            } -        ) - -        # 'tag_name' has at least one special character. -        if tag_name != raw_name: -            log.warning(f"{author} tried to put a special character in a tag name. " +        # 'tag_name' has at least one invalid character. +        if ascii(tag_name)[1:-1] != tag_name: +            log.warning(f"{author} tried to put an invalid character in a tag name. "                          "Rejecting the request.") -            embed.title = "Please don't do that" -            embed.description = "Don't be ridiculous, special characters obviously aren't allowed in the tag name" +            embed.description = "Don't be ridiculous, you can't use that character!"          # 'tag_content' or 'tag_name' are either empty, or consist of nothing but whitespace          elif (tag_content is not None and not tag_content) or not tag_name:              log.warning(f"{author} tried to create a tag with a name consisting only of whitespace. "                          "Rejecting the request.") -            embed.title = "Please don't do that"              embed.description = "Tags should not be empty, or filled with whitespace."          # 'tag_name' is a number of some kind, we don't allow that. @@ -138,12 +137,18 @@ class Tags:              log.error("inside the is_number")              log.warning(f"{author} tried to create a tag with a digit as its name. "                          "Rejecting the request.") -            embed.title = "Please don't do that"              embed.description = "Tag names can't be numbers." +        # 'tag_name' is longer than 127 characters +        elif len(tag_name) > 127: +            log.warning(f"{author} tried to request a tag name with over 127 characters. " +                        "Rejecting the request.") +            embed.description = "Are you insane? That's way too long!" +          else:              return None +        embed.title = random.choice(Tags.FAIL_TITLES)          return embed      @command(name="tags()", aliases=["tags"], hidden=True) @@ -158,7 +163,7 @@ class Tags:          return await ctx.invoke(self.bot.get_command("help"), "Tags")      @command(name="tags.get()", aliases=["tags.get", "tags.show()", "tags.show", "get_tag"]) -    async def get_command(self, ctx: Context, tag_name=None): +    async def get_command(self, ctx: Context, tag_name: str=None):          """          Get a list of all tags or a specified tag. @@ -198,11 +203,13 @@ class Tags:              return          tags = [] -        tag_name = tag_name.lower().strip() -        validation = await self.validate(ctx.author, tag_name) -        if validation is not None: -            return await ctx.send(embed=validation) +        if tag_name is not None: +            tag_name = tag_name.lower().strip() +            validation = await self.validate(ctx.author, tag_name) + +            if validation is not None: +                return await ctx.send(embed=validation)          embed = Embed()          embed.colour = Colour.red() @@ -237,14 +244,14 @@ class Tags:              if isinstance(tag_data, dict):                  log.warning(f"{ctx.author} requested the tag '{tag_name}', but it could not be found.") -                embed.description = f"Unknown tag: **{tag_name}**" +                embed.description = f"**{tag_name}** is an unknown tag name. Please check the spelling and try again."              else:                  log.warning(f"{ctx.author} requested a list of all tags, but the tags database was empty!")                  embed.description = "**There are no tags in the database!**"              if tag_name:                  embed.set_footer(text="To show a list of all tags, use bot.tags.get().") -                embed.title = "Tag not found" +                embed.title = "Tag not found."          # Paginate if this is a list of all tags          if tags: @@ -270,13 +277,14 @@ class Tags:          :param tag_content: The content of the tag.          """ -        tag_name = tag_name.lower().strip() -        tag_content = tag_content.strip()          validation = await self.validate(ctx.author, tag_name, tag_content)          if validation is not None:              return await ctx.send(embed=validation) +        tag_name = tag_name.lower().strip() +        tag_content = tag_content.strip() +          embed = Embed()          embed.colour = Colour.red()          tag_data = await self.post_tag_data(tag_name, tag_content) @@ -309,12 +317,12 @@ class Tags:          :param tag_name: The name of the tag to delete.          """ -        tag_name = tag_name.lower().strip()          validation = await self.validate(ctx.author, tag_name)          if validation is not None:              return await ctx.send(embed=validation) +        tag_name = tag_name.lower().strip()          embed = Embed()          embed.colour = Colour.red()          tag_data = await self.delete_tag_data(tag_name) @@ -2,5 +2,5 @@  max-line-length=120  application_import_names=bot  exclude=.venv -ignore=B311,W503,E226 +ignore=B311,W503,E226,S311  import-order-style=pycharm  |