From aa51e2cda0205948662a2d13949ab493d1ddd7bc Mon Sep 17 00:00:00 2001 From: klemek Date: Wed, 13 Jan 2021 16:41:10 +0100 Subject: [PATCH] %mentioned command --- src/main.py | 7 ++ src/scanners/__init__.py | 1 + src/scanners/mentioned_scanner.py | 104 ++++++++++++++++++++++++++++++ src/utils/utils.py | 6 +- 4 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 src/scanners/mentioned_scanner.py diff --git a/src/main.py b/src/main.py index d41df9c..6210987 100644 --- a/src/main.py +++ b/src/main.py @@ -9,6 +9,7 @@ from scanners import ( CompositionScanner, PresenceScanner, MentionsScanner, + MentionedScanner, ) from logs import GuildLogs @@ -32,6 +33,12 @@ bot.register_command( "cancel: stop current analysis", "```\n" + "%cancel : Stop current analysis\n" + "```", ) +bot.register_command( + "(mentioned)", + lambda *args: MentionedScanner().compute(*args), + "mentioned: rank specific user mentions by their usage", + MentionedScanner.help(), +) bot.register_command( "(mentions?)", lambda *args: MentionsScanner().compute(*args), diff --git a/src/scanners/__init__.py b/src/scanners/__init__.py index 1d8bcf6..47999ff 100644 --- a/src/scanners/__init__.py +++ b/src/scanners/__init__.py @@ -4,3 +4,4 @@ from .composition_scanner import CompositionScanner from .presence_scanner import PresenceScanner from .full_scanner import FullScanner from .mentions_scanner import MentionsScanner +from .mentioned_scanner import MentionedScanner diff --git a/src/scanners/mentioned_scanner.py b/src/scanners/mentioned_scanner.py new file mode 100644 index 0000000..8c2f63a --- /dev/null +++ b/src/scanners/mentioned_scanner.py @@ -0,0 +1,104 @@ +from typing import Dict, List +from collections import defaultdict +import discord + + +# Custom libs + +from logs import ChannelLogs, MessageLog +from .scanner import Scanner +from data_types import Mention +from utils import ( + COMMON_HELP_ARGS, + plural, + precise, + mention, + alt_mention, + role_mention, + channel_mention, +) + + +class MentionedScanner(Scanner): + @staticmethod + def help() -> str: + return ( + "```\n" + + "%mentioned : Rank specific user's mentions by their usage\n" + + "arguments:\n" + + "* @member/me : (required) one or more member\n" + + "\n".join(COMMON_HELP_ARGS.split("\n")[1:]) + + "* : top mentions, default is 10\n" + + "* all: include bots mentions\n" + + "Example: %mentioned 10 @user\n" + + "```" + ) + + def __init__(self): + super().__init__( + valid_args=["all"], + help=MentionedScanner.help(), + intro_context="Mentioned by members", + ) + self.top = 10 + + 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(): + self.top = int(arg) + if len(self.members) == 0: + await message.channel.send( + "You need to mention at least one member or use `me`", reference=message + ) + return False + self.all_mentions = "all" in args + # Create mentions dict + self.mentions = defaultdict(Mention) + return True + + def compute_message(self, channel: ChannelLogs, message: MessageLog): + return MentionedScanner.analyse_message( + message, self.mentions, self.raw_members, all_mentions=self.all_mentions + ) + + def get_results(self, intro: str) -> List[str]: + 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 = sum([mention.usages for mention in self.mentions.values()]) + res = [intro] + res += [ + self.mentions[name].to_string( + names.index(name), + name, + total_usage=usage_count, + ) + for name in names + ] + res += [ + f"Total: {plural(usage_count,'time')} ({precise(usage_count/self.msg_count)}/msg)" + ] + return res + + @staticmethod + def analyse_message( + message: MessageLog, + mentions: Dict[str, Mention], + raw_members: List[int], + *, + all_mentions: bool, + ) -> bool: + impacted = True + if not message.bot or all_mentions: + for member_id in message.mentions: + if member_id in raw_members: + count = message.content.count( + mention(member_id) + ) + message.content.count(alt_mention(member_id)) + mentions[mention(message.author)].update_use( + count, message.created_at + ) + return impacted diff --git a/src/utils/utils.py b/src/utils/utils.py index a8f36a5..8807fd6 100644 --- a/src/utils/utils.py +++ b/src/utils/utils.py @@ -1,4 +1,4 @@ -from typing import List, Dict, Union +from typing import List, Dict, Union, Optional import os import logging import discord @@ -135,7 +135,9 @@ def str_datetime(date: datetime) -> str: return date.strftime("%H:%M, %d %b. %Y") # 12:05, 12 Jun. 2018 -def from_now(src: datetime) -> str: +def from_now(src: Optional[datetime]) -> str: + if src is None: + return "never" delay = datetime.utcnow() - src seconds = delay.seconds minutes = seconds // 60