command_cache for %repeat and %mobile

This commit is contained in:
Klemek
2021-04-21 20:14:06 +02:00
parent 3100e6fa20
commit 7fad35a4b3
5 changed files with 70 additions and 2 deletions
+3
View File
@@ -21,6 +21,8 @@
* %first - read first message * %first - read first message
* %rand - read a random message * %rand - read a random message
* %last - read last 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 * %gdpr - displays GDPR information
* %emojis - rank emotes by their usage * %emojis - rank emotes by their usage
* arguments: * arguments:
@@ -112,6 +114,7 @@ python3 src/main.py
* **v1.14** * **v1.14**
* `mobile/mention` arg to fix mobile bug * `mobile/mention` arg to fix mobile bug
* `%repeat`, `%mobile` to repeat commands
* **v1.13** * **v1.13**
* improved scan `%words` * improved scan `%words`
* remove old and unused logs at start and guild leaving * remove old and unused logs at start and guild leaving
+13 -1
View File
@@ -6,7 +6,7 @@ if sys.version_info < (3, 7):
print("Please upgrade your Python version to 3.7.0 or higher") print("Please upgrade your Python version to 3.7.0 or higher")
sys.exit(1) sys.exit(1)
from utils import emojis, gdpr from utils import emojis, gdpr, command_cache
from scanners import ( from scanners import (
EmotesScanner, EmotesScanner,
FullScanner, FullScanner,
@@ -71,6 +71,18 @@ bot.register_command(
"words: (BETA) rank words by their usage", "words: (BETA) rank words by their usage",
WordsScanner.help(), 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( bot.register_command(
"last", "last",
lambda *args: LastScanner().compute(*args), lambda *args: LastScanner().compute(*args),
+1
View File
@@ -1,3 +1,4 @@
from .scanner import Scanner
from .emotes_scanner import EmotesScanner from .emotes_scanner import EmotesScanner
from .frequency_scanner import FrequencyScanner from .frequency_scanner import FrequencyScanner
from .composition_scanner import CompositionScanner from .composition_scanner import CompositionScanner
+8 -1
View File
@@ -14,6 +14,7 @@ from utils import (
ISO8601_REGEX, ISO8601_REGEX,
RELATIVE_REGEX, RELATIVE_REGEX,
parse_time, parse_time,
command_cache,
) )
from logs import ( from logs import (
GuildLogs, GuildLogs,
@@ -48,7 +49,11 @@ class Scanner(ABC):
self.chan_count = 0 self.chan_count = 0
async def compute( 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) args = list(args)
guild = message.guild guild = message.guild
@@ -85,6 +90,7 @@ class Scanner(ABC):
and (not arg.isdigit() or not self.has_digit_args) and (not arg.isdigit() or not self.has_digit_args)
and arg not in str_channel_mentions and arg not in str_channel_mentions
and arg not in str_mentions and arg not in str_mentions
and arg not in other_mentions
and not skip_check and not skip_check
): ):
await message.channel.send( await message.channel.send(
@@ -240,6 +246,7 @@ class Scanner(ABC):
reference=message if first else None, reference=message if first else None,
allowed_mentions=allowed_mentions, allowed_mentions=allowed_mentions,
) )
command_cache.cache(self, message, args)
# Delete custom progress message # Delete custom progress message
await progress.delete() await progress.delete()
+45
View File
@@ -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
)