Updated moments to use a database.

This commit is contained in:
Dan
2025-01-27 22:30:46 -05:00
parent 03f6337e27
commit c4962f2d09
3 changed files with 206 additions and 35 deletions

View File

@ -1,16 +1,14 @@
import discord
from discord import app_commands
from typing import Optional, List
from typing import Optional
from datetime import datetime
import logging
from utils.database import Database
# Temporary storage for moments (consider database in future)
saved_moments = []
incident_logs = {}
db = Database()
async def setup(client):
# Create command group
moments_group = app_commands.Group(
name="moments",
description="Capture and manage important moments"
@ -30,9 +28,7 @@ async def setup(client):
message_link: str,
description: Optional[str] = None
):
"""Capture a funny moment with reaction tracking"""
try:
# Parse message ID from link
parts = message_link.split("/")
channel_id = int(parts[-2])
message_id = int(parts[-1])
@ -40,20 +36,18 @@ async def setup(client):
channel = interaction.guild.get_channel(channel_id)
message = await channel.fetch_message(message_id)
# Add tracking reactions
await message.add_reaction("😂")
await message.add_reaction("🎉")
# Store moment
saved_moments.append({
"message": message.content,
"author": message.author.id,
"description": description,
"timestamp": datetime.now().isoformat()
})
# Store in database
record_id = db.add_funny_moment(
message_link=message_link,
author_id=message.author.id,
description=description
)
await interaction.response.send_message(
"✅ Funny moment registered! Reactions added!",
f"✅ Funny moment registered! (ID: {record_id})",
ephemeral=True
)
@ -64,7 +58,7 @@ async def setup(client):
ephemeral=True
)
# Incident tracking command
# Incident command
@moments_group.command(
name="incident",
description="Log an incident with recent messages"
@ -79,29 +73,33 @@ async def setup(client):
message_count: app_commands.Range[int, 1, 50],
reason: str
):
"""Log messages for moderator review"""
try:
# Fetch messages
messages = [
msg async for msg in interaction.channel.history(limit=message_count)
][::-1] # Reverse to get chronological order
][::-1]
# Format log
log_content = "\n".join(
f"{msg.author.display_name} ({msg.author.id}) | {msg.created_at}:\n"
f"{msg.content}\n{'-'*40}"
for msg in messages
# Prepare messages for storage
formatted_messages = [{
"id": msg.id,
"author_id": msg.author.id,
"content": msg.content,
"timestamp": msg.created_at
} for msg in messages]
# Generate incident ID
incident_id = f"incident_{int(datetime.now().timestamp())}"
# Store in database
success = db.add_incident(
incident_id=incident_id,
reason=reason,
moderator_id=interaction.user.id,
messages=formatted_messages
)
# Store log
incident_id = f"incident_{datetime.now().timestamp()}"
incident_logs[incident_id] = {
"reason": reason,
"messages": log_content,
"moderator": interaction.user.id
}
if not success:
raise Exception("Database storage failed")
# Send confirmation
await interaction.response.send_message(
f"✅ Incident logged with ID `{incident_id}`\n"
f"**Reason:** {reason}\n"
@ -112,9 +110,60 @@ async def setup(client):
except Exception as e:
logging.error(f"Incident log error: {e}")
await interaction.response.send_message(
"❌ Failed to log incident. Check permissions!",
"❌ Failed to log incident. Check permissions and message count!",
ephemeral=True
)
# Add review command
@moments_group.command(
name="review",
description="Review a logged incident"
)
@app_commands.describe(
incident_id="The incident ID to review"
)
@app_commands.checks.has_permissions(manage_messages=True)
async def review_incident(
interaction: discord.Interaction,
incident_id: str
):
try:
incident = db.get_incident(incident_id)
if not incident:
await interaction.response.send_message(
"❌ Incident not found",
ephemeral=True
)
return
# Format response
messages = "\n\n".join(
f"**{msg['timestamp']}** <@{msg['author_id']}>:\n"
f"{msg['content']}"
for msg in incident['messages']
)
embed = discord.Embed(
title=f"Incident {incident_id}",
description=f"**Reason:** {incident['details']['reason']}",
color=0xff0000
)
embed.add_field(
name="Messages",
value=messages[:1020] + "..." if len(messages) > 1024 else messages,
inline=False
)
await interaction.response.send_message(
embed=embed,
ephemeral=True
)
except Exception as e:
logging.error(f"Incident review error: {e}")
await interaction.response.send_message(
"❌ Failed to retrieve incident",
ephemeral=True
)
# Add commands to client
client.tree.add_command(moments_group)