emotes => emojis

This commit is contained in:
Klemek
2021-04-22 13:09:07 +02:00
parent 6afb05148d
commit f8e294f647
15 changed files with 69 additions and 75 deletions
+3 -3
View File
@@ -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:
* <n> - top <n> 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 <n> emotes
* top <n> emojis
* bug fix
* **v1.4**:
* integrate miniscord
+1 -1
View File
@@ -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
+11 -11
View File
@@ -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
+1 -1
View File
@@ -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 / (
@@ -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
+3 -3
View File
@@ -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?)?)",
+1 -1
View File
@@ -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
-1
View File
@@ -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():
+11 -11
View File
@@ -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"(<a?:[\w\-\~]+:\d+>|:[\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
@@ -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
-2
View File
@@ -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 += [
-2
View File
@@ -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 += [
-1
View File
@@ -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():
-1
View File
@@ -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():
-1
View File
@@ -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)]