composition scanner working yay

This commit is contained in:
klemek
2021-01-11 19:23:26 +01:00
parent e7daaed35b
commit d48031e4b5
4 changed files with 146 additions and 45 deletions
+66 -17
View File
@@ -1,24 +1,73 @@
from typing import List
from collections import defaultdict
from utils import percent, top_key, plural, precise
class Composition:
def __init__(self):
pass # TODO
self.total_characters = 0
self.plain_text = 0
self.emote_msg = 0
self.emote_only = 0
self.emotes = defaultdict(int)
self.edited = 0
self.everyone = 0
self.answers = 0
self.images = 0
self.tts = 0
self.mentions = 0
self.mention_msg = 0
self.links = 0
self.link_msg = 0
self.spoilers = 0
def to_string(self, msg_count: int) -> List[str]:
return [
# f"- **avg characters / message**: n",
# f"- **plain text messages**: n - %",
# f"- **emojis in messages**: n - %",
# f"- **spoilers in messages**: n - %",
# f"- **emoji-only messages**: n - %",
# f"- **most used emoji**: xx (see more with %emotes)",
# f"- **answers**: n - %",
# f"- **links**: n - %",
# f"- **images**: n - %",
# f"- **other media**: n - %",
# f"- **mentions**: n - %",
# f"- **most mentioned member**: @x",
# f"- **everyone/here**: n - %",
# f"- **edited messages**: n - %",
] # TODO
ret = []
ret += [
f"- **avg. characters / message**: {self.total_characters/msg_count:.2f}"
]
if self.plain_text > 0:
ret += [
f"- **plain text messages**: {self.plain_text:,} ({percent(self.plain_text/msg_count)})"
]
if self.edited > 0:
ret += [
f"- **edited messages**: {self.edited} ({percent(self.edited/msg_count)})"
]
if self.everyone > 0:
ret += [
f"- **everyone**: {self.everyone} ({percent(self.everyone/msg_count)})"
]
if self.mentions > 0:
ret += [
f"- **mentions**: {self.mentions:,} (in {percent(self.mention_msg/msg_count)} of msg, avg. {precise(self.mentions/msg_count)}/msg)",
]
if self.answers > 0:
ret += [
f"- **answers**: {self.answers} ({percent(self.answers/msg_count)})"
]
total_emotes = sum(self.emotes.values())
if total_emotes > 0:
top_emote = top_key(self.emotes)
ret += [
f"- **emojis**: {total_emotes:,} (in {percent(self.emote_msg/msg_count)} of msg, avg. {precise(total_emotes/msg_count)}/msg)",
f"- **most used emoji**: {top_emote} ({plural(self.emotes[top_emote], 'time')}, {percent(self.emotes[top_emote]/total_emotes)})",
]
if self.emote_only > 0:
ret += [
f"- **emoji-only messages**: {self.emote_only:,} ({percent(self.emote_only/msg_count)})"
]
if self.images > 0:
ret += [f"- **images**: {self.images} ({percent(self.images/msg_count)})"]
if self.links > 0:
ret += [
f"- **links**: {self.links} (in {percent(self.link_msg/msg_count)} of msg, avg. {precise(self.links/msg_count)}/msg)"
]
if self.spoilers > 0:
ret += [
f"- **spoilers**: {self.spoilers} ({percent(self.spoilers/msg_count)})"
]
if self.tts > 0:
ret += [f"- **tts messages**: {self.tts} ({percent(self.tts/msg_count)})"]
return ret
+4
View File
@@ -27,6 +27,8 @@ class MessageLog:
self.role_mentions = message.raw_role_mentions
self.channel_mentions = message.raw_channel_mentions
self.image = False
self.attachment = len(message.attachments) > 0
self.embed = len(message.embeds) > 0
for attachment in message.attachments:
if is_extension(attachment.filename, IMAGE_FORMAT):
self.image = True
@@ -58,6 +60,8 @@ class MessageLog:
self.role_mentions = [int(m) for m in message["role_mentions"]]
self.channel_mentions = [int(m) for m in message["channel_mentions"]]
self.image = message["image"]
self.embed = message["embed"]
self.attachment = message["attachment"]
self.reactions = message["reactions"]
async def load(self, message: discord.Message):
+68 -22
View File
@@ -1,15 +1,14 @@
from typing import List, Dict
from collections import defaultdict
from typing import List
import re
import discord
# Custom libs
from .scanner import Scanner
from . import EmotesScanner
from data_types import Composition, Emote, get_emote_dict
from data_types import Composition
from logs import ChannelLogs, MessageLog
from utils import COMMON_HELP_ARGS
from utils import emojis, COMMON_HELP_ARGS
class CompositionScanner(Scanner):
@@ -31,36 +30,83 @@ class CompositionScanner(Scanner):
)
async def init(self, message: discord.Message, *args: str) -> bool:
guild = message.channel.guild
self.comp = Composition()
# Create emotes dict from custom emojis of the guild
self.emotes = get_emote_dict(message.channel.guild)
self.compo = Composition()
return True
def compute_message(self, channel: ChannelLogs, message: MessageLog):
ret = CompositionScanner.analyse_message(message, self.comp, self.raw_members)
ret &= EmotesScanner.analyse_message(
message, self.emotes, self.raw_members, all_emojis=True
)
ret = CompositionScanner.analyse_message(message, self.compo, self.raw_members)
return ret
def get_results(self, intro: str) -> List[str]:
CompositionScanner.compute_results(self.comp, self.emotes)
res = [intro]
res += self.comp.to_string()
res += self.compo.to_string(self.msg_count)
return res
@staticmethod
def analyse_message(
message: MessageLog, comp: Composition, raw_members: List[int]
message: MessageLog, compo: Composition, raw_members: List[int]
) -> bool:
impacted = False
# If author is included in the selection (empty list is all)
if len(raw_members) == 0 or message.author in raw_members:
if not message.bot and len(raw_members) == 0 or message.author in raw_members:
impacted = True
pass # TODO
return impacted
compo.total_characters += len(message.content)
@staticmethod
def compute_results(comp: Composition, emotes: Dict[str, Emote]):
pass # TODO
emotes_found = emojis.regex.findall(message.content)
without_emote = message.content
for name in emotes_found:
if name not in compo.emotes:
if name not in emojis.unicode_list:
continue
compo.emotes[name] += 1
i = without_emote.index(name)
without_emote = without_emote[:i] + without_emote[i + len(name) :]
if len(message.content.strip()) > 0 and len(without_emote.strip()) == 0:
compo.emote_only += 1
if len(emotes_found) > 0:
compo.emote_msg += 1
links_found = re.findall(r"https?:\/\/", message.content)
compo.links += len(links_found)
if len(links_found) > 0:
compo.link_msg += 1
mentions = (
len(message.mentions)
+ len(message.role_mentions)
+ len(message.channel_mentions)
)
if message.mention_everyone:
compo.everyone += 1
mentions += 1
if mentions > 0:
compo.mentions += mentions
compo.mention_msg += 1
spoilers_found = re.findall(r"\|\|[^|]+\|\|", message.content)
if len(spoilers_found) > 0:
compo.spoilers += 1
if message.edited_at is not None:
compo.edited += 1
if message.reference is not None:
compo.answers += 1
if message.image:
compo.images += 1
if message.tts:
compo.tts += 1
if (
len(emotes_found) == 0
and message.reference is None
and not message.image
and len(message.mentions) == 0
and len(message.role_mentions) == 0
and len(message.channel_mentions) == 0
and not message.tts
and not message.mention_everyone
and not message.embed
and not message.attachment
):
compo.plain_text += 1
return impacted
+8 -6
View File
@@ -2,6 +2,7 @@ from typing import List, Dict, Union
import os
import logging
import discord
import math
from datetime import datetime
# OTHER
@@ -104,12 +105,13 @@ def plural(count: int, word: str) -> str:
def percent(p: float) -> str:
if p < 0.01:
return f"{100*p:.2f}%"
elif p < 0.1:
return f"{100*p:.1f}%"
else:
return f"{100*p:.0f}%"
return f"{precise(100*p)}%"
def precise(p: float) -> str:
precision = abs(min(0, math.ceil(math.log10(p)) - 2))
s = "{:." + str(precision) + "f}"
return s.format(p)
# DATE FORMATTING