composition scanner working yay
This commit is contained in:
@@ -1,24 +1,73 @@
|
|||||||
from typing import List
|
from typing import List
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
from utils import percent, top_key, plural, precise
|
||||||
|
|
||||||
|
|
||||||
class Composition:
|
class Composition:
|
||||||
def __init__(self):
|
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]:
|
def to_string(self, msg_count: int) -> List[str]:
|
||||||
return [
|
ret = []
|
||||||
# f"- **avg characters / message**: n",
|
ret += [
|
||||||
# f"- **plain text messages**: n - %",
|
f"- **avg. characters / message**: {self.total_characters/msg_count:.2f}"
|
||||||
# f"- **emojis in messages**: n - %",
|
]
|
||||||
# f"- **spoilers in messages**: n - %",
|
if self.plain_text > 0:
|
||||||
# f"- **emoji-only messages**: n - %",
|
ret += [
|
||||||
# f"- **most used emoji**: xx (see more with %emotes)",
|
f"- **plain text messages**: {self.plain_text:,} ({percent(self.plain_text/msg_count)})"
|
||||||
# f"- **answers**: n - %",
|
]
|
||||||
# f"- **links**: n - %",
|
if self.edited > 0:
|
||||||
# f"- **images**: n - %",
|
ret += [
|
||||||
# f"- **other media**: n - %",
|
f"- **edited messages**: {self.edited} ({percent(self.edited/msg_count)})"
|
||||||
# f"- **mentions**: n - %",
|
]
|
||||||
# f"- **most mentioned member**: @x",
|
if self.everyone > 0:
|
||||||
# f"- **everyone/here**: n - %",
|
ret += [
|
||||||
# f"- **edited messages**: n - %",
|
f"- **everyone**: {self.everyone} ({percent(self.everyone/msg_count)})"
|
||||||
] # TODO
|
]
|
||||||
|
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
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ class MessageLog:
|
|||||||
self.role_mentions = message.raw_role_mentions
|
self.role_mentions = message.raw_role_mentions
|
||||||
self.channel_mentions = message.raw_channel_mentions
|
self.channel_mentions = message.raw_channel_mentions
|
||||||
self.image = False
|
self.image = False
|
||||||
|
self.attachment = len(message.attachments) > 0
|
||||||
|
self.embed = len(message.embeds) > 0
|
||||||
for attachment in message.attachments:
|
for attachment in message.attachments:
|
||||||
if is_extension(attachment.filename, IMAGE_FORMAT):
|
if is_extension(attachment.filename, IMAGE_FORMAT):
|
||||||
self.image = True
|
self.image = True
|
||||||
@@ -58,6 +60,8 @@ class MessageLog:
|
|||||||
self.role_mentions = [int(m) for m in message["role_mentions"]]
|
self.role_mentions = [int(m) for m in message["role_mentions"]]
|
||||||
self.channel_mentions = [int(m) for m in message["channel_mentions"]]
|
self.channel_mentions = [int(m) for m in message["channel_mentions"]]
|
||||||
self.image = message["image"]
|
self.image = message["image"]
|
||||||
|
self.embed = message["embed"]
|
||||||
|
self.attachment = message["attachment"]
|
||||||
self.reactions = message["reactions"]
|
self.reactions = message["reactions"]
|
||||||
|
|
||||||
async def load(self, message: discord.Message):
|
async def load(self, message: discord.Message):
|
||||||
|
|||||||
@@ -1,15 +1,14 @@
|
|||||||
from typing import List, Dict
|
from typing import List
|
||||||
from collections import defaultdict
|
import re
|
||||||
import discord
|
import discord
|
||||||
|
|
||||||
|
|
||||||
# Custom libs
|
# Custom libs
|
||||||
|
|
||||||
from .scanner import Scanner
|
from .scanner import Scanner
|
||||||
from . import EmotesScanner
|
from data_types import Composition
|
||||||
from data_types import Composition, Emote, get_emote_dict
|
|
||||||
from logs import ChannelLogs, MessageLog
|
from logs import ChannelLogs, MessageLog
|
||||||
from utils import COMMON_HELP_ARGS
|
from utils import emojis, COMMON_HELP_ARGS
|
||||||
|
|
||||||
|
|
||||||
class CompositionScanner(Scanner):
|
class CompositionScanner(Scanner):
|
||||||
@@ -31,36 +30,83 @@ class CompositionScanner(Scanner):
|
|||||||
)
|
)
|
||||||
|
|
||||||
async def init(self, message: discord.Message, *args: str) -> bool:
|
async def init(self, message: discord.Message, *args: str) -> bool:
|
||||||
guild = message.channel.guild
|
self.compo = Composition()
|
||||||
self.comp = Composition()
|
|
||||||
# Create emotes dict from custom emojis of the guild
|
|
||||||
self.emotes = get_emote_dict(message.channel.guild)
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def compute_message(self, channel: ChannelLogs, message: MessageLog):
|
def compute_message(self, channel: ChannelLogs, message: MessageLog):
|
||||||
ret = CompositionScanner.analyse_message(message, self.comp, self.raw_members)
|
ret = CompositionScanner.analyse_message(message, self.compo, self.raw_members)
|
||||||
ret &= EmotesScanner.analyse_message(
|
|
||||||
message, self.emotes, self.raw_members, all_emojis=True
|
|
||||||
)
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def get_results(self, intro: str) -> List[str]:
|
def get_results(self, intro: str) -> List[str]:
|
||||||
CompositionScanner.compute_results(self.comp, self.emotes)
|
|
||||||
res = [intro]
|
res = [intro]
|
||||||
res += self.comp.to_string()
|
res += self.compo.to_string(self.msg_count)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def analyse_message(
|
def analyse_message(
|
||||||
message: MessageLog, comp: Composition, raw_members: List[int]
|
message: MessageLog, compo: Composition, raw_members: List[int]
|
||||||
) -> bool:
|
) -> bool:
|
||||||
impacted = False
|
impacted = False
|
||||||
# If author is included in the selection (empty list is all)
|
# 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
|
impacted = True
|
||||||
pass # TODO
|
compo.total_characters += len(message.content)
|
||||||
return impacted
|
|
||||||
|
|
||||||
@staticmethod
|
emotes_found = emojis.regex.findall(message.content)
|
||||||
def compute_results(comp: Composition, emotes: Dict[str, Emote]):
|
without_emote = message.content
|
||||||
pass # TODO
|
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
@@ -2,6 +2,7 @@ from typing import List, Dict, Union
|
|||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
import discord
|
import discord
|
||||||
|
import math
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
# OTHER
|
# OTHER
|
||||||
@@ -104,12 +105,13 @@ def plural(count: int, word: str) -> str:
|
|||||||
|
|
||||||
|
|
||||||
def percent(p: float) -> str:
|
def percent(p: float) -> str:
|
||||||
if p < 0.01:
|
return f"{precise(100*p)}%"
|
||||||
return f"{100*p:.2f}%"
|
|
||||||
elif p < 0.1:
|
|
||||||
return f"{100*p:.1f}%"
|
def precise(p: float) -> str:
|
||||||
else:
|
precision = abs(min(0, math.ceil(math.log10(p)) - 2))
|
||||||
return f"{100*p:.0f}%"
|
s = "{:." + str(precision) + "f}"
|
||||||
|
return s.format(p)
|
||||||
|
|
||||||
|
|
||||||
# DATE FORMATTING
|
# DATE FORMATTING
|
||||||
|
|||||||
Reference in New Issue
Block a user