diff options
| -rw-r--r-- | bot/__init__.py | 75 | ||||
| -rw-r--r-- | bot/cogs/tags.py | 10 |
2 files changed, 69 insertions, 16 deletions
diff --git a/bot/__init__.py b/bot/__init__.py index 59b0eb2fd..95ea77d96 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -1,8 +1,10 @@ # coding=utf-8 +import ast + import discord.ext.commands.view -def case_insensitive_skip_string(self, string: str) -> bool: +def _skip_string(self, string: str) -> bool: """ Our version of the skip_string method from discord.ext.commands.view; used to find @@ -18,24 +20,71 @@ def case_insensitive_skip_string(self, string: str) -> bool: return False -def case_insensitive_get_word(self) -> str: +def _get_word(self) -> str: """ Invokes the get_word method from discord.ext.commands.view used to find the bot command part of a message, but - allows the command to ignore case sensitivity + allows the command to ignore case sensitivity, + and allows commands to have Python syntax. + + Example of valid Python syntax calls: + ------------------------------ + bot.tags.set("test", 'a dark, dark night') + bot.help(tags.delete) + bot.hELP(tags.delete) """ - word = _get_word(self) - if isinstance(word, str): - return word.lower() - return word + pos = 0 + while not self.eof: + try: + current = self.buffer[self.index + pos] + if current.isspace() or current == "(": + break + pos += 1 + except IndexError: + break + + self.previous = self.index + result = self.buffer[self.index:self.index + pos] + self.index += pos + + if current == "(" and self.buffer[self.index + 1] != ")": + + # Parse the args + args = self.buffer[self.index:] + args = ast.literal_eval(args) + + # Force args into container + if isinstance(args, str): + args = (args,) + + # Type validate and format + new_args = [] + for arg in args: + + # Other types get converted to strings + if not isinstance(arg, str): + arg = str(arg) + + # Adding double quotes to every argument + new_args.append(f'"{arg}"') + + new_args = " ".join(new_args) + self.buffer = f"{self.buffer[:self.index]} {new_args}" + self.end = len(self.buffer) # Recalibrate the end since we've removed commas + + else: + # Move the cursor to capture the ()'s + pos += 2 + result = self.buffer[self.previous:self.index + (pos+2)] + self.index += 2 + if isinstance(result, str): + return result.lower() # Case insensitivity, baby + return result -# Save the old methods -_skip_string = discord.ext.commands.view.StringView.skip_string -_get_word = discord.ext.commands.view.StringView.get_word -# Monkey patch them to be case insensitive -discord.ext.commands.view.StringView.skip_string = case_insensitive_skip_string -discord.ext.commands.view.StringView.get_word = case_insensitive_get_word +# Monkey patch the methods +discord.ext.commands.view.StringView.skip_string = _skip_string +discord.ext.commands.view.StringView.get_word = _get_word diff --git a/bot/cogs/tags.py b/bot/cogs/tags.py index 2bb939ff0..be57ed483 100644 --- a/bot/cogs/tags.py +++ b/bot/cogs/tags.py @@ -204,9 +204,13 @@ class Tags: embed.title = "Please don't do that" embed.description = "Don't be ridiculous. Newlines are obviously not allowed in the tag name." - if not tag_name.isalpha(): + elif tag_name.isdigit(): embed.title = "Please don't do that" - embed.description = "Only letters in the tag names, please." + embed.description = "Tag names can't be numbers." + + elif not tag_content.strip(): + embed.title = "Please don't do that" + embed.description = "Tags should not be empty, or filled with whitespace." else: if not (tag_name and tag_content): @@ -222,7 +226,7 @@ class Tags: embed.title = "Tag successfully added" embed.description = f"**{tag_name}** added to tag database." else: - embed.title = "Database error", + embed.title = "Database error" embed.description = ("There was a problem adding the data to the tags database. " "Please try again. If the problem persists, check the API logs.") |