From 1a17e232ed9110ba9c846ca6ad7533fc8834a68b Mon Sep 17 00:00:00 2001 From: Klemek Date: Wed, 19 May 2021 11:59:19 +0200 Subject: [PATCH] allow queries in %first/%history/%last --- src/scanners/first_scanner.py | 4 ++-- src/scanners/history_scanner.py | 24 ++++++++++++++++++++++-- src/scanners/last_scanner.py | 4 ++-- src/scanners/random_scanner.py | 4 ++-- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/scanners/first_scanner.py b/src/scanners/first_scanner.py index ab98add..0a2bb63 100644 --- a/src/scanners/first_scanner.py +++ b/src/scanners/first_scanner.py @@ -9,10 +9,10 @@ from utils import generate_help class FirstScanner(HistoryScanner): @staticmethod def help() -> str: - return generate_help("first", "Read first message") + return generate_help("first", "Read first message (add text to filter like %find)") def __init__(self): - super().__init__(help=FirstScanner.help()) + super().__init__(help=FirstScanner.help(), allow_queries=True) def allow_message(self, *_) -> bool: return True diff --git a/src/scanners/history_scanner.py b/src/scanners/history_scanner.py index 7ddcbaf..1848dd5 100644 --- a/src/scanners/history_scanner.py +++ b/src/scanners/history_scanner.py @@ -1,6 +1,7 @@ from abc import ABC, abstractmethod from typing import Callable, List import discord +import re # Custom libs @@ -10,17 +11,23 @@ from logs import ChannelLogs, MessageLog class HistoryScanner(Scanner, ABC): - def __init__(self, *, valid_args: List[str] = [], help: str): + def __init__(self, *, help: str, valid_args: List[str] = [], allow_queries: bool = False): super().__init__( has_digit_args=True, valid_args=["all", "everyone"] + valid_args, help=help, intro_context="", + all_args=allow_queries, ) + self.allow_queries = allow_queries async def init(self, message: discord.Message, *args: str) -> bool: self.history = History() self.all_messages = "all" in args or "everyone" in args + if self.allow_queries: + self.queries = [(query.lower(), query.strip("`") if re.match(r"^`.*`$", query) else None) for query in self.other_args] + else: + self.queries = [] return True def compute_message(self, channel: ChannelLogs, message: MessageLog): @@ -30,7 +37,7 @@ class HistoryScanner(Scanner, ABC): self.history, self.raw_members, all_messages=self.all_messages, - allow_message=self.allow_message, + allow_message=lambda *args: self.allow_message(*args) and self.allow_message_query(*args), ) @abstractmethod @@ -41,6 +48,19 @@ class HistoryScanner(Scanner, ABC): def allow_message(self, channel: ChannelLogs, message: MessageLog) -> bool: pass + def allow_message_query(self, channel: ChannelLogs, message: MessageLog) -> bool: + if not self.allow_queries or len(self.queries) == 0: + return True + else: + content = message.content.lower() + for query in self.queries: + if query[1] is not None: + if not re.match(query[1], message.content): + return False + elif not query[0] in content: + return False + return True + @staticmethod def analyse_message( channel: ChannelLogs, diff --git a/src/scanners/last_scanner.py b/src/scanners/last_scanner.py index eb69fb4..792c2ff 100644 --- a/src/scanners/last_scanner.py +++ b/src/scanners/last_scanner.py @@ -9,10 +9,10 @@ from utils import generate_help class LastScanner(HistoryScanner): @staticmethod def help() -> str: - return generate_help("last", "Read last message") + return generate_help("last", "Read last message (add text to filter like %find)") def __init__(self): - super().__init__(help=LastScanner.help()) + super().__init__(help=LastScanner.help(), allow_queries=True) def allow_message(self, *_) -> bool: return True diff --git a/src/scanners/random_scanner.py b/src/scanners/random_scanner.py index 990ebd1..c42d249 100644 --- a/src/scanners/random_scanner.py +++ b/src/scanners/random_scanner.py @@ -9,10 +9,10 @@ from utils import generate_help class RandomScanner(HistoryScanner): @staticmethod def help() -> str: - return generate_help("rand", "Read a random message") + return generate_help("rand", "Read a random message (add text to filter like %find)") def __init__(self): - super().__init__(help=RandomScanner.help()) + super().__init__(help=RandomScanner.help(), allow_queries=True) def allow_message(self, *_) -> bool: return True