scanner exception handling

This commit is contained in:
Klemek
2021-04-21 20:22:36 +02:00
parent 634f34fb54
commit 6afb05148d
2 changed files with 178 additions and 164 deletions
+1
View File
@@ -115,6 +115,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 * `%repeat`, `%mobile` to repeat commands
* bug fix
* **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
+177 -164
View File
@@ -57,198 +57,211 @@ class Scanner(ABC):
): ):
args = list(args) args = list(args)
guild = message.guild guild = message.guild
with GuildLogs(guild) as logs: progress = None
# If "%cmd help" redirect to "%help cmd" try:
if "help" in args: with GuildLogs(guild) as logs:
await client.bot.help(client, message, "help", args[0]) # If "%cmd help" redirect to "%help cmd"
return if "help" in args:
await client.bot.help(client, message, "help", args[0])
return
# check args validity # check args validity
str_channel_mentions = [ str_channel_mentions = [
str(channel.id) for channel in message.channel_mentions str(channel.id) for channel in message.channel_mentions
] ]
str_mentions = [str(member.id) for member in message.mentions] str_mentions = [str(member.id) for member in message.mentions]
dates = [] dates = []
for i, arg in enumerate(args[1:]): for i, arg in enumerate(args[1:]):
skip_check = False skip_check = False
if re.match(r"^<@!?\d+>$", arg): if re.match(r"^<@!?\d+>$", arg):
arg = arg[3:-1] if "!" in arg else arg[2:-1] arg = arg[3:-1] if "!" in arg else arg[2:-1]
elif re.match(r"^<#!?\d+>$", arg): elif re.match(r"^<#!?\d+>$", arg):
arg = arg[3:-1] if "!" in arg else arg[2:-1] arg = arg[3:-1] if "!" in arg else arg[2:-1]
elif re.match(ISO8601_REGEX, arg) or re.match(RELATIVE_REGEX, arg): elif re.match(ISO8601_REGEX, arg) or re.match(RELATIVE_REGEX, arg):
dates += [parse_time(arg)] dates += [parse_time(arg)]
skip_check = True skip_check = True
if len(dates) > 2: 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", "mobile", "mention"]
and (not arg.isdigit() or not self.has_digit_args)
and arg not in str_channel_mentions
and arg not in str_mentions
and arg not in other_mentions
and not skip_check
):
await message.channel.send( await message.channel.send(
f"Too many date arguments: `{arg}`", reference=message f"Unrecognized argument: `{arg}`", reference=message
) )
return return
if (
arg self.start_date = None if len(dates) < 1 else min(dates)
not in self.valid_args self.stop_date = None if len(dates) < 2 else max(dates)
+ ["me", "here", "fast", "fresh", "mobile", "mention"]
and (not arg.isdigit() or not self.has_digit_args) if self.start_date is not None and self.start_date > datetime.now():
and arg not in str_channel_mentions
and arg not in str_mentions
and arg not in other_mentions
and not skip_check
):
await message.channel.send( await message.channel.send(
f"Unrecognized argument: `{arg}`", reference=message f"Start date is after today", reference=message
) )
return return
self.start_date = None if len(dates) < 1 else min(dates) # Get selected channels or all of them if no channel arguments
self.stop_date = None if len(dates) < 2 else max(dates) self.channels = no_duplicate(message.channel_mentions)
if self.start_date is not None and self.start_date > datetime.now(): # transform the "here" arg
await message.channel.send( if "here" in args:
f"Start date is after today", reference=message self.channels += [message.channel]
)
return
# Get selected channels or all of them if no channel arguments self.full = len(self.channels) == 0
self.channels = no_duplicate(message.channel_mentions) if self.full:
self.channels = guild.text_channels
# transform the "here" arg # Get selected members
if "here" in args: self.members = no_duplicate(message.mentions)
self.channels += [message.channel] self.raw_members = no_duplicate(message.raw_mentions)
self.full = len(self.channels) == 0 # transform the "me" arg
if self.full: if "me" in args:
self.channels = guild.text_channels self.members += [message.author]
self.raw_members += [message.author.id]
# Get selected members self.mention_users = "mention" in args or "mobile" in args
self.members = no_duplicate(message.mentions)
self.raw_members = no_duplicate(message.raw_mentions)
# transform the "me" arg if not await self.init(message, *args):
if "me" in args: return
self.members += [message.author]
self.raw_members += [message.author.id]
self.mention_users = "mention" in args or "mobile" in args # Start computing data
async with message.channel.typing():
if not await self.init(message, *args): progress = await message.channel.send(
return "```Starting analysis...```",
# Start computing data
async with message.channel.typing():
progress = await message.channel.send(
"```Starting analysis...```",
reference=message,
allowed_mentions=discord.AllowedMentions.none(),
)
total_msg, total_chan = await logs.load(
progress,
self.channels,
self.start_date,
self.stop_date,
fast="fast" in args,
fresh="fresh" in args,
)
if total_msg == CANCELLED:
await message.channel.send(
"Operation cancelled by user",
reference=message, reference=message,
allowed_mentions=discord.AllowedMentions.none(),
) )
elif total_msg == ALREADY_RUNNING: total_msg, total_chan = await logs.load(
await message.channel.send( progress,
"An analysis is already running on this server, please be patient.", self.channels,
reference=message, self.start_date,
self.stop_date,
fast="fast" in args,
fresh="fresh" in args,
) )
elif total_msg == NO_FILE: if total_msg == CANCELLED:
await message.channel.send(gdpr.TEXT)
else:
if self.start_date is not None and len(logs.channels) > 0:
self.start_date = max(
self.start_date,
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 self.stop_date is None:
self.stop_date = datetime.utcnow()
self.msg_count = 0
self.total_msg = 0
self.chan_count = 0
t0 = datetime.now()
for channel in self.channels:
if channel.id in logs.channels:
channel_logs = logs.channels[channel.id]
count = sum(
[
self.compute_message(channel_logs, message_log)
for message_log in channel_logs.messages
if (
self.start_date is None
or message_log.created_at >= self.start_date
)
and (
self.stop_date is None
or message_log.created_at <= self.stop_date
)
]
)
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.msg_count == 0:
await message.channel.send( await message.channel.send(
"There are no messages found matching the filters", "Operation cancelled by user",
reference=message, reference=message,
) )
elif total_msg == ALREADY_RUNNING:
await message.channel.send(
"An analysis is already running on this server, please be patient.",
reference=message,
)
elif total_msg == NO_FILE:
await message.channel.send(gdpr.TEXT)
else: else:
await progress.edit(content="```Computing results...```") if self.start_date is not None and len(logs.channels) > 0:
# Display results self.start_date = max(
t0 = datetime.now()
results = self.get_results(
get_intro(
self.intro_context,
self.full,
self.channels,
self.members,
self.msg_count,
self.chan_count,
self.start_date, self.start_date,
self.stop_date, 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 self.stop_date is None:
logging.info(f"scan {guild.id} > results in {delta(t0):,}ms") self.stop_date = datetime.utcnow()
response = ""
first = True self.msg_count = 0
allowed_mentions = ( self.total_msg = 0
discord.AllowedMentions.all() self.chan_count = 0
if self.mention_users t0 = datetime.now()
else discord.AllowedMentions.none() for channel in self.channels:
) if channel.id in logs.channels:
for r in results: channel_logs = logs.channels[channel.id]
if r: count = sum(
if len(response + "\n" + r) > 2000: [
await message.channel.send( self.compute_message(channel_logs, message_log)
response, for message_log in channel_logs.messages
reference=message if first else None, if (
allowed_mentions=allowed_mentions, self.start_date is None
) or message_log.created_at >= self.start_date
first = False )
response = "" and (
response += "\n" + r self.stop_date is None
if len(response) > 0: or message_log.created_at <= self.stop_date
)
]
)
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.msg_count == 0:
await message.channel.send( await message.channel.send(
response, "There are no messages found matching the filters",
reference=message if first else None, reference=message,
allowed_mentions=allowed_mentions,
) )
command_cache.cache(self, message, args) else:
await progress.edit(content="```Computing results...```")
# Display results
t0 = datetime.now()
results = self.get_results(
get_intro(
self.intro_context,
self.full,
self.channels,
self.members,
self.msg_count,
self.chan_count,
self.start_date,
self.stop_date,
)
)
logging.info(
f"scan {guild.id} > results in {delta(t0):,}ms"
)
response = ""
first = True
allowed_mentions = (
discord.AllowedMentions.all()
if self.mention_users
else discord.AllowedMentions.none()
)
for r in results:
if r:
if len(response + "\n" + r) > 2000:
await message.channel.send(
response,
reference=message if first else None,
allowed_mentions=allowed_mentions,
)
first = False
response = ""
response += "\n" + r
if len(response) > 0:
await message.channel.send(
response,
reference=message if first else None,
allowed_mentions=allowed_mentions,
)
command_cache.cache(self, message, args)
# Delete custom progress message # Delete custom progress message
await progress.delete() await progress.delete()
except Exception as error:
logging.exception(error)
await message.channel.send(
"An unexpected error happened while computing your command, we're sorry for the inconvenience.",
reference=message,
)
if progress is not None:
await progress.delete()
@abstractmethod @abstractmethod
async def init(self, message: discord.Message, *args: str) -> bool: async def init(self, message: discord.Message, *args: str) -> bool: