Updated moments to use a database.
This commit is contained in:
parent
03f6337e27
commit
c4962f2d09
1
.gitignore
vendored
1
.gitignore
vendored
@ -172,3 +172,4 @@ cython_debug/
|
|||||||
|
|
||||||
# PyPI configuration file
|
# PyPI configuration file
|
||||||
.pypirc
|
.pypirc
|
||||||
|
/data
|
@ -1,16 +1,14 @@
|
|||||||
import discord
|
import discord
|
||||||
from discord import app_commands
|
from discord import app_commands
|
||||||
from typing import Optional, List
|
from typing import Optional
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import logging
|
import logging
|
||||||
|
from utils.database import Database
|
||||||
|
|
||||||
# Temporary storage for moments (consider database in future)
|
db = Database()
|
||||||
saved_moments = []
|
|
||||||
incident_logs = {}
|
|
||||||
|
|
||||||
|
|
||||||
async def setup(client):
|
async def setup(client):
|
||||||
# Create command group
|
|
||||||
moments_group = app_commands.Group(
|
moments_group = app_commands.Group(
|
||||||
name="moments",
|
name="moments",
|
||||||
description="Capture and manage important moments"
|
description="Capture and manage important moments"
|
||||||
@ -30,9 +28,7 @@ async def setup(client):
|
|||||||
message_link: str,
|
message_link: str,
|
||||||
description: Optional[str] = None
|
description: Optional[str] = None
|
||||||
):
|
):
|
||||||
"""Capture a funny moment with reaction tracking"""
|
|
||||||
try:
|
try:
|
||||||
# Parse message ID from link
|
|
||||||
parts = message_link.split("/")
|
parts = message_link.split("/")
|
||||||
channel_id = int(parts[-2])
|
channel_id = int(parts[-2])
|
||||||
message_id = int(parts[-1])
|
message_id = int(parts[-1])
|
||||||
@ -40,20 +36,18 @@ async def setup(client):
|
|||||||
channel = interaction.guild.get_channel(channel_id)
|
channel = interaction.guild.get_channel(channel_id)
|
||||||
message = await channel.fetch_message(message_id)
|
message = await channel.fetch_message(message_id)
|
||||||
|
|
||||||
# Add tracking reactions
|
|
||||||
await message.add_reaction("😂")
|
await message.add_reaction("😂")
|
||||||
await message.add_reaction("🎉")
|
await message.add_reaction("🎉")
|
||||||
|
|
||||||
# Store moment
|
# Store in database
|
||||||
saved_moments.append({
|
record_id = db.add_funny_moment(
|
||||||
"message": message.content,
|
message_link=message_link,
|
||||||
"author": message.author.id,
|
author_id=message.author.id,
|
||||||
"description": description,
|
description=description
|
||||||
"timestamp": datetime.now().isoformat()
|
)
|
||||||
})
|
|
||||||
|
|
||||||
await interaction.response.send_message(
|
await interaction.response.send_message(
|
||||||
"✅ Funny moment registered! Reactions added!",
|
f"✅ Funny moment registered! (ID: {record_id})",
|
||||||
ephemeral=True
|
ephemeral=True
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -64,7 +58,7 @@ async def setup(client):
|
|||||||
ephemeral=True
|
ephemeral=True
|
||||||
)
|
)
|
||||||
|
|
||||||
# Incident tracking command
|
# Incident command
|
||||||
@moments_group.command(
|
@moments_group.command(
|
||||||
name="incident",
|
name="incident",
|
||||||
description="Log an incident with recent messages"
|
description="Log an incident with recent messages"
|
||||||
@ -79,29 +73,33 @@ async def setup(client):
|
|||||||
message_count: app_commands.Range[int, 1, 50],
|
message_count: app_commands.Range[int, 1, 50],
|
||||||
reason: str
|
reason: str
|
||||||
):
|
):
|
||||||
"""Log messages for moderator review"""
|
|
||||||
try:
|
try:
|
||||||
# Fetch messages
|
|
||||||
messages = [
|
messages = [
|
||||||
msg async for msg in interaction.channel.history(limit=message_count)
|
msg async for msg in interaction.channel.history(limit=message_count)
|
||||||
][::-1] # Reverse to get chronological order
|
][::-1]
|
||||||
|
|
||||||
# Format log
|
# Prepare messages for storage
|
||||||
log_content = "\n".join(
|
formatted_messages = [{
|
||||||
f"{msg.author.display_name} ({msg.author.id}) | {msg.created_at}:\n"
|
"id": msg.id,
|
||||||
f"{msg.content}\n{'-'*40}"
|
"author_id": msg.author.id,
|
||||||
for msg in messages
|
"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
|
if not success:
|
||||||
incident_id = f"incident_{datetime.now().timestamp()}"
|
raise Exception("Database storage failed")
|
||||||
incident_logs[incident_id] = {
|
|
||||||
"reason": reason,
|
|
||||||
"messages": log_content,
|
|
||||||
"moderator": interaction.user.id
|
|
||||||
}
|
|
||||||
|
|
||||||
# Send confirmation
|
|
||||||
await interaction.response.send_message(
|
await interaction.response.send_message(
|
||||||
f"✅ Incident logged with ID `{incident_id}`\n"
|
f"✅ Incident logged with ID `{incident_id}`\n"
|
||||||
f"**Reason:** {reason}\n"
|
f"**Reason:** {reason}\n"
|
||||||
@ -112,9 +110,60 @@ async def setup(client):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Incident log error: {e}")
|
logging.error(f"Incident log error: {e}")
|
||||||
await interaction.response.send_message(
|
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
|
ephemeral=True
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add commands to client
|
|
||||||
client.tree.add_command(moments_group)
|
client.tree.add_command(moments_group)
|
||||||
|
121
utils/database.py
Normal file
121
utils/database.py
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
import sqlite3
|
||||||
|
import logging
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import List, Dict, Optional
|
||||||
|
|
||||||
|
|
||||||
|
class Database:
|
||||||
|
def __init__(self, db_path: str = "data/moments.db"):
|
||||||
|
self.db_path = db_path
|
||||||
|
self._init_db()
|
||||||
|
|
||||||
|
def _init_db(self):
|
||||||
|
"""Initialize database tables"""
|
||||||
|
with self._get_connection() as conn:
|
||||||
|
conn.execute("""
|
||||||
|
CREATE TABLE IF NOT EXISTS funny_moments (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
message_link TEXT NOT NULL,
|
||||||
|
description TEXT,
|
||||||
|
author_id INTEGER NOT NULL,
|
||||||
|
timestamp DATETIME NOT NULL
|
||||||
|
)
|
||||||
|
""")
|
||||||
|
|
||||||
|
conn.execute("""
|
||||||
|
CREATE TABLE IF NOT EXISTS incidents (
|
||||||
|
id TEXT PRIMARY KEY,
|
||||||
|
reason TEXT NOT NULL,
|
||||||
|
moderator_id INTEGER NOT NULL,
|
||||||
|
timestamp DATETIME NOT NULL
|
||||||
|
)
|
||||||
|
""")
|
||||||
|
|
||||||
|
conn.execute("""
|
||||||
|
CREATE TABLE IF NOT EXISTS incident_messages (
|
||||||
|
incident_id TEXT,
|
||||||
|
message_id INTEGER,
|
||||||
|
author_id INTEGER,
|
||||||
|
content TEXT,
|
||||||
|
timestamp DATETIME,
|
||||||
|
PRIMARY KEY (incident_id, message_id),
|
||||||
|
FOREIGN KEY (incident_id) REFERENCES incidents(id)
|
||||||
|
)
|
||||||
|
""")
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
def _get_connection(self):
|
||||||
|
return sqlite3.connect(self.db_path)
|
||||||
|
|
||||||
|
def add_funny_moment(self, message_link: str, author_id: int, description: str = None) -> int:
|
||||||
|
"""Store a funny moment in database"""
|
||||||
|
with self._get_connection() as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute("""
|
||||||
|
INSERT INTO funny_moments
|
||||||
|
(message_link, description, author_id, timestamp)
|
||||||
|
VALUES (?, ?, ?, ?)
|
||||||
|
""", (message_link, description, author_id, datetime.now()))
|
||||||
|
conn.commit()
|
||||||
|
return cursor.lastrowid
|
||||||
|
|
||||||
|
def add_incident(self, incident_id: str, reason: str, moderator_id: int, messages: List[Dict]) -> bool:
|
||||||
|
"""Store an incident with related messages"""
|
||||||
|
try:
|
||||||
|
with self._get_connection() as conn:
|
||||||
|
# Add incident record
|
||||||
|
conn.execute("""
|
||||||
|
INSERT INTO incidents
|
||||||
|
(id, reason, moderator_id, timestamp)
|
||||||
|
VALUES (?, ?, ?, ?)
|
||||||
|
""", (incident_id, reason, moderator_id, datetime.now()))
|
||||||
|
|
||||||
|
# Add incident messages
|
||||||
|
for msg in messages:
|
||||||
|
conn.execute("""
|
||||||
|
INSERT INTO incident_messages
|
||||||
|
(incident_id, message_id, author_id, content, timestamp)
|
||||||
|
VALUES (?, ?, ?, ?, ?)
|
||||||
|
""", (
|
||||||
|
incident_id,
|
||||||
|
msg['id'],
|
||||||
|
msg['author_id'],
|
||||||
|
msg['content'],
|
||||||
|
msg['timestamp']
|
||||||
|
))
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Failed to save incident: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
def get_incident(self, incident_id: str) -> Optional[Dict]:
|
||||||
|
"""Retrieve an incident with its messages"""
|
||||||
|
with self._get_connection() as conn:
|
||||||
|
conn.row_factory = sqlite3.Row
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
# Get incident details
|
||||||
|
cursor.execute("""
|
||||||
|
SELECT * FROM incidents
|
||||||
|
WHERE id = ?
|
||||||
|
""", (incident_id,))
|
||||||
|
incident = cursor.fetchone()
|
||||||
|
|
||||||
|
if not incident:
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Get related messages
|
||||||
|
cursor.execute("""
|
||||||
|
SELECT * FROM incident_messages
|
||||||
|
WHERE incident_id = ?
|
||||||
|
ORDER BY timestamp ASC
|
||||||
|
""", (incident_id,))
|
||||||
|
messages = cursor.fetchall()
|
||||||
|
|
||||||
|
return {
|
||||||
|
"details": dict(incident),
|
||||||
|
"messages": [dict(msg) for msg in messages]
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user