chorus/bot/chorus_bot/cogs/thread_ingest.py

43 lines
1.5 KiB
Python

import discord
from discord.ext import commands
from chorus_bot.ping import parse_ping_embed
class ThreadIngestCog(commands.Cog):
"""Opening a thread on an #inbox ping is what creates the real triage row."""
def __init__(self, bot: commands.Bot, config, backend_client):
self.bot = bot
self.config = config
self.backend_client = backend_client
@commands.Cog.listener()
async def on_thread_create(self, thread: discord.Thread):
if thread.parent_id != self.config.inbox_channel_id:
return
try:
parent_message = await thread.parent.fetch_message(thread.id)
except discord.NotFound:
# Standalone thread not started from a message -- nothing to ingest.
return
if not parent_message.embeds:
return
parsed = parse_ping_embed(parent_message.embeds[0])
item = await self.backend_client.create_item(
modality=parsed["modality"],
raw_content=parsed["raw_content"],
captured_at=parsed["captured_at"],
discord_message_id=str(thread.id),
sender_id=parsed["sender_id"],
direction=parsed["direction"],
)
item_url = f"{self.config.web_base_url}/?item={item['id']}"
await thread.send(
f"Logged in Chorus - {item_url} to fill in details and track this one."
)
async def setup(bot: commands.Bot, config, backend_client):
await bot.add_cog(ThreadIngestCog(bot, config, backend_client))