reworked presence to get rid of useless emote analysis

This commit is contained in:
klemek
2021-01-11 18:11:43 +01:00
parent 6a6e15b054
commit efb6dddce4
4 changed files with 41 additions and 67 deletions
+13 -13
View File
@@ -2,15 +2,13 @@ from typing import List
from collections import defaultdict
from utils import mention, channel_mention, plural, percent
from utils import mention, channel_mention, plural, percent, top_key
class Presence:
def __init__(self):
self.most_used_reaction = ""
self.most_used_reaction_count = 0
self.used_reaction_total = 1
self.used_reaction_all_total = 1
self.reactions = defaultdict(int)
self.used_reaction_total = 0
self.channel_usage = defaultdict(int)
self.channel_total = defaultdict(int)
self.mentions = defaultdict(int)
@@ -31,7 +29,7 @@ class Presence:
f"- **messages**: {msg_count} ({percent(msg_count/total_msg)} of server's)"
]
if show_top_channel:
top_channel = sorted(self.channel_usage)[-1]
top_channel = top_key(self.channel_usage)
channel_sum = sum(self.channel_usage.values())
found_in = sorted(
self.channel_usage,
@@ -43,7 +41,7 @@ class Presence:
]
if member_specific:
if len(self.mentions) > 0:
top_mention = sorted(self.mentions)[-1]
top_mention = top_key(self.mentions)
mention_sum = sum(self.mentions.values())
ret += [
f"- **was mentioned**: {plural(mention_sum, 'time')} ({percent(mention_sum/self.mention_count)} of server's)",
@@ -52,7 +50,7 @@ class Presence:
else:
ret += ["- **was mentioned**: 0 times"]
if len(self.mention_others) > 0:
top_mention = sorted(self.mention_others)[-1]
top_mention = top_key(self.mention_others)
mention_sum = sum(self.mention_others.values())
ret += [
f"- **mentioned others**: {plural(mention_sum, 'time')} ({percent(mention_sum/self.mention_count)} of server's)",
@@ -60,16 +58,18 @@ class Presence:
]
else:
ret += ["- **was mentioned**: 0 times"]
if self.used_reaction_total > 0:
# ({self.used_reaction_total/self.used_reaction_all_total})
if len(self.reactions) > 0:
total_used = sum(self.reactions.values())
top_reaction = top_key(self.reactions)
ret += [
f"- **reactions**: {plural(self.used_reaction_total, 'time')}",
f"- **most used reaction**: {self.most_used_reaction} ({plural(self.most_used_reaction_count, 'time')}, {percent(self.most_used_reaction_count/self.used_reaction_total)})",
f"- **reactions**: {plural(total_used, 'time')}",
f"- **most used reaction**: {top_reaction} ({plural(self.reactions[top_reaction], 'time')}, {percent(self.reactions[top_reaction]/total_used)})",
]
if member_specific:
ret[
-2
] += f" ({percent(self.used_reaction_total/self.used_reaction_all_total)} of server's)"
] += f" ({percent(total_used/self.used_reaction_total)} of server's)"
else:
ret += ["- **reactions**: 0 times"]
return ret
+3 -16
View File
@@ -33,41 +33,28 @@ class FullScanner(Scanner):
async def init(self, message: discord.Message, *args: str) -> bool:
guild = message.channel.guild
self.freq = Frequency()
self.comp = Composition()
self.compo = Composition()
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)
self.total_msg = 0
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):
self.total_msg += 1
FrequencyScanner.analyse_message(message, self.freq, self.raw_members)
CompositionScanner.analyse_message(message, self.comp, self.raw_members)
CompositionScanner.analyse_message(message, self.compo, self.raw_members)
PresenceScanner.analyse_message(channel, message, self.pres, self.raw_members)
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
)
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, self.emotes_all)
res = [intro]
res += ["__Frequency__:"]
res += self.freq.to_string()
res += ["__Composition__:"]
res += self.comp.to_string(self.msg_count)
res += self.compo.to_string(self.msg_count)
res += ["__Presence__:"]
res += self.pres.to_string(
self.msg_count,
+17 -37
View File
@@ -1,12 +1,11 @@
from typing import List, Dict
from typing import List
import discord
# Custom libs
from .scanner import Scanner
from . import EmotesScanner
from data_types import Presence, Emote, get_emote_dict
from data_types import Presence
from logs import ChannelLogs, MessageLog
from utils import COMMON_HELP_ARGS
@@ -33,27 +32,15 @@ class PresenceScanner(Scanner):
self.pres = Presence()
self.total_msg = 0
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):
self.total_msg += 1
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, self.emotes_all)
res = [intro]
res += self.pres.to_string(
self.msg_count,
@@ -75,29 +62,22 @@ 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[message.author] += 1
pres.mention_count += 1
if len(raw_members) > 0:
for mention in message.mentions:
if mention in raw_members:
pres.mentions[message.author] += 1
pres.mention_count += 1
for reaction in message.reactions:
pres.used_reaction_total += len(message.reactions[reaction])
for member_id in message.reactions[reaction]:
if member_id in raw_members:
pres.reactions[reaction] += 1
else:
pres.mention_count += len(message.mentions)
for reaction in message.reactions:
pres.used_reaction_total += len(message.reactions[reaction])
pres.reactions[reaction] += len(message.reactions[reaction])
return impacted
@staticmethod
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)[
-1
]
pres.most_used_reaction_count = emotes[pres.most_used_reaction].reactions
+8 -1
View File
@@ -1,4 +1,4 @@
from typing import List
from typing import List, Dict, Union
import os
import logging
import discord
@@ -75,6 +75,13 @@ def no_duplicate(seq: list) -> list:
return list(dict.fromkeys(seq))
# DICTS
def top_key(d: Dict[Union[str, int], int]) -> Union[str, int]:
return sorted(d, key=lambda k: d[k])[-1]
# MESSAGE FORMATTING