From d2cdea3db6b33cc0f56cb8fe86eeea184979a609 Mon Sep 17 00:00:00 2001 From: Klemek Date: Thu, 22 Apr 2021 16:12:13 +0200 Subject: [PATCH 1/2] escape text in find scanner --- src/scanners/find_scanner.py | 6 ++++-- src/utils/utils.py | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/scanners/find_scanner.py b/src/scanners/find_scanner.py index 34a5397..1cfe60f 100644 --- a/src/scanners/find_scanner.py +++ b/src/scanners/find_scanner.py @@ -1,7 +1,6 @@ from typing import Dict, List from collections import defaultdict import discord -import re # Custom libs @@ -13,6 +12,7 @@ from utils import ( plural, precise, mention, + escape_text, ) @@ -64,7 +64,9 @@ class FindScanner(Scanner): res += [ self.matches[match].to_string( matches.index(match), - f"`{match}`", + f'"{escape_text(match)}"' + if not match.startswith("<:") + else escape_text(match), total_usage=self.msg_count, ranking=False, transform=lambda id: f" by {mention(id)}", diff --git a/src/utils/utils.py b/src/utils/utils.py index 7e5e9b6..4457326 100644 --- a/src/utils/utils.py +++ b/src/utils/utils.py @@ -1,4 +1,3 @@ -from calendar import month from typing import Callable, List, Dict, Union, Optional, Any import os 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}" +def escape_text(text: str) -> str: + return discord.utils.escape_markdown(discord.utils.escape_mentions(text)) + + class FakeMessage: def __init__(self, id: int): self.id = id From 8d1875a3629e5792a68b7cbcb529b11aa3cdd413 Mon Sep 17 00:00:00 2001 From: Klemek Date: Thu, 22 Apr 2021 16:22:26 +0200 Subject: [PATCH 2/2] top arg for %find --- src/scanners/find_scanner.py | 55 +++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/src/scanners/find_scanner.py b/src/scanners/find_scanner.py index 1cfe60f..0fd78e9 100644 --- a/src/scanners/find_scanner.py +++ b/src/scanners/find_scanner.py @@ -23,6 +23,7 @@ class FindScanner(Scanner): "find", "Find specific words or phrases (you can use quotes to add spaces in queries)", args=[ + "top - rank users for these queries", "all/everyone - include bots", ], example='#mychannel1 #mychannel2 @user "I love you" "you too"', @@ -31,7 +32,7 @@ class FindScanner(Scanner): def __init__(self): super().__init__( all_args=True, - valid_args=["all", "everyone"], + valid_args=["all", "everyone", "top"], help=FindScanner.help(), intro_context="Matches", ) @@ -39,6 +40,7 @@ class FindScanner(Scanner): async def init(self, message: discord.Message, *args: str) -> bool: self.matches = defaultdict(Counter) self.all_messages = "all" in args or "everyone" in args + self.top = "top" in args if len(self.other_args) == 0: await message.channel.send( "You need to add a query to find (you can use quotes to add spaces in queries)", @@ -54,27 +56,38 @@ class FindScanner(Scanner): self.other_args, self.raw_members, all_messages=self.all_messages, + top=self.top, ) def get_results(self, intro: str) -> List[str]: + res = [intro] matches = [match for match in self.matches] matches.sort(key=lambda match: self.matches[match].score(), reverse=True) usage_count = Counter.total(self.matches) - res = [intro] - 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, - ranking=False, - transform=lambda id: f" by {mention(id)}", - top=len(self.members) != 1, - ) - for match in matches - ] - if len(matches) > 1: + if self.top: + res += [ + self.matches[match].to_string( + matches.index(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, + ranking=False, + transform=lambda id: f" by {mention(id)}", + top=len(self.members) != 1, + ) + for match in matches + ] + if self.top or len(matches) > 1: res += [ f"Total: {plural(usage_count,'time')} ({precise(usage_count/self.msg_count)}/msg)" ] @@ -90,6 +103,7 @@ class FindScanner(Scanner): raw_members: List[int], *, all_messages: bool, + top: bool, ) -> bool: impacted = False # If author is included in the selection (empty list is all) @@ -101,7 +115,10 @@ class FindScanner(Scanner): impacted = True content = message.content.lower() for query in queries: - matches[query].update_use( - content.count(query.lower()), message.created_at, message.author - ) + count = content.count(query.lower()) + 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