refactor keyword adding using exceptions
This commit is contained in:
parent
3d90376535
commit
76521c9d82
1 changed files with 50 additions and 44 deletions
|
@ -22,6 +22,19 @@ class NotifierLogAdapter(logging.LoggerAdapter):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class KeywordError(Exception):
|
||||||
|
def __init__(self, msg=None, dm_msg=None, *args):
|
||||||
|
self.msg = msg
|
||||||
|
self.dm_msg = dm_msg
|
||||||
|
super().__init__(msg, dm_msg, *args)
|
||||||
|
|
||||||
|
async def send(self, ctx):
|
||||||
|
if self.msg:
|
||||||
|
await ctx.send(self.msg + " (check DMs)")
|
||||||
|
if self.dm_msg:
|
||||||
|
await ctx.author.send(self.dm_msg)
|
||||||
|
|
||||||
|
|
||||||
async def handle_notification(db_updates, ctx, message, keyword, user_id, use_embed):
|
async def handle_notification(db_updates, ctx, message, keyword, user_id, use_embed):
|
||||||
"""
|
"""
|
||||||
Async task to dispatch a notification
|
Async task to dispatch a notification
|
||||||
|
@ -158,64 +171,47 @@ class Notifications(Cog):
|
||||||
"""
|
"""
|
||||||
await ctx.send_help(self.keyword)
|
await ctx.send_help(self.keyword)
|
||||||
|
|
||||||
async def _add_keyword(self, ctx, keyword: str, regex: bool):
|
async def _check_conflicts(self, guild_id, user_id, keyword):
|
||||||
log.debug(
|
return await self.bot.db.fetch_unpacked(
|
||||||
f"Adding {'regex' if regex else 'keyword'}: {keyword} of {ctx.author} ({ctx.author.id}) on {ctx.guild} ({ctx.guild.id})"
|
"SELECT keyword FROM keywords WHERE guild_id=? AND user_id=? AND contains(?, keyword, regex)",
|
||||||
|
(guild_id, user_id, keyword),
|
||||||
)
|
)
|
||||||
|
|
||||||
params = {
|
async def _check_redundants(self, guild_id, user_id, keyword, regex):
|
||||||
"keyword": keyword,
|
return await self.bot.db.fetch_unpacked(
|
||||||
"guild_id": ctx.guild.id,
|
"SELECT keyword FROM keywords WHERE guild_id=? AND user_id=? AND contains(keyword, ?, ?)",
|
||||||
"user_id": ctx.author.id,
|
(guild_id, user_id, keyword, regex),
|
||||||
"regex": regex,
|
)
|
||||||
}
|
|
||||||
|
|
||||||
existing = """
|
async def add_keyword(self, guild_id: int, user_id: int, keyword: str, regex: bool, initial_count: int = 0):
|
||||||
SELECT keyword FROM keywords
|
log.debug(
|
||||||
WHERE guild_id=:guild_id
|
f"Adding {'regex' if regex else 'keyword'}: {keyword} of {user_id} on {guild_id} with count {initial_count}"
|
||||||
AND user_id=:user_id
|
)
|
||||||
AND contains(:keyword, keyword, regex)
|
|
||||||
"""
|
|
||||||
redundant = """
|
|
||||||
SELECT keyword FROM keywords
|
|
||||||
WHERE guild_id=:guild_id
|
|
||||||
AND user_id=:user_id
|
|
||||||
AND contains(keyword, :keyword, :regex)
|
|
||||||
"""
|
|
||||||
|
|
||||||
if test_keyword(keyword, regex):
|
if test_keyword(keyword, regex):
|
||||||
log.debug("Keyword too common")
|
log.debug("Keyword too common")
|
||||||
await ctx.send(f"{'Regex' if regex else 'Keyword'} matches a word that is too common")
|
raise KeywordError(f"{'Regex' if regex else 'Keyword'} matches a word that is too common")
|
||||||
return
|
|
||||||
|
|
||||||
conflicts = await ctx.bot.db.fetch_unpacked(existing, params)
|
if conflicts := await self._check_conflicts(guild_id, user_id, keyword):
|
||||||
|
|
||||||
if conflicts:
|
|
||||||
log.debug("Keyword conflicts with existing keyword")
|
log.debug("Keyword conflicts with existing keyword")
|
||||||
await ctx.send(f"Any instance of `{keyword}` would be matched by existing keywords (check DMs)")
|
raise KeywordError(
|
||||||
await ctx.author.send(
|
f"Any instance of `{keyword}` would be matched by existing keywords",
|
||||||
f"Conflicts with keyword `{keyword}`:\n" + "\n".join(f"- `{conflict}`" for conflict in conflicts)
|
f"Conflicts with keyword `{keyword}`:\n" + "\n".join(f"- `{conflict}`" for conflict in conflicts),
|
||||||
)
|
)
|
||||||
return
|
|
||||||
|
|
||||||
conflicts = await ctx.bot.db.fetch_unpacked(redundant, params)
|
if redundants := await self._check_redundants(guild_id, user_id, keyword, regex):
|
||||||
|
|
||||||
if conflicts:
|
|
||||||
log.debug("Keyword renders existing redundant")
|
log.debug("Keyword renders existing redundant")
|
||||||
await ctx.send(f"Adding `{keyword}` will cause existing keywords to never match (check DMs)")
|
raise KeywordError(
|
||||||
await ctx.author.send(
|
f"Adding `{keyword}` will cause existing keywords to never match",
|
||||||
f"Keywords redundant from `{keyword}`:\n" + "\n".join(f" - `{conflict}`" for conflict in conflicts)
|
f"Keywords redundant from `{keyword}`:\n" + "\n".join(f" - `{conflict}`" for conflict in redundants),
|
||||||
)
|
)
|
||||||
return
|
|
||||||
|
|
||||||
log.debug("Keyword valid, adding")
|
log.debug("Keyword valid, adding")
|
||||||
|
|
||||||
await ctx.bot.db.execute(
|
await self.bot.db.execute(
|
||||||
"INSERT INTO keywords (guild_id, keyword, user_id, regex) VALUES (:guild_id, :keyword, :user_id, :regex)",
|
"INSERT INTO keywords VALUES (?, ?, ?, ?, ?)", (guild_id, keyword, user_id, regex, initial_count)
|
||||||
params,
|
|
||||||
)
|
)
|
||||||
await ctx.bot.db.commit()
|
await self.bot.db.commit()
|
||||||
await ctx.send(f"Added `{keyword}` to your list of keywords")
|
|
||||||
|
|
||||||
@keyword.command()
|
@keyword.command()
|
||||||
@guild_only()
|
@guild_only()
|
||||||
|
@ -226,7 +222,12 @@ class Notifications(Cog):
|
||||||
Use quotes for a keyword with spaces!
|
Use quotes for a keyword with spaces!
|
||||||
"""
|
"""
|
||||||
|
|
||||||
await self._add_keyword(ctx, keyword, False)
|
try:
|
||||||
|
await self.add_keyword(ctx.guild.id, ctx.author.id, keyword, False)
|
||||||
|
except KeywordError as e:
|
||||||
|
await e.send(ctx)
|
||||||
|
else:
|
||||||
|
await ctx.send(f"Added `{keyword}` to your list of keywords")
|
||||||
|
|
||||||
@keyword.command()
|
@keyword.command()
|
||||||
@guild_only()
|
@guild_only()
|
||||||
|
@ -238,7 +239,12 @@ class Notifications(Cog):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# TODO: Add regex names to make notifications cleaner
|
# TODO: Add regex names to make notifications cleaner
|
||||||
await self._add_keyword(ctx, keyword, True)
|
try:
|
||||||
|
await self.add_keyword(ctx.guild.id, ctx.author.id, keyword, True)
|
||||||
|
except KeywordError as e:
|
||||||
|
await e.send(ctx)
|
||||||
|
else:
|
||||||
|
await ctx.send(f"Added regex `{keyword}` to your list of keywords")
|
||||||
|
|
||||||
@keyword.command(aliases=["delete", "del"])
|
@keyword.command(aliases=["delete", "del"])
|
||||||
@guild_only()
|
@guild_only()
|
||||||
|
|
Loading…
Reference in a new issue