%mentioned command

This commit is contained in:
klemek
2021-01-13 16:41:10 +01:00
parent 86dc115697
commit aa51e2cda0
4 changed files with 116 additions and 2 deletions
+7
View File
@@ -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),
+1
View File
@@ -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
+104
View File
@@ -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:])
+ "* <n>: top <n> 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
+4 -2
View File
@@ -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