Merge pull request #38 from Klemek/dev

v1.14 fix
This commit is contained in:
Klemek
2021-04-22 16:23:18 +02:00
committed by GitHub
2 changed files with 41 additions and 19 deletions
+27 -8
View File
@@ -1,7 +1,6 @@
from typing import Dict, List from typing import Dict, List
from collections import defaultdict from collections import defaultdict
import discord import discord
import re
# Custom libs # Custom libs
@@ -13,6 +12,7 @@ from utils import (
plural, plural,
precise, precise,
mention, mention,
escape_text,
) )
@@ -23,6 +23,7 @@ class FindScanner(Scanner):
"find", "find",
"Find specific words or phrases (you can use quotes to add spaces in queries)", "Find specific words or phrases (you can use quotes to add spaces in queries)",
args=[ args=[
"top - rank users for these queries",
"all/everyone - include bots", "all/everyone - include bots",
], ],
example='#mychannel1 #mychannel2 @user "I love you" "you too"', example='#mychannel1 #mychannel2 @user "I love you" "you too"',
@@ -31,7 +32,7 @@ class FindScanner(Scanner):
def __init__(self): def __init__(self):
super().__init__( super().__init__(
all_args=True, all_args=True,
valid_args=["all", "everyone"], valid_args=["all", "everyone", "top"],
help=FindScanner.help(), help=FindScanner.help(),
intro_context="Matches", intro_context="Matches",
) )
@@ -39,6 +40,7 @@ class FindScanner(Scanner):
async def init(self, message: discord.Message, *args: str) -> bool: async def init(self, message: discord.Message, *args: str) -> bool:
self.matches = defaultdict(Counter) self.matches = defaultdict(Counter)
self.all_messages = "all" in args or "everyone" in args self.all_messages = "all" in args or "everyone" in args
self.top = "top" in args
if len(self.other_args) == 0: if len(self.other_args) == 0:
await message.channel.send( await message.channel.send(
"You need to add a query to find (you can use quotes to add spaces in queries)", "You need to add a query to find (you can use quotes to add spaces in queries)",
@@ -54,17 +56,30 @@ class FindScanner(Scanner):
self.other_args, self.other_args,
self.raw_members, self.raw_members,
all_messages=self.all_messages, all_messages=self.all_messages,
top=self.top,
) )
def get_results(self, intro: str) -> List[str]: def get_results(self, intro: str) -> List[str]:
res = [intro]
matches = [match for match in self.matches] matches = [match for match in self.matches]
matches.sort(key=lambda match: self.matches[match].score(), reverse=True) matches.sort(key=lambda match: self.matches[match].score(), reverse=True)
usage_count = Counter.total(self.matches) usage_count = Counter.total(self.matches)
res = [intro] if self.top:
res += [ res += [
self.matches[match].to_string( self.matches[match].to_string(
matches.index(match), matches.index(match),
f"`{match}`", mention(match),
total_usage=usage_count,
)
for match in matches
]
else:
res += [
self.matches[match].to_string(
matches.index(match),
f'"{escape_text(match)}"'
if not match.startswith("<:")
else escape_text(match),
total_usage=self.msg_count, total_usage=self.msg_count,
ranking=False, ranking=False,
transform=lambda id: f" by {mention(id)}", transform=lambda id: f" by {mention(id)}",
@@ -72,7 +87,7 @@ class FindScanner(Scanner):
) )
for match in matches for match in matches
] ]
if len(matches) > 1: if self.top or len(matches) > 1:
res += [ res += [
f"Total: {plural(usage_count,'time')} ({precise(usage_count/self.msg_count)}/msg)" f"Total: {plural(usage_count,'time')} ({precise(usage_count/self.msg_count)}/msg)"
] ]
@@ -88,6 +103,7 @@ class FindScanner(Scanner):
raw_members: List[int], raw_members: List[int],
*, *,
all_messages: bool, all_messages: bool,
top: bool,
) -> bool: ) -> bool:
impacted = False impacted = False
# If author is included in the selection (empty list is all) # If author is included in the selection (empty list is all)
@@ -99,7 +115,10 @@ class FindScanner(Scanner):
impacted = True impacted = True
content = message.content.lower() content = message.content.lower()
for query in queries: for query in queries:
matches[query].update_use( count = content.count(query.lower())
content.count(query.lower()), message.created_at, message.author if top:
) if count > 0:
matches[message.author].update_use(count, message.created_at)
else:
matches[query].update_use(count, message.created_at, message.author)
return impacted return impacted
+4 -1
View File
@@ -1,4 +1,3 @@
from calendar import month
from typing import Callable, List, Dict, Union, Optional, Any from typing import Callable, List, Dict, Union, Optional, Any
import os import os
import logging import logging
@@ -81,6 +80,10 @@ def message_link(message: discord.Message) -> str:
return f"https://discord.com/channels/{message.channel.guild.id}/{message.channel.id}/{message.id}" return f"https://discord.com/channels/{message.channel.guild.id}/{message.channel.id}/{message.id}"
def escape_text(text: str) -> str:
return discord.utils.escape_markdown(discord.utils.escape_mentions(text))
class FakeMessage: class FakeMessage:
def __init__(self, id: int): def __init__(self, id: int):
self.id = id self.id = id