implement plaintext notifications

This commit is contained in:
Infinidoge 2024-12-23 00:49:14 -05:00
parent ec54e31b5c
commit 0df744c946
Signed by: Infinidoge
SSH key fingerprint: SHA256:GT2StvPQMMfFHyiiFJymQxfTG/z6EWLJ6NWItf5K5sA

View file

@ -1,5 +1,6 @@
import logging import logging
from asyncio import TaskGroup from asyncio import TaskGroup
from textwrap import indent
from disnake import Embed from disnake import Embed
from disnake.ext.commands import Cog, group, guild_only from disnake.ext.commands import Cog, group, guild_only
@ -10,7 +11,7 @@ log = logging.getLogger("nomen.notifications")
log.setLevel(logging.INFO) log.setLevel(logging.INFO)
async def handle_notification(db_updates, ctx, message, keyword, user_id): 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
""" """
@ -26,17 +27,19 @@ async def handle_notification(db_updates, ctx, message, keyword, user_id):
log.debug(f"- - Notifying {user_id}") log.debug(f"- - Notifying {user_id}")
db_updates.append((ctx.guild.id, keyword, user_id)) db_updates.append((ctx.guild.id, keyword, user_id))
footer = f"\n\n<t:{int(message.created_at.timestamp())}:R> | [Show]({message.jump_url}) | <#{ctx.channel.id}>" header = f"🔔 `{message.author.display_name}` mentioned `{keyword}` on `{ctx.guild}`:"
footer = f"\n<t:{int(message.created_at.timestamp())}:R> | [Show]({message.jump_url}) | <#{ctx.channel.id}>"
embed = Embed( if use_embed:
description=message.content + footer, embed = Embed(description=message.content + "\n" + footer)
) embed.set_author(
embed.set_author(name=message.author, icon_url=message.author.display_avatar) name=f"{message.author.display_name} ({message.author})",
icon_url=message.author.display_avatar,
)
await member.send( await member.send(header, embed=embed)
f":bell: `{message.author}` mentioned `{keyword}` on `{ctx.guild}`:", else:
embed=embed, await member.send("\n".join((header, indent(message.content, "> ", lambda line: True).strip(), footer)))
)
async def handle_triggers(ctx, message): async def handle_triggers(ctx, message):
@ -88,7 +91,7 @@ async def handle_triggers(ctx, message):
async for keyword, user_id, use_embed in cur: async for keyword, user_id, use_embed in cur:
log.debug(f"- Creating notification task: '{keyword}' for {user_id}") log.debug(f"- Creating notification task: '{keyword}' for {user_id}")
tg.create_task(handle_notification(db_updates, ctx, message, keyword, user_id)) tg.create_task(handle_notification(db_updates, ctx, message, keyword, user_id, use_embed))
await ctx.bot.db.executemany( await ctx.bot.db.executemany(
"UPDATE keywords SET count = count + 1 WHERE guild_id=? AND keyword=? AND user_id=?", "UPDATE keywords SET count = count + 1 WHERE guild_id=? AND keyword=? AND user_id=?",