history commands

This commit is contained in:
Klemek
2021-04-06 23:01:51 +02:00
parent aa32382e1d
commit e9505fa082
14 changed files with 215 additions and 11 deletions
+3
View File
@@ -8,3 +8,6 @@ from .mentioned_scanner import MentionedScanner
from .messages_scanner import MessagesScanner
from .channels_scanner import ChannelsScanner
from .reactions_scanner import ReactionsScanner
from .first_scanner import FirstScanner
from .last_scanner import LastScanner
from .random_scanner import RandomScanner
+19
View File
@@ -0,0 +1,19 @@
from typing import List
# Custom libs
from .history_scanner import HistoryScanner
class FirstScanner(HistoryScanner):
@staticmethod
def help() -> str:
return super(FirstScanner, FirstScanner).help(
cmd="first", text="Read first message"
)
def __init__(self):
super().__init__(help=FirstScanner.help())
def get_results(self, intro: str) -> List[str]:
return self.history.to_string(type="first")
+70
View File
@@ -0,0 +1,70 @@
from abc import ABC, abstractmethod
from typing import List
import discord
# Custom libs
from .scanner import Scanner
from data_types import History
from logs import ChannelLogs, MessageLog
from utils import COMMON_HELP_ARGS
class HistoryScanner(Scanner, ABC):
@staticmethod
def help(*, cmd: str, text: str) -> str:
return (
"```\n"
+ f"%{cmd}: {text}\n"
+ "arguments:\n"
+ COMMON_HELP_ARGS
+ "* all/everyone - include bots\n"
+ "Example: %{cmd} #mychannel1 @user\n"
+ "```"
)
def __init__(self, *, help: str):
super().__init__(
has_digit_args=True,
valid_args=["all", "everyone"],
help=help,
intro_context="",
)
async def init(self, message: discord.Message, *args: str) -> bool:
self.history = History()
self.all_messages = "all" in args or "everyone" in args
return True
def compute_message(self, channel: ChannelLogs, message: MessageLog):
return HistoryScanner.analyse_message(
channel,
message,
self.history,
self.raw_members,
all_messages=self.all_messages,
)
@abstractmethod
def get_results(self, intro: str):
pass
@staticmethod
def analyse_message(
channel: ChannelLogs,
message: MessageLog,
history: History,
raw_members: List[int],
*,
all_messages: bool,
) -> bool:
impacted = False
# If author is included in the selection (empty list is all)
if (
(not message.bot or all_messages)
and len(raw_members) == 0
or message.author in raw_members
) and (message.content or message.attachment):
impacted = True
history.messages += [message]
return impacted
+19
View File
@@ -0,0 +1,19 @@
from typing import List
# Custom libs
from .history_scanner import HistoryScanner
class LastScanner(HistoryScanner):
@staticmethod
def help() -> str:
return super(LastScanner, LastScanner).help(
cmd="last", text="Read last message"
)
def __init__(self):
super().__init__(help=LastScanner.help())
def get_results(self, intro: str) -> List[str]:
return self.history.to_string(type="last")
+19
View File
@@ -0,0 +1,19 @@
from typing import List
# Custom libs
from .history_scanner import HistoryScanner
class RandomScanner(HistoryScanner):
@staticmethod
def help() -> str:
return super(RandomScanner, RandomScanner).help(
cmd="rand", text="Read a random message"
)
def __init__(self):
super().__init__(help=RandomScanner.help())
def get_results(self, intro: str) -> List[str]:
return self.history.to_string(type="random")
+2 -2
View File
@@ -5,7 +5,7 @@ import logging
import re
import discord
from utils import no_duplicate, get_intro, delta, deltas, mention, channel_mention
from utils import no_duplicate, get_intro, delta
from logs import GuildLogs, ChannelLogs, MessageLog, ALREADY_RUNNING, CANCELLED
@@ -173,5 +173,5 @@ class Scanner(ABC):
pass
@abstractmethod
def get_results(self, intro: str):
def get_results(self, intro: str) -> List[str]:
pass