Compare commits

...

2 commits

Author SHA1 Message Date
9cd672e382
paginate embed notifications 2025-08-04 19:15:32 -04:00
a0afb916e7
expand keyword tests 2025-08-04 17:57:19 -04:00
2 changed files with 114 additions and 62 deletions

View file

@ -74,30 +74,45 @@ async def handle_notification(db_updates, ctx, message, keyword, user_id, use_em
footer = f"<t:{int(message.created_at.timestamp())}:R> | [Show]({message.jump_url}) | <#{ctx.channel.id}>" footer = f"<t:{int(message.created_at.timestamp())}:R> | [Show]({message.jump_url}) | <#{ctx.channel.id}>"
content = message.content or "(*Keyword in embed*)" content = message.content or "(*Keyword in embed*)"
if use_embed:
paginator = Paginator(prefix="", suffix="", max_size=4000)
else:
paginator = Paginator(prefix="", suffix="")
paginator.add_line(header)
paginator.add_line()
content = indent(content, "> ", lambda line: True)
for line in content.strip().split("\n"):
if len(line) < 1990:
paginator.add_line(line)
else:
paginator.add_line(line[:1975] + " (... Line too long)")
paginator.add_line()
paginator.add_line(footer)
if use_embed: if use_embed:
log.debug("Sending embed") log.debug("Sending embed")
embed = Embed(description=content + "\n\n" + footer) embeds = []
embed = Embed(description=paginator.pages[0])
embed.set_author( embed.set_author(
name=f"{message.author.display_name} ({message.author})", name=f"{message.author.display_name} ({message.author})",
icon_url=message.author.display_avatar, icon_url=message.author.display_avatar,
) )
embeds.append(embed)
for page in paginator.pages[1:]:
embeds.append(description=page)
embeds.extend(message.embeds)
try: try:
await member.send(header, embeds=[embed] + message.embeds[:9]) await member.send(header, embeds=embeds[:10])
except Forbidden: except Forbidden:
log.warning("Cannot send messages to this user") log.warning("Cannot send messages to this user")
else: else:
log.debug("Sending plain message") log.debug("Sending plain message")
paginator = Paginator(prefix="", suffix="")
paginator.add_line(header)
paginator.add_line()
for line in indent(content, "> ", lambda line: True).strip().split("\n"):
if len(line) < 1990:
paginator.add_line(line)
else:
paginator.add_line(line[:1975] + " (... Line too long)")
paginator.add_line()
paginator.add_line(footer)
try: try:
for page in paginator.pages[:-1]: for page in paginator.pages[:-1]:
await member.send(page) await member.send(page)

View file

@ -1,55 +1,15 @@
import logging import logging
from asyncio import TimeoutError from asyncio import TimeoutError
from itertools import chain
import re2 as re import re2 as re
from disnake.ext.commands import BadArgument from disnake.ext.commands import BadArgument
log = logging.getLogger("nomen.utils") log = logging.getLogger("nomen.utils")
ALPHABET = list("abcdefghijklmnopqrstuvwxyz ") ALPHABET = "abcdefghijklmnopqrstuvwxyz"
SYMBOLS = "!@#$%^&*()[]{}-_=+`~;:'\"/\\"
COMMON_WORDS = [ PRONOUNS = [
# common words
"of",
"in",
"is",
"to",
"it",
"as",
"on",
"by",
"or",
"be",
"an",
"at",
"if",
"up",
"so",
"do",
"th",
"no",
"de",
"the",
"and",
"was",
"for",
"that",
"are",
"with",
"from",
"this",
"not",
"also",
"has",
"were",
"which",
"have",
"people",
"one",
"can",
"help",
# pronouns
"you", "you",
"your", "your",
"yours", "yours",
@ -67,15 +27,92 @@ COMMON_WORDS = [
"theirs", "theirs",
"themself", "themself",
"themselves", "themselves",
"a" * 100, "we",
"me",
"any",
"us",
] ]
TESTS = list( NUMBERS = [
chain( *"0123456789",
ALPHABET, # single letters "10",
COMMON_WORDS, "zero",
) "one",
) "two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
]
COMMON_WORDS = [
"about",
"after",
"also",
"an",
"and",
"are",
"as",
"at",
"be",
"by",
"can",
"do",
"for",
"from",
"has",
"have",
"help",
"if",
"in",
"into",
"is",
"it",
"no",
"not",
"of",
"on",
"or",
"over",
"people",
"so",
"that",
"the",
"this",
"to",
"up",
"was",
"were",
"which",
"with",
]
COMMON_FRAGMENTS = [
"de",
"th",
"un",
]
WILDCARDS = [
"a" * 100, # letters
"0" * 100, # numbers
" " * 100, # whitespace
]
TESTS = [
*ALPHABET,
*NUMBERS,
*PRONOUNS,
*COMMON_WORDS,
*COMMON_FRAGMENTS,
*WILDCARDS,
" ", # single space
"\n", # newline
]
regex_cache = {} regex_cache = {}