From 155fe48e70d3f60cf9f751a538f4d6a4e2c9ff6e Mon Sep 17 00:00:00 2001 From: klemek Date: Mon, 11 Jan 2021 17:26:34 +0100 Subject: [PATCH] presence scanner working yay --- src/data_types/presence.py | 41 +++++++++++++++++++++++++++----- src/scanners/full_scanner.py | 12 +++++++--- src/scanners/presence_scanner.py | 27 +++++++++++++++++---- 3 files changed, 67 insertions(+), 13 deletions(-) diff --git a/src/data_types/presence.py b/src/data_types/presence.py index 1f269cb..52ba9e5 100644 --- a/src/data_types/presence.py +++ b/src/data_types/presence.py @@ -10,32 +10,61 @@ class Presence: self.most_used_reaction = "" self.most_used_reaction_count = 0 self.used_reaction_total = 1 + self.used_reaction_all_total = 1 self.channel_usage = defaultdict(int) + self.channel_total = defaultdict(int) self.mentions = defaultdict(int) + self.mention_others = defaultdict(int) + self.msg_count = 0 + self.total_msg = 0 + self.mention_count = 0 - def to_string(self, *, show_top_channel: bool, show_mentioned: bool) -> List[str]: + def to_string(self, *, show_top_channel: bool, member_specific: bool) -> List[str]: ret = [] + if member_specific: + ret += [ + f"- **messages**: {self.msg_count} ({100*self.msg_count/self.total_msg:.0f}% of server's)" + ] if show_top_channel: top_channel = sorted(self.channel_usage)[-1] channel_sum = sum(self.channel_usage.values()) + found_in = sorted( + self.channel_usage, + key=lambda k: self.channel_usage[k] / self.channel_total[k], + )[-1] ret += [ - f"- **most visited channel**: {channel_mention(top_channel)} ({self.channel_usage[top_channel]:,} msg, {100*self.channel_usage[top_channel]//channel_sum:.0f}%)" + f"- **most visited channel**: {channel_mention(top_channel)} ({self.channel_usage[top_channel]:,} msg, {100*self.channel_usage[top_channel]//channel_sum:.0f}%)", + f"- **mostly found in**: {channel_mention(found_in)} ({self.channel_usage[found_in]:,} msg, {100*self.channel_usage[found_in]//self.channel_total[found_in]:.0f}% of channel's)", ] - if show_mentioned: + if member_specific: if len(self.mentions) > 0: top_mention = sorted(self.mentions)[-1] mention_sum = sum(self.mentions.values()) ret += [ - f"- **mentioned**: {plural(mention_sum, 'time')}", + f"- **was mentioned**: {plural(mention_sum, 'time')} ({100*mention_sum/self.mention_count:.0f}% of server's)", f"- **mostly mentioned by**: {mention(top_mention)} ({plural(self.mentions[top_mention], 'time')}, {100*self.mentions[top_mention]//mention_sum:.0f}%)", ] else: - ret += [f"- **mentioned**: 0 times"] + ret += ["- **was mentioned**: 0 times"] + if len(self.mention_others) > 0: + top_mention = sorted(self.mention_others)[-1] + mention_sum = sum(self.mention_others.values()) + ret += [ + f"- **mentioned others**: {plural(mention_sum, 'time')} ({100*mention_sum/self.mention_count:.0f}% of server's)", + f"- **mostly mentioned**: {mention(top_mention)} ({plural(self.mention_others[top_mention], 'time')}, {100*self.mention_others[top_mention]//mention_sum:.0f}%)", + ] + else: + ret += ["- **was mentioned**: 0 times"] if self.used_reaction_total > 0: + # ({self.used_reaction_total/self.used_reaction_all_total}) ret += [ f"- **reactions**: {plural(self.used_reaction_total, 'time')}", f"- **most used reaction**: {self.most_used_reaction} ({plural(self.most_used_reaction_count, 'time')}, {100*self.most_used_reaction_count/self.used_reaction_total:.0f}%)", ] + if member_specific: + ret[ + -2 + ] += f" ({100*self.used_reaction_total/self.used_reaction_all_total:.0f}% of server's)" else: - ret += [f"- **reactions**: 0 times"] + ret += ["- **reactions**: 0 times"] return ret diff --git a/src/scanners/full_scanner.py b/src/scanners/full_scanner.py index b87aae6..9a8ad12 100644 --- a/src/scanners/full_scanner.py +++ b/src/scanners/full_scanner.py @@ -35,9 +35,13 @@ class FullScanner(Scanner): self.freq = Frequency() self.comp = Composition() self.pres = Presence() - # Create emotes dict from custom emojis of the guild + self.member_specific = len(self.members) > 0 # Create emotes dict from custom emojis of the guild self.emotes = get_emote_dict(message.channel.guild) + if self.member_specific: + self.emotes_all = get_emote_dict(message.channel.guild) + else: + self.emotes_all = {} return True def compute_message(self, channel: ChannelLogs, message: MessageLog): @@ -47,6 +51,8 @@ class FullScanner(Scanner): EmotesScanner.analyse_message( message, self.emotes, self.raw_members, all_emojis=True ) + if self.member_specific: + EmotesScanner.analyse_message(message, self.emotes_all, [], all_emojis=True) return not message.bot and ( len(self.raw_members) == 0 or message.author in self.raw_members ) @@ -54,7 +60,7 @@ class FullScanner(Scanner): def get_results(self, intro: str) -> List[str]: FrequencyScanner.compute_results(self.freq) CompositionScanner.compute_results(self.comp, self.emotes) - PresenceScanner.compute_results(self.pres, self.emotes) + PresenceScanner.compute_results(self.pres, self.emotes, self.emotes_all) res = [intro] res += ["__Frequency__:"] res += self.freq.to_string() @@ -63,6 +69,6 @@ class FullScanner(Scanner): res += ["__Presence__:"] res += self.pres.to_string( show_top_channel=len(self.channels) > 1, - show_mentioned=(len(self.members) > 0), + member_specific=self.member_specific, ) return res diff --git a/src/scanners/presence_scanner.py b/src/scanners/presence_scanner.py index 9034d75..4c62b29 100644 --- a/src/scanners/presence_scanner.py +++ b/src/scanners/presence_scanner.py @@ -31,24 +31,31 @@ class PresenceScanner(Scanner): async def init(self, message: discord.Message, *args: str) -> bool: self.pres = Presence() + self.member_specific = len(self.members) > 0 # Create emotes dict from custom emojis of the guild self.emotes = get_emote_dict(message.channel.guild) + if self.member_specific: + self.emotes_all = get_emote_dict(message.channel.guild) + else: + self.emotes_all = {} return True def compute_message(self, channel: ChannelLogs, message: MessageLog): EmotesScanner.analyse_message( message, self.emotes, self.raw_members, all_emojis=True ) + if self.member_specific: + EmotesScanner.analyse_message(message, self.emotes_all, [], all_emojis=True) return PresenceScanner.analyse_message( channel, message, self.pres, self.raw_members ) def get_results(self, intro: str) -> List[str]: - PresenceScanner.compute_results(self.pres, self.emotes) + PresenceScanner.compute_results(self.pres, self.emotes, self.emotes_all) res = [intro] res += self.pres.to_string( show_top_channel=(len(self.channels) > 1), - show_mentioned=(len(self.members) > 0), + member_specific=self.member_specific, ) return res @@ -64,15 +71,27 @@ class PresenceScanner(Scanner): if not message.bot and (len(raw_members) == 0 or message.author in raw_members): impacted = True pres.channel_usage[channel.id] += 1 + pres.msg_count += 1 + for mention in message.mentions: + pres.mention_others[mention] += 1 + pres.channel_total[channel.id] += 1 for mention in message.mentions: if mention in raw_members: - pres.mentions[mention] += 1 + pres.mentions[message.author] += 1 + pres.mention_count += 1 + pres.total_msg += 1 return impacted @staticmethod - def compute_results(pres: Presence, emotes: Dict[str, Emote]): + def compute_results( + pres: Presence, emotes: Dict[str, Emote], emotes_all: Dict[str, Emote] + ): # calculate total reactions pres.used_reaction_total = sum([emote.reactions for emote in emotes.values()]) + if len(emotes_all) > 0: + pres.used_reaction_all_total = sum( + [emote.reactions for emote in emotes_all.values()] + ) if pres.used_reaction_total > 0: # calculate most used reaction pres.most_used_reaction = sorted(emotes, key=lambda k: emotes[k].reactions)[