diff --git a/README.md b/README.md index ebde127..d699661 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,8 @@ * %first - read first message * %rand - read a random message * %last - read last message +* %repeat - repeat last analysis (adding supplied arguments) +* %mobile - fix @invalid-user for last command but mentions users * %gdpr - displays GDPR information * %emojis - rank emotes by their usage * arguments: @@ -112,6 +114,7 @@ python3 src/main.py * **v1.14** * `mobile/mention` arg to fix mobile bug + * `%repeat`, `%mobile` to repeat commands * **v1.13** * improved scan `%words` * remove old and unused logs at start and guild leaving diff --git a/src/main.py b/src/main.py index 53d4472..fb3c194 100644 --- a/src/main.py +++ b/src/main.py @@ -6,7 +6,7 @@ if sys.version_info < (3, 7): print("Please upgrade your Python version to 3.7.0 or higher") sys.exit(1) -from utils import emojis, gdpr +from utils import emojis, gdpr, command_cache from scanners import ( EmotesScanner, FullScanner, @@ -71,6 +71,18 @@ bot.register_command( "words: (BETA) rank words by their usage", WordsScanner.help(), ) +bot.register_command( + "repeat", + command_cache.repeat, + "repeat: repeat last analysis (adding supplied arguments)", + "```\n%repeat: repeat last analysis (adding supplied arguments)\n```", +) +bot.register_command( + "mobile", + lambda *args: command_cache.repeat(*args, add_args=["mobile"]), + "mobile: fix @invalid-user for last command but mentions users", + "```\n%mobile: fix @invalid-user for last command but mentions users\n```", +) bot.register_command( "last", lambda *args: LastScanner().compute(*args), diff --git a/src/scanners/__init__.py b/src/scanners/__init__.py index ed9141d..8ac7b97 100644 --- a/src/scanners/__init__.py +++ b/src/scanners/__init__.py @@ -1,3 +1,4 @@ +from .scanner import Scanner from .emotes_scanner import EmotesScanner from .frequency_scanner import FrequencyScanner from .composition_scanner import CompositionScanner diff --git a/src/scanners/scanner.py b/src/scanners/scanner.py index eb4d30c..934c334 100644 --- a/src/scanners/scanner.py +++ b/src/scanners/scanner.py @@ -14,6 +14,7 @@ from utils import ( ISO8601_REGEX, RELATIVE_REGEX, parse_time, + command_cache, ) from logs import ( GuildLogs, @@ -48,7 +49,11 @@ class Scanner(ABC): self.chan_count = 0 async def compute( - self, client: discord.client, message: discord.Message, *args: str + self, + client: discord.client, + message: discord.Message, + *args: str, + other_mentions: List[str] = [], ): args = list(args) guild = message.guild @@ -85,6 +90,7 @@ class Scanner(ABC): and (not arg.isdigit() or not self.has_digit_args) and arg not in str_channel_mentions and arg not in str_mentions + and arg not in other_mentions and not skip_check ): await message.channel.send( @@ -240,6 +246,7 @@ class Scanner(ABC): reference=message if first else None, allowed_mentions=allowed_mentions, ) + command_cache.cache(self, message, args) # Delete custom progress message await progress.delete() diff --git a/src/utils/command_cache.py b/src/utils/command_cache.py new file mode 100644 index 0000000..4cdbf7b --- /dev/null +++ b/src/utils/command_cache.py @@ -0,0 +1,45 @@ +from typing import List +import logging +import discord + +from scanners import Scanner + +command_cache = {} + + +def cache(scanner: Scanner, message: discord.Message, args: List[str]): + id = message.channel.id + command_cache[id] = ( + type(scanner), + list(args), + [str(channel.id) for channel in message.channel_mentions] + + [str(member.id) for member in message.mentions], + ) + + +async def repeat( + client: discord.client, + message: discord.Message, + *args: str, + add_args: List[str] = [], +): + if "help" in args: + await client.bot.help(client, message, "help", args[0]) + return + id = message.channel.id + if id not in command_cache: + await message.channel.send( + "No command to repeat on this channel (type %help for more info)", + reference=message, + ) + return + ( + scannerType, + original_args, + original_mentions, + ) = command_cache[id] + args = original_args + add_args + list(args[1:]) + ["fast"] + logging.info(f"repeating {args}") + await scannerType().compute( + client, message, *args, other_mentions=original_mentions + )