From f8e294f64728572d9b1e95889fd266cd6bdaa6ce Mon Sep 17 00:00:00 2001 From: Klemek Date: Thu, 22 Apr 2021 13:09:07 +0200 Subject: [PATCH] emotes => emojis --- README.md | 6 +- src/data_types/__init__.py | 2 +- src/data_types/composition.py | 22 +++---- src/data_types/counter.py | 2 +- src/data_types/{emote.py => emoji.py} | 14 ++--- src/main.py | 6 +- src/scanners/__init__.py | 2 +- src/scanners/channels_scanner.py | 1 - src/scanners/composition_scanner.py | 22 +++---- .../{emotes_scanner.py => emojis_scanner.py} | 60 ++++++++++--------- src/scanners/mentioned_scanner.py | 2 - src/scanners/mentions_scanner.py | 2 - src/scanners/messages_scanner.py | 1 - src/scanners/reactions_scanner.py | 1 - src/scanners/words_scanner.py | 1 - 15 files changed, 69 insertions(+), 75 deletions(-) rename src/data_types/{emote.py => emoji.py} (92%) rename src/scanners/{emotes_scanner.py => emojis_scanner.py} (72%) diff --git a/README.md b/README.md index eac16f3..fae04f0 100644 --- a/README.md +++ b/README.md @@ -24,11 +24,11 @@ * %repeat - repeat last analysis (adding supplied arguments) * %mobile - fix @invalid-user for last command but mentions users * %gdpr - displays GDPR information -* %emojis - rank emotes by their usage +* %emojis - rank emojis by their usage * arguments: * - top emojis, default is 20 * all - list all common emojis in addition to this guild's - * members - show top member for each emote + * members - show top member for each emoji * sort:usage/reaction - other sorting methods * %mentions - rank mentions by their usage * arguments: @@ -150,7 +150,7 @@ python3 src/main.py * more scans: `%scan`, `%freq`, `%compo`, `%pres` * huge bug fix * **v1.5**: - * top emotes + * top emojis * bug fix * **v1.4**: * integrate miniscord diff --git a/src/data_types/__init__.py b/src/data_types/__init__.py index e4be59f..531bb61 100644 --- a/src/data_types/__init__.py +++ b/src/data_types/__init__.py @@ -1,4 +1,4 @@ -from .emote import Emote, get_emote_dict +from .emoji import Emoji, get_emoji_dict from .frequency import Frequency from .composition import Composition from .presence import Presence diff --git a/src/data_types/composition.py b/src/data_types/composition.py index 69364a1..b16797c 100644 --- a/src/data_types/composition.py +++ b/src/data_types/composition.py @@ -8,9 +8,9 @@ class Composition: def __init__(self): self.total_characters = 0 self.plain_text = 0 - self.emote_msg = 0 - self.emote_only = 0 - self.emotes = defaultdict(int) + self.emoji_msg = 0 + self.emoji_only = 0 + self.emojis = defaultdict(int) self.edited = 0 self.everyone = 0 self.answers = 0 @@ -23,8 +23,8 @@ class Composition: self.spoilers = 0 def to_string(self, msg_count: int) -> List[str]: - total_emotes = val_sum(self.emotes) - top_emote = top_key(self.emotes) + total_emojis = val_sum(self.emojis) + top_emoji = top_key(self.emojis) ret = [ f"- **avg. characters / message**: {self.total_characters/msg_count:.2f}", f"- **plain text messages**: {self.plain_text:,} ({percent(self.plain_text/msg_count)})" @@ -42,14 +42,14 @@ class Composition: f"- **answers**: {self.answers:,} ({percent(self.answers/msg_count)})" if self.answers > 0 else "", - f"- **emojis**: {total_emotes:,} (in {percent(self.emote_msg/msg_count)} of msg, avg. {precise(total_emotes/msg_count)}/msg)" - if total_emotes > 0 + f"- **emojis**: {total_emojis:,} (in {percent(self.emoji_msg/msg_count)} of msg, avg. {precise(total_emojis/msg_count)}/msg)" + if total_emojis > 0 else "", - f"- **most used emoji**: {top_emote} ({plural(self.emotes[top_emote], 'time')}, {percent(self.emotes[top_emote]/total_emotes)})" - if total_emotes > 0 + f"- **most used emoji**: {top_emoji} ({plural(self.emojis[top_emoji], 'time')}, {percent(self.emojis[top_emoji]/total_emojis)})" + if total_emojis > 0 else "", - f"- **emoji-only messages**: {self.emote_only:,} ({percent(self.emote_only/msg_count)})" - if self.emote_only > 0 + f"- **emoji-only messages**: {self.emoji_only:,} ({percent(self.emoji_only/msg_count)})" + if self.emoji_only > 0 else "", f"- **images**: {self.images:,} ({percent(self.images/msg_count)})" if self.images > 0 diff --git a/src/data_types/counter.py b/src/data_types/counter.py index c59ad43..6996808 100644 --- a/src/data_types/counter.py +++ b/src/data_types/counter.py @@ -19,7 +19,7 @@ class Counter: def score(self) -> float: # Score is compose of usages + reactions - # When 2 emotes have the same score, + # When 2 emojis have the same score, # the days since last use is stored in the digits # (more recent first) return self.all_usages() + 1 / ( diff --git a/src/data_types/emote.py b/src/data_types/emoji.py similarity index 92% rename from src/data_types/emote.py rename to src/data_types/emoji.py index 168263d..cb052e9 100644 --- a/src/data_types/emote.py +++ b/src/data_types/emoji.py @@ -8,9 +8,9 @@ import discord from utils import mention, plural, from_now, top_key, percent -class Emote: +class Emoji: """ - Custom class to store emotes data + Custom class to store emojis data """ def __init__(self, emoji: Optional[discord.Emoji] = None): @@ -34,7 +34,7 @@ class Emote: def score(self, *, usage_weight: int = 1, react_weight: int = 1) -> float: # Score is compose of usages + reactions - # When 2 emotes have the same score, + # When 2 emojis have the same score, # the days since last use is stored in the digits # (more recent first) return ( @@ -99,8 +99,8 @@ class Emote: return output -def get_emote_dict(guild: discord.Guild) -> Dict[str, Emote]: - emotes = defaultdict(Emote) +def get_emoji_dict(guild: discord.Guild) -> Dict[str, Emoji]: + emojis = defaultdict(Emoji) for emoji in guild.emojis: - emotes[str(emoji)] = Emote(emoji) - return emotes + emojis[str(emoji)] = Emoji(emoji) + return emojis diff --git a/src/main.py b/src/main.py index fb3c194..89b9809 100644 --- a/src/main.py +++ b/src/main.py @@ -8,7 +8,7 @@ if sys.version_info < (3, 7): from utils import emojis, gdpr, command_cache from scanners import ( - EmotesScanner, + EmojisScanner, FullScanner, FrequencyScanner, CompositionScanner, @@ -115,9 +115,9 @@ bot.register_command( ) bot.register_command( "(emojis?|emotes?)", - lambda *args: EmotesScanner().compute(*args), + lambda *args: EmojisScanner().compute(*args), "emojis: rank emojis by their usage", - EmotesScanner.help(), + EmojisScanner.help(), ) bot.register_command( "(react(ions?)?)", diff --git a/src/scanners/__init__.py b/src/scanners/__init__.py index 8ac7b97..eac71a5 100644 --- a/src/scanners/__init__.py +++ b/src/scanners/__init__.py @@ -1,5 +1,5 @@ from .scanner import Scanner -from .emotes_scanner import EmotesScanner +from .emojis_scanner import EmojisScanner from .frequency_scanner import FrequencyScanner from .composition_scanner import CompositionScanner from .presence_scanner import PresenceScanner diff --git a/src/scanners/channels_scanner.py b/src/scanners/channels_scanner.py index c766fb4..8359ef2 100644 --- a/src/scanners/channels_scanner.py +++ b/src/scanners/channels_scanner.py @@ -30,7 +30,6 @@ class ChannelsScanner(Scanner): ) async def init(self, message: discord.Message, *args: str) -> bool: - # get max emotes to view self.top = 10 for arg in args: if arg.isdigit(): diff --git a/src/scanners/composition_scanner.py b/src/scanners/composition_scanner.py index a2f3822..be64c22 100644 --- a/src/scanners/composition_scanner.py +++ b/src/scanners/composition_scanner.py @@ -57,19 +57,19 @@ class CompositionScanner(Scanner): impacted = True compo.total_characters += len(message.content) - emotes_found = emojis.regex.findall(message.content) - without_emote = message.content - for name in emotes_found: + emojis_found = emojis.regex.findall(message.content) + without_emoji = message.content + for name in emojis_found: if name in emojis.unicode_list or re.match( r"(|:[\w\\-\~]+:)", name ): - 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 + compo.emojis[name] += 1 + i = without_emoji.index(name) + without_emoji = without_emoji[:i] + without_emoji[i + len(name) :] + if len(message.content.strip()) > 0 and len(without_emoji.strip()) == 0: + compo.emoji_only += 1 + if len(emojis_found) > 0: + compo.emoji_msg += 1 links_found = re.findall(r"https?:\/\/", message.content) compo.links += len(links_found) @@ -102,7 +102,7 @@ class CompositionScanner(Scanner): compo.tts += 1 if ( - len(emotes_found) == 0 + len(emojis_found) == 0 and message.reference is None and not message.image and len(message.mentions) == 0 diff --git a/src/scanners/emotes_scanner.py b/src/scanners/emojis_scanner.py similarity index 72% rename from src/scanners/emotes_scanner.py rename to src/scanners/emojis_scanner.py index b126812..c1c0de2 100644 --- a/src/scanners/emotes_scanner.py +++ b/src/scanners/emojis_scanner.py @@ -6,12 +6,12 @@ import discord # Custom libs from logs import ChannelLogs, MessageLog -from data_types import Emote, get_emote_dict +from data_types import Emoji, get_emoji_dict from .scanner import Scanner from utils import emojis, generate_help, plural, precise -class EmotesScanner(Scanner): +class EmojisScanner(Scanner): @staticmethod def help() -> str: return generate_help( @@ -31,13 +31,13 @@ class EmotesScanner(Scanner): super().__init__( has_digit_args=True, valid_args=["all", "members", "sort:usage", "sort:reaction", "everyone"], - help=EmotesScanner.help(), + help=EmojisScanner.help(), intro_context="Emoji usage", ) async def init(self, message: discord.Message, *args: str) -> bool: guild = message.channel.guild - # get max emotes to view + # get max emojis to view self.top = 20 for arg in args: if arg.isdigit(): @@ -47,8 +47,8 @@ class EmotesScanner(Scanner): self.show_members = "members" in args and ( len(self.members) == 0 or len(self.members) > 1 ) - # Create emotes dict from custom emojis of the guild - self.emotes = get_emote_dict(guild) + # Create emojis dict from custom emojis of the guild + self.emojis = get_emoji_dict(guild) self.sort = None if "sort:usage" in args: self.sort = "usage" @@ -58,36 +58,36 @@ class EmotesScanner(Scanner): return True def compute_message(self, channel: ChannelLogs, message: MessageLog): - return EmotesScanner.analyse_message( + return EmojisScanner.analyse_message( message, - self.emotes, + self.emojis, self.raw_members, all_emojis=self.all_emojis, all_messages=self.all_messages, ) def get_results(self, intro: str) -> List[str]: - names = [name for name in self.emotes] + names = [name for name in self.emojis] names.sort( - key=lambda name: self.emotes[name].score( + key=lambda name: self.emojis[name].score( usage_weight=(0 if self.sort == "reaction" else 1), react_weight=(0 if self.sort == "usage" else 1), ), reverse=True, ) names = names[: self.top] - # Get the total of all emotes used + # Get the total of all emojis used usage_count = 0 reaction_count = 0 - for name in self.emotes: - usage_count += self.emotes[name].usages - reaction_count += self.emotes[name].reactions + for name in self.emojis: + usage_count += self.emojis[name].usages + reaction_count += self.emojis[name].reactions res = [intro] allow_unused = self.full and len(self.members) == 0 if self.sort is not None: res += [f"(Sorted by {self.sort})"] res += [ - self.emotes[name].to_string( + self.emojis[name].to_string( names.index(name), name, total_usage=usage_count, @@ -96,7 +96,7 @@ class EmotesScanner(Scanner): show_members=self.show_members or len(self.raw_members) == 0, ) for name in names - if allow_unused or self.emotes[name].used() + if allow_unused or self.emojis[name].used() ] res += [ f"Total: {plural(usage_count,'time')} ({precise(usage_count/self.msg_count)}/msg)" @@ -108,7 +108,7 @@ class EmotesScanner(Scanner): @staticmethod def analyse_message( message: MessageLog, - emotes: Dict[str, Emote], + emojis_dict: Dict[str, Emoji], raw_members: List[int], *, all_emojis: bool, @@ -122,27 +122,29 @@ class EmotesScanner(Scanner): or message.author in raw_members ): impacted = True - # Find all emotes un the current message in the form "<:emoji:123456789>" - # Filter for known emotes + # Find all emojis un the current message in the form "<:emoji:123456789>" + # Filter for known emojis found = emojis.regex.findall(message.content) - # For each emote, update its usage + # For each emoji, update its usage for name in found: - if name not in emotes: + if name not in emojis_dict: 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 + emojis_dict[name].usages += 1 + emojis_dict[name].update_use(message.created_at, [message.author]) + # For each reaction of this message, test if known emoji and update when it's the case for name in message.reactions: - if name not in emotes: + if name not in emojis_dict: 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]) + emojis_dict[name].reactions += len(message.reactions[name]) + emojis_dict[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]) + emojis_dict[name].reactions += 1 + emojis_dict[name].update_use(message.created_at, [member]) return impacted diff --git a/src/scanners/mentioned_scanner.py b/src/scanners/mentioned_scanner.py index fa6c09e..cdb9b5d 100644 --- a/src/scanners/mentioned_scanner.py +++ b/src/scanners/mentioned_scanner.py @@ -31,7 +31,6 @@ class MentionedScanner(Scanner): ) async def init(self, message: discord.Message, *args: str) -> bool: - # get max emotes to view self.top = 10 for arg in args: if arg.isdigit(): @@ -55,7 +54,6 @@ class MentionedScanner(Scanner): names = [name for name in self.mentions] names.sort(key=lambda name: self.mentions[name].score(), reverse=True) names = names[: self.top] - # Get the total of all emotes used usage_count = Counter.total(self.mentions) res = [intro] res += [ diff --git a/src/scanners/mentions_scanner.py b/src/scanners/mentions_scanner.py index 50a0f5c..21e7246 100644 --- a/src/scanners/mentions_scanner.py +++ b/src/scanners/mentions_scanner.py @@ -42,7 +42,6 @@ class MentionsScanner(Scanner): ) async def init(self, message: discord.Message, *args: str) -> bool: - # get max emotes to view self.top = 10 for arg in args: if arg.isdigit(): @@ -67,7 +66,6 @@ class MentionsScanner(Scanner): names = [name for name in self.mentions] names.sort(key=lambda name: self.mentions[name].score(), reverse=True) names = names[: self.top] - # Get the total of all emotes used usage_count = Counter.total(self.mentions) res = [intro] res += [ diff --git a/src/scanners/messages_scanner.py b/src/scanners/messages_scanner.py index a79735e..4c775e6 100644 --- a/src/scanners/messages_scanner.py +++ b/src/scanners/messages_scanner.py @@ -30,7 +30,6 @@ class MessagesScanner(Scanner): ) async def init(self, message: discord.Message, *args: str) -> bool: - # get max emotes to view self.top = 10 for arg in args: if arg.isdigit(): diff --git a/src/scanners/reactions_scanner.py b/src/scanners/reactions_scanner.py index 3603a06..25a1a88 100644 --- a/src/scanners/reactions_scanner.py +++ b/src/scanners/reactions_scanner.py @@ -29,7 +29,6 @@ class ReactionsScanner(Scanner): ) async def init(self, message: discord.Message, *args: str) -> bool: - # get max emotes to view self.top = 10 for arg in args: if arg.isdigit(): diff --git a/src/scanners/words_scanner.py b/src/scanners/words_scanner.py index f7f6dd7..4460938 100644 --- a/src/scanners/words_scanner.py +++ b/src/scanners/words_scanner.py @@ -67,7 +67,6 @@ class WordsScanner(Scanner): words = [word for word in self.words] words.sort(key=lambda word: self.words[word].score(), reverse=True) words = words[: self.top] - # Get the total of all emotes used usage_count = Counter.total(self.words) print(len(self.words)) res = [intro.format(self.letters)]