(WIP) refactor to extract scan constants

This commit is contained in:
klemek
2021-01-07 18:28:52 +01:00
parent 69c451d3aa
commit 03003f24b0
5 changed files with 39 additions and 35 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ emojis.load_emojis()
bot = Bot( bot = Bot(
"Discord Analyst", # name "Discord Analyst", # name
"1.5", # version "1.6", # version
alias="%", # respond to '|command' messages alias="%", # respond to '|command' messages
) )
bot.log_calls = True bot.log_calls = True
+36 -32
View File
@@ -7,7 +7,7 @@ import discord
# Custom libs # Custom libs
from utils import no_duplicate, mention, plural, day_interval, get_intro from utils import no_duplicate, mention, plural, day_interval, get_intro
from log_manager import GuildLogs, ChannelLogs from log_manager import GuildLogs, MessageLog
import emojis import emojis
# CONSTANTS # CONSTANTS
@@ -92,8 +92,13 @@ async def compute(client: discord.client, message: discord.Message, *args: str):
msg_count = 0 msg_count = 0
chan_count = 0 chan_count = 0
for id in logs.channels: for id in logs.channels:
count = analyse_channel( count = sum(
logs.channels[id], emotes, raw_members, all_emojis=all_emojis [
analyse_message(
messagelog, emotes, raw_members, all_emojis=all_emojis
)
for messagelog in logs.channels[id].messages
]
) )
msg_count += count msg_count += count
chan_count += 1 if count > 0 else 0 chan_count += 1 if count > 0 else 0
@@ -198,42 +203,41 @@ class Emote:
# ANALYSIS # ANALYSIS
def analyse_channel( def analyse_message(
channel: ChannelLogs, message: MessageLog,
emotes: Dict[str, Emote], emotes: Dict[str, Emote],
raw_members: List[int], raw_members: List[int],
*, *,
all_emojis: bool, all_emojis: bool,
) -> int: ) -> bool:
count = 0 impacted = False
for message in channel.messages: # If author is included in the selection (empty list is all)
# 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):
if not message.bot and (len(raw_members) == 0 or message.author in raw_members): impacted = True
count += 1 # Find all emotes un the current message in the form "<:emoji:123456789>"
# Find all emotes un the current message in the form "<:emoji:123456789>" # Filter for known emotes
# Filter for known emotes found = emojis.regex.findall(message.content)
found = emojis.regex.findall(message.content) # For each emote, update its usage
# For each emote, update its usage for name in found:
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 name not in emotes:
if not all_emojis or name not in emojis.unicode_list: if not all_emojis or name not in emojis.unicode_list:
continue continue
if len(raw_members) == 0: emotes[name].usages += 1
emotes[name].reactions += len(message.reactions[name]) emotes[name].update_use(message.created_at, [message.author])
emotes[name].update_use(message.created_at, message.reactions[name]) # For each reaction of this message, test if known emote and update when it's the case
else: for name in message.reactions:
for member in raw_members: if name not in emotes:
if member in message.reactions[name]: if not all_emojis or name not in emojis.unicode_list:
emotes[name].reactions += 1 continue
emotes[name].update_use(message.created_at, [member]) if len(raw_members) == 0:
return count 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
# RESULTS # RESULTS
+1 -1
View File
@@ -46,7 +46,7 @@ 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
for attachment in message.attachment: 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
break break
View File
+1 -1
View File
@@ -59,7 +59,7 @@ def aggregate(names: List[str]) -> str:
def plural(count: int, word: str) -> str: def plural(count: int, word: str) -> str:
return str(count) + " " + word + "s" if count == 1 else "" return str(count) + " " + word + ("s" if count != 1 else "")
def day_interval(interval: int) -> str: def day_interval(interval: int) -> str: