allow queries in %first/%history/%last

This commit is contained in:
Klemek
2021-05-19 11:59:19 +02:00
parent c101002b6c
commit 1a17e232ed
4 changed files with 28 additions and 8 deletions
+2 -2
View File
@@ -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
+22 -2
View File
@@ -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,
+2 -2
View File
@@ -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
+2 -2
View File
@@ -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