From 03003f24b096decf1afd44a606ea0c32d7e2cf40 Mon Sep 17 00:00:00 2001 From: klemek Date: Thu, 7 Jan 2021 18:28:52 +0100 Subject: [PATCH] (WIP) refactor to extract scan constants --- bot.py | 2 +- emotes.py | 68 ++++++++++++++++++++++++++------------------------ log_manager.py | 2 +- scan.py | 0 utils.py | 2 +- 5 files changed, 39 insertions(+), 35 deletions(-) create mode 100644 scan.py diff --git a/bot.py b/bot.py index 9074a5b..26856cf 100755 --- a/bot.py +++ b/bot.py @@ -12,7 +12,7 @@ emojis.load_emojis() bot = Bot( "Discord Analyst", # name - "1.5", # version + "1.6", # version alias="%", # respond to '|command' messages ) bot.log_calls = True diff --git a/emotes.py b/emotes.py index e60495d..93940b8 100644 --- a/emotes.py +++ b/emotes.py @@ -7,7 +7,7 @@ import discord # Custom libs from utils import no_duplicate, mention, plural, day_interval, get_intro -from log_manager import GuildLogs, ChannelLogs +from log_manager import GuildLogs, MessageLog import emojis # CONSTANTS @@ -92,8 +92,13 @@ async def compute(client: discord.client, message: discord.Message, *args: str): msg_count = 0 chan_count = 0 for id in logs.channels: - count = analyse_channel( - logs.channels[id], emotes, raw_members, all_emojis=all_emojis + count = sum( + [ + analyse_message( + messagelog, emotes, raw_members, all_emojis=all_emojis + ) + for messagelog in logs.channels[id].messages + ] ) msg_count += count chan_count += 1 if count > 0 else 0 @@ -198,42 +203,41 @@ class Emote: # ANALYSIS -def analyse_channel( - channel: ChannelLogs, +def analyse_message( + message: MessageLog, emotes: Dict[str, Emote], raw_members: List[int], *, all_emojis: bool, -) -> int: - count = 0 - for message in channel.messages: - # If author is included in the selection (empty list is all) - if not message.bot and (len(raw_members) == 0 or message.author in raw_members): - count += 1 - # Find all emotes un the current message in the form "<:emoji:123456789>" - # Filter for known emotes - found = emojis.regex.findall(message.content) - # For each emote, update its usage - for name in found: - if name not in emotes: - if not all_emojis or name not in emojis.unicode_list: - continue - emotes[name].usages += 1 - emotes[name].update_use(message.created_at, [message.author]) - # For each reaction of this message, test if known emote and update when it's the case - for name in message.reactions: +) -> bool: + impacted = False + # If author is included in the selection (empty list is all) + if not message.bot and (len(raw_members) == 0 or message.author in raw_members): + impacted = True + # Find all emotes un the current message in the form "<:emoji:123456789>" + # Filter for known emotes + found = emojis.regex.findall(message.content) + # For each emote, update its usage + for name in found: if name not in emotes: if not all_emojis or name not in emojis.unicode_list: continue - if len(raw_members) == 0: - emotes[name].reactions += len(message.reactions[name]) - emotes[name].update_use(message.created_at, message.reactions[name]) - else: - for member in raw_members: - if member in message.reactions[name]: - emotes[name].reactions += 1 - emotes[name].update_use(message.created_at, [member]) - return count + emotes[name].usages += 1 + emotes[name].update_use(message.created_at, [message.author]) + # For each reaction of this message, test if known emote and update when it's the case + for name in message.reactions: + if name not in emotes: + if not all_emojis or name not in emojis.unicode_list: + continue + if len(raw_members) == 0: + emotes[name].reactions += len(message.reactions[name]) + emotes[name].update_use(message.created_at, message.reactions[name]) + else: + for member in raw_members: + if member in message.reactions[name]: + emotes[name].reactions += 1 + emotes[name].update_use(message.created_at, [member]) + return impacted # RESULTS diff --git a/log_manager.py b/log_manager.py index 93c1ba5..4a96aed 100644 --- a/log_manager.py +++ b/log_manager.py @@ -46,7 +46,7 @@ class MessageLog: self.role_mentions = message.raw_role_mentions self.channel_mentions = message.raw_channel_mentions self.image = False - for attachment in message.attachment: + for attachment in message.attachments: if is_extension(attachment.filename, IMAGE_FORMAT): self.image = True break diff --git a/scan.py b/scan.py new file mode 100644 index 0000000..e69de29 diff --git a/utils.py b/utils.py index b96e8eb..db2ac6d 100644 --- a/utils.py +++ b/utils.py @@ -59,7 +59,7 @@ def aggregate(names: List[str]) -> str: def plural(count: int, word: str) -> str: - return str(count) + " " + word + "s" if count == 1 else "" + return str(count) + " " + word + ("s" if count != 1 else "") def day_interval(interval: int) -> str: