code into packages

This commit is contained in:
klemek
2021-01-08 12:57:23 +01:00
parent 038b1a294c
commit 5a965bbc7a
14 changed files with 12 additions and 11 deletions
+1
View File
@@ -0,0 +1 @@
from .emotes_scanner import EmotesScanner
View File
+120
View File
@@ -0,0 +1,120 @@
from typing import Dict, List
import discord
# Custom libs
from utils.log_manager import ChannelLogs, MessageLog
from utils.data_types import Emote
from .scanner import Scanner
from utils import emojis
class EmotesScanner(Scanner):
@staticmethod
def help() -> str:
return "```\n"
+"%emotes : Rank emotes by their usage\n"
+"arguments:\n"
+"* @member : filter for one or more member\n"
+"* #channel : filter for one or more channel\n"
+"* <n> : top <n> emojis, default is 20\n"
+"* all : list all common emojis in addition to this guild's\n"
+"* members : show top member for each emote\n"
+"Example: %emotes 10 all #mychannel1 #mychannel2 @user\n"
+"```"
def __init__(self):
super().__init__(
has_digit_args=True,
valid_args=["all", "members"],
help=EmotesScanner.help(),
intro_context="emotes usage",
)
self.top = 20
self.all_emojis = False
self.show_members = False
async def compute_args(self, message: discord.Message, *args: str) -> bool:
# get max emotes to view
self.top = 20
for arg in args:
if arg.isdigit():
self.top = int(arg)
# check other args
self.all_emojis = "all" in args
self.show_members = "members" in args and (
len(self.members) == 0 or len(self.members) > 1
)
return True
def compute_message(self, channel: ChannelLogs, message: MessageLog):
return analyse_message(
message, self.emotes, self.raw_members, all_emojis=self.all_emojis
)
def get_results(self, intro: str) -> List[str]:
names = [name for name in self.emotes]
names.sort(key=lambda name: self.emotes[name].score(), reverse=True)
names = names[: self.top]
res = [intro]
allow_unused = self.full and len(self.members) == 0
res += [
self.emotes[name].to_string(
names.index(name), name, False, self.show_members
)
for name in names
if allow_unused or self.emotes[name].used()
]
# Get the total of all emotes used
usage_count = 0
reaction_count = 0
for name in self.emotes:
usage_count += self.emotes[name].usages
reaction_count += self.emotes[name].reactions
res += [
f"Total: {usage_count:,} times ({usage_count / self.msg_count:.4f} / message)"
]
if reaction_count > 0:
res[-1] += f" and {reaction_count:,} reactions"
return res
# ANALYSIS
def analyse_message(
message: MessageLog,
emotes: Dict[str, Emote],
raw_members: List[int],
*,
all_emojis: bool,
) -> bool:
impacted = False
# If author is included in the selection (empty list is all)
if not message.bot and (len(raw_members) == 0 or message.author in raw_members):
impacted = True
# Find all emotes un the current message in the form "<:emoji:123456789>"
# Filter for known emotes
found = emojis.regex.findall(message.content)
# For each emote, update its usage
for name in found:
if name not in emotes:
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
for name in message.reactions:
if name not in emotes:
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])
else:
for member in raw_members:
if member in message.reactions[name]:
emotes[name].reactions += 1
emotes[name].update_use(message.created_at, [member])
return impacted
View File
View File
+131
View File
@@ -0,0 +1,131 @@
from abc import ABC, abstractmethod
from typing import List
from collections import defaultdict
import discord
from utils.utils import no_duplicate, get_intro
from utils.log_manager import GuildLogs, ChannelLogs, MessageLog
from utils.data_types import Emote
class Scanner(ABC):
def __init__(
self,
*,
has_digit_args: bool,
valid_args: List[str],
help: str,
intro_context: str,
):
self.has_digit_args = has_digit_args
self.valid_args = valid_args
self.help = help
self.intro_context = intro_context
self.members = []
self.raw_members = []
self.full = False
self.channels = []
self.msg_count = 0
self.chan_count = 0
async def compute(
self, client: discord.client, message: discord.Message, *args: str
):
guild = message.guild
logs = GuildLogs(guild)
# If "%cmd help" redirect to "%help cmd"
if "help" in args:
await client.bot.help(client, message, "help", args[0])
return
# check args validity
str_channel_mentions = [channel.mention for channel in message.channel_mentions]
str_mentions = [member.mention for member in message.mentions]
for arg in args[1:]:
if (
arg not in self.valid_args
and (not arg.isdigit() or not self.has_digit_args)
and arg not in str_channel_mentions
and arg not in str_mentions
):
await message.channel.send(
f"{message.author.mention} unrecognized argument: `{arg}`"
)
return
# Create emotes dict from custom emojis of the guild
self.emotes = defaultdict(Emote)
for emoji in guild.emojis:
self.emotes[str(emoji)] = Emote(emoji)
# Get selected channels or all of them if no channel arguments
self.channels = no_duplicate(message.channel_mentions)
self.full = len(self.channels) == 0
if self.full:
self.channels = guild.text_channels
# Get selected members
self.members = no_duplicate(message.mentions)
self.raw_members = no_duplicate(message.raw_mentions)
if not await self.compute_args(message, *args):
return
# Start computing data
async with message.channel.typing():
progress = await message.channel.send("```Starting analysis...```")
total_msg, total_chan = await logs.load(progress, self.channels)
if total_msg == -1:
await message.channel.send(
f"{message.author.mention} An analysis is already running on this server, please be patient."
)
else:
self.msg_count = 0
self.chan_count = 0
for channel in self.channels:
channel_logs = logs.channels[channel.id]
count = sum(
[
self.compute_message(channel_logs, message_log)
for message_log in channel_logs.messages
]
)
self.msg_count += count
self.chan_count += 1 if count > 0 else 0
await progress.edit(content="```Computing results...```")
# Display results
results = self.get_results(
get_intro(
self.intro_context,
self.full,
self.channels,
self.members,
self.msg_count,
self.chan_count,
)
)
response = ""
for r in results:
if len(response + "\n" + r) > 2000:
await message.channel.send(response)
response = ""
response += "\n" + r
if len(response) > 0:
await message.channel.send(response)
# Delete custom progress message
await progress.delete()
@abstractmethod
async def compute_args(self, message: discord.Message, *args: str) -> bool:
pass
@abstractmethod
def compute_message(self, channel: ChannelLogs, message: MessageLog) -> bool:
pass
@abstractmethod
def get_results(self, intro: str):
pass