diff options
-rw-r--r-- | bot/__init__.py | 47 | ||||
-rw-r--r-- | bot/cogs/bot.py | 40 | ||||
-rw-r--r-- | bot/cogs/fun.py | 4 | ||||
-rw-r--r-- | bot/constants.py | 1 |
4 files changed, 62 insertions, 30 deletions
diff --git a/bot/__init__.py b/bot/__init__.py index 4d78b238c..d4b9b13a0 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -103,7 +103,7 @@ def _get_word(self) -> str: # Check what's after the '(' or '[' next_char = None - if len(self.buffer) != self.index: + if len(self.buffer) - 1 != self.index: next_char = self.buffer[self.index + 1] # Catch raw channel, member or role mentions and wrap them in quotes. @@ -136,6 +136,13 @@ def _get_word(self) -> str: log.trace(f"Returning {self.buffer[self.previous:self.index]}") parsed_result = self.buffer[self.previous:self.index] + elif current == "(" or current == "[" and not next_char: + + # Just remove the start bracket + log.debug("User called command with a single bracket. Removing bracket.") + parsed_result = self.buffer[self.previous:self.index] + args = None + # Check if a command in the form of `bot.tags['ask']` # or alternatively `bot.tags['ask'] = 'whatever'` was used. elif current == "[": @@ -183,29 +190,35 @@ def _get_word(self) -> str: args = '' log.trace(f"Command is of unknown syntax: {self.buffer}") - # Force args into container - if not isinstance(args, tuple): - args = (args,) - - # Type validate and format + # Args handling new_args = [] - for arg in args: + if args: + # Force args into container + if not isinstance(args, tuple): + args = (args,) - # Other types get converted to strings - if not isinstance(arg, str): - log.trace(f"{arg} is not a str, casting to str.") - arg = str(arg) + # Type validate and format + for arg in args: - # Allow using double quotes within triple double quotes - arg = arg.replace('"', '\\"') + # Other types get converted to strings + if not isinstance(arg, str): + log.trace(f"{arg} is not a str, casting to str.") + arg = str(arg) - # Adding double quotes to every argument - log.trace("Wrapping all args in double quotes.") - new_args.append(f'"{arg}"') + # Allow using double quotes within triple double quotes + arg = arg.replace('"', '\\"') + + # Adding double quotes to every argument + log.trace("Wrapping all args in double quotes.") + new_args.append(f'"{arg}"') # Reconstruct valid discord.py syntax prefix = self.buffer[:self.previous] - self.buffer = f"{prefix}{parsed_result} {' '.join(new_args)}" + self.buffer = f"{prefix}{parsed_result}" + + if new_args: + self.buffer += ' '.join(new_args) + self.index = len(f"{prefix}{parsed_result}") self.end = len(self.buffer) log.trace(f"Modified the buffer. New buffer is now '{self.buffer}'") diff --git a/bot/cogs/bot.py b/bot/cogs/bot.py index e64d928fa..e1571313f 100644 --- a/bot/cogs/bot.py +++ b/bot/cogs/bot.py @@ -8,7 +8,7 @@ from discord.ext.commands import AutoShardedBot, Context, command, group from dulwich.repo import Repo from bot.constants import ( - ADMIN_ROLE, BOT_AVATAR_URL, BOT_CHANNEL, + ADMIN_ROLE, BOT_AVATAR_URL, BOT_COMMANDS_CHANNEL, DEVTEST_CHANNEL, HELP1_CHANNEL, HELP2_CHANNEL, HELP3_CHANNEL, HELP4_CHANNEL, MODERATOR_ROLE, OWNER_ROLE, PYTHON_CHANNEL, PYTHON_GUILD, VERIFIED_ROLE @@ -26,17 +26,21 @@ class Bot: def __init__(self, bot: AutoShardedBot): self.bot = bot - # Stores allowed channels plus unix timestamp from last call. + # Stores allowed channels plus epoch time since last call. self.channel_cooldowns = { HELP1_CHANNEL: 0, HELP2_CHANNEL: 0, HELP3_CHANNEL: 0, HELP4_CHANNEL: 0, PYTHON_CHANNEL: 0, - DEVTEST_CHANNEL: 0, - BOT_CHANNEL: 0 } + # These channels will also work, but will not be subject to cooldown + self.channel_whitelist = ( + BOT_COMMANDS_CHANNEL, + DEVTEST_CHANNEL, + ) + @group(invoke_without_command=True, name="bot", hidden=True) @with_role(VERIFIED_ROLE) async def bot_group(self, ctx: Context): @@ -225,16 +229,31 @@ class Bot: final += line[4:] log.trace(f"Formatted: \n\n{msg}\n\n to \n\n{final}\n\n") if not final: - log.debug(f"Found no REPL code in \n\n{msg}\n\n") + log.trace(f"Found no REPL code in \n\n{msg}\n\n") return msg, False else: - log.debug(f"Found REPL code in \n\n{msg}\n\n") + log.trace(f"Found REPL code in \n\n{msg}\n\n") return final.rstrip(), True async def on_message(self, msg: Message): - if msg.channel.id in self.channel_cooldowns and not msg.author.bot and len(msg.content.splitlines()) > 3: - on_cooldown = time.time() - self.channel_cooldowns[msg.channel.id] < 300 - if not on_cooldown or msg.channel.id == DEVTEST_CHANNEL: + """ + Detect poorly formatted Python code and send the user + a helpful message explaining how to do properly + formatted Python syntax highlighting codeblocks. + """ + + parse_codeblock = ( + ( + msg.channel.id in self.channel_cooldowns + or msg.channel.id in self.channel_whitelist + ) + and not msg.author.bot + and len(msg.content.splitlines()) > 3 + ) + + if parse_codeblock: + on_cooldown = (time.time() - self.channel_cooldowns.get(msg.channel.id, 0)) < 300 + if not on_cooldown: try: not_backticks = ["'''", '"""', "´´´", "‘‘‘", "’’’", "′′′", "“““", "”””", "″″″", "〃〃〃"] bad_ticks = msg.content[:3] in not_backticks @@ -324,7 +343,8 @@ class Bot: else: return - self.channel_cooldowns[msg.channel.id] = time.time() + if msg.channel.id not in self.channel_whitelist: + self.channel_cooldowns[msg.channel.id] = time.time() except SyntaxError: log.trace( diff --git a/bot/cogs/fun.py b/bot/cogs/fun.py index 0689d82dc..4ce3fc27f 100644 --- a/bot/cogs/fun.py +++ b/bot/cogs/fun.py @@ -3,7 +3,7 @@ import logging from discord import Message from discord.ext.commands import AutoShardedBot -from bot.constants import BOT_CHANNEL +from bot.constants import BOT_COMMANDS_CHANNEL RESPONSES = { "_pokes {us}_": "_Pokes {them}_", @@ -33,7 +33,7 @@ class Fun: del RESPONSES[key] async def on_message(self, message: Message): - if message.channel.id != BOT_CHANNEL: + if message.channel.id != BOT_COMMANDS_CHANNEL: return content = message.content diff --git a/bot/constants.py b/bot/constants.py index 7b2d8214a..4e8c9c900 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -4,7 +4,6 @@ import os PYTHON_GUILD = 267624335836053506 # Channels -BOT_CHANNEL = 267659945086812160 BOT_COMMANDS_CHANNEL = 267659945086812160 CHECKPOINT_TEST_CHANNEL = 422077681434099723 DEVLOG_CHANNEL = 409308876241108992 |