backticks in %find can use regexes
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
from typing import Dict, List
|
from typing import Dict, List, Optional, Tuple
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
import discord
|
import discord
|
||||||
|
import re
|
||||||
|
|
||||||
# Custom libs
|
# Custom libs
|
||||||
|
|
||||||
@@ -21,7 +22,7 @@ class FindScanner(Scanner):
|
|||||||
def help() -> str:
|
def help() -> str:
|
||||||
return generate_help(
|
return generate_help(
|
||||||
"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, backticks define regexes)",
|
||||||
args=[
|
args=[
|
||||||
"top - rank users for these queries",
|
"top - rank users for these queries",
|
||||||
"all/everyone - include bots",
|
"all/everyone - include bots",
|
||||||
@@ -43,17 +44,18 @@ class FindScanner(Scanner):
|
|||||||
self.top = "top" in args or len(self.other_args) == 1
|
self.top = "top" in args or len(self.other_args) == 1
|
||||||
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, backticks define regexes)",
|
||||||
reference=message,
|
reference=message,
|
||||||
)
|
)
|
||||||
return False
|
return False
|
||||||
|
self.queries = [(query, query.strip("`") if re.match(r"^`.*`$", query) else None) for query in self.other_args]
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def compute_message(self, channel: ChannelLogs, message: MessageLog):
|
def compute_message(self, channel: ChannelLogs, message: MessageLog):
|
||||||
return FindScanner.analyse_message(
|
return FindScanner.analyse_message(
|
||||||
message,
|
message,
|
||||||
self.matches,
|
self.matches,
|
||||||
self.other_args,
|
self.queries,
|
||||||
self.raw_members,
|
self.raw_members,
|
||||||
all_messages=self.all_messages,
|
all_messages=self.all_messages,
|
||||||
top=self.top,
|
top=self.top,
|
||||||
@@ -77,7 +79,9 @@ class FindScanner(Scanner):
|
|||||||
res += [
|
res += [
|
||||||
self.matches[match].to_string(
|
self.matches[match].to_string(
|
||||||
matches.index(match),
|
matches.index(match),
|
||||||
f'"{escape_text(match)}"',
|
f'"{escape_text(match)}"'
|
||||||
|
if len(match.strip("`")) == len(match)
|
||||||
|
else 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)}",
|
||||||
@@ -97,7 +101,7 @@ class FindScanner(Scanner):
|
|||||||
def analyse_message(
|
def analyse_message(
|
||||||
message: MessageLog,
|
message: MessageLog,
|
||||||
matches: Dict[str, Counter],
|
matches: Dict[str, Counter],
|
||||||
queries: List[str],
|
queries: List[Tuple[str, Optional[str]]],
|
||||||
raw_members: List[int],
|
raw_members: List[int],
|
||||||
*,
|
*,
|
||||||
all_messages: bool,
|
all_messages: bool,
|
||||||
@@ -113,10 +117,13 @@ class FindScanner(Scanner):
|
|||||||
impacted = True
|
impacted = True
|
||||||
content = message.content.lower()
|
content = message.content.lower()
|
||||||
for query in queries:
|
for query in queries:
|
||||||
count = content.count(query.lower())
|
if query[1] is not None:
|
||||||
|
count = len(re.findall(query[1], message.content))
|
||||||
|
else:
|
||||||
|
count = content.count(query[0].lower())
|
||||||
if top:
|
if top:
|
||||||
if count > 0:
|
if count > 0:
|
||||||
matches[message.author].update_use(count, message.created_at)
|
matches[message.author].update_use(count, message.created_at)
|
||||||
else:
|
else:
|
||||||
matches[query].update_use(count, message.created_at, message.author)
|
matches[query[0]].update_use(count, message.created_at, message.author)
|
||||||
return impacted
|
return impacted
|
||||||
|
|||||||
Reference in New Issue
Block a user