start en stop dates

This commit is contained in:
Klemek
2021-04-09 17:39:42 +02:00
parent b7a6f3313b
commit 2062f08721
6 changed files with 156 additions and 37 deletions
+63 -11
View File
@@ -5,7 +5,16 @@ import logging
import re
import discord
from utils import no_duplicate, get_intro, delta, gdpr
from utils import (
no_duplicate,
get_intro,
delta,
gdpr,
ISO8601_REGEX,
parse_time,
RELATIVE_TIME,
)
from logs import (
GuildLogs,
ChannelLogs,
@@ -54,22 +63,42 @@ class Scanner(ABC):
str(channel.id) for channel in message.channel_mentions
]
str_mentions = [str(member.id) for member in message.mentions]
dates = []
for i, arg in enumerate(args[1:]):
skip_check = False
if re.match(r"^<@!?\d+>$", arg):
arg = arg[3:-1] if "!" in arg else arg[2:-1]
elif re.match(r"^<#!?\d+>$", arg):
arg = arg[3:-1] if "!" in arg else arg[2:-1]
elif re.match(ISO8601_REGEX, arg) or arg in RELATIVE_TIME:
dates += [parse_time(arg)]
skip_check = True
if len(dates) > 2:
await message.channel.send(
f"Too many date arguments: `{arg}`", reference=message
)
return
if (
arg not in self.valid_args + ["me", "here", "fast", "fresh"]
and (not arg.isdigit() or not self.has_digit_args)
and arg not in str_channel_mentions
and arg not in str_mentions
and not skip_check
):
await message.channel.send(
f"Unrecognized argument: `{arg}`", reference=message
)
return
self.start_datetime = None if len(dates) < 1 else min(dates)
self.stop_datetime = datetime.now() if len(dates) < 2 else max(dates)
if self.start_datetime is not None and self.start_datetime > datetime.now():
await message.channel.send(
f"Start date is after today", reference=message
)
return
# Get selected channels or all of them if no channel arguments
self.channels = no_duplicate(message.channel_mentions)
@@ -103,6 +132,18 @@ class Scanner(ABC):
total_msg, total_chan = await logs.load(
progress, self.channels, fast="fast" in args, fresh="fresh" in args
)
if self.start_datetime is not None:
self.start_datetime = max(
self.start_datetime,
min(
[
logs.channels[channel.id].start_date
for channel in self.channels
if channel.id in logs.channels
and logs.channels[channel.id].start_date is not None
]
),
)
if total_msg == CANCELLED:
await message.channel.send(
"Operation cancelled by user",
@@ -127,13 +168,21 @@ class Scanner(ABC):
[
self.compute_message(channel_logs, message_log)
for message_log in channel_logs.messages
if (
self.start_datetime is None
or message_log.created_at >= self.start_datetime
)
and (
self.stop_datetime is None
or message_log.created_at <= self.stop_datetime
)
]
)
self.total_msg += len(channel_logs.messages)
self.msg_count += count
self.chan_count += 1 if count > 0 else 0
logging.info(f"scan {guild.id} > scanned in {delta(t0):,}ms")
if self.total_msg == 0:
if self.msg_count == 0:
await message.channel.send(
"There are no messages found matching the filters",
reference=message,
@@ -150,21 +199,24 @@ class Scanner(ABC):
self.members,
self.msg_count,
self.chan_count,
self.start_datetime,
self.stop_datetime,
)
)
logging.info(f"scan {guild.id} > results in {delta(t0):,}ms")
response = ""
first = True
for r in results:
if len(response + "\n" + r) > 2000:
await message.channel.send(
response,
reference=message if first else None,
allowed_mentions=discord.AllowedMentions.none(),
)
first = False
response = ""
response += "\n" + r
if r:
if len(response + "\n" + r) > 2000:
await message.channel.send(
response,
reference=message if first else None,
allowed_mentions=discord.AllowedMentions.none(),
)
first = False
response = ""
response += "\n" + r
if len(response) > 0:
await message.channel.send(
response,