sparkle stats works now yippee

This commit is contained in:
Lilac-Rose
2025-12-01 20:21:39 +01:00
parent 1acfa10b5f
commit 08474dd7ad

View File

@@ -159,41 +159,62 @@ class SparkleCommands(commands.Cog):
def db_task():
conn = get_db()
# Get all sparkles from sparkles table (historical totals)
cursor = conn.execute(
"""SELECT sparkle_type, timestamp FROM sparkle_events
"""SELECT COALESCE(SUM(epic), 0), COALESCE(SUM(rare), 0), COALESCE(SUM(regular), 0)
FROM sparkles
WHERE server_id = ?""",
(str(interaction.guild.id),)
)
total_epic, total_rare, total_regular = cursor.fetchone()
# Get sparkle events (for timing data)
cursor = conn.execute(
"""SELECT sparkle_type, timestamp, message_id FROM sparkle_events
WHERE server_id = ?
ORDER BY timestamp ASC""",
(str(interaction.guild.id),)
)
rows = cursor.fetchall()
events = cursor.fetchall()
conn.close()
return rows
# Get total message count from stats.db (located in ../stats/)
import os
import sqlite3
stats_db_path = os.path.join(os.path.dirname(__file__), "..", "stats", "stats.db")
total_messages = 0
try:
stats_conn = sqlite3.connect(stats_db_path)
stats_cursor = stats_conn.execute(
"SELECT SUM(message_count) FROM message_stats"
)
result = stats_cursor.fetchone()
if result and result[0]:
total_messages = result[0]
stats_conn.close()
except:
pass # If stats.db doesn't exist or has issues, just use 0
return total_epic, total_rare, total_regular, events, total_messages
rows = await asyncio.to_thread(db_task)
total_epic, total_rare, total_regular, events, total_messages = await asyncio.to_thread(db_task)
if not rows:
# Calculate actual totals from sparkles table (for display)
epic = total_epic
rare = total_rare
regular = total_regular
total = epic + rare + regular
# Count sparkles from events (for average calculation)
event_count = len(events)
if total == 0:
return await interaction.followup.send(
"This server has **no sparkles yet!** ✨",
ephemeral=True
)
total = len(rows)
epic = sum(1 for r in rows if r[0] == "epic")
rare = sum(1 for r in rows if r[0] == "rare")
regular = sum(1 for r in rows if r[0] == "regular")
timestamps = [r[1] for r in rows]
deltas = [
timestamps[i+1] - timestamps[i]
for i in range(len(timestamps) - 1)
]
avg_time = sum(deltas) / len(deltas) if deltas else 0
last_sparkle = timestamps[-1]
def humanize(seconds):
return str(datetime.timedelta(seconds=int(seconds)))
embed = discord.Embed(
title=f"📊 Sparkle Stats for {interaction.guild.name}",
color=discord.Color.purple()
@@ -210,20 +231,45 @@ class SparkleCommands(commands.Cog):
inline=False
)
embed.add_field(
name="Timing",
value=(
f"**Average time between sparkles:** {humanize(avg_time)}\n"
f"**Last sparkle:** "
f"<t:{int(last_sparkle)}:R>"
),
inline=False
)
# Calculate average messages per sparkle using real message count
# Only use sparkles from events (since old sparkles have no corresponding message data)
if total_messages > 0 and event_count > 0:
avg_messages_per_sparkle = total_messages / event_count
embed.add_field(
name="Message Statistics",
value=f"**Average messages per sparkle:** ~{int(avg_messages_per_sparkle):,}",
inline=False
)
embed.set_footer(text="Sparkle stats are calculated from logged sparkle events.")
# Only show timing data if we have events
if events:
timestamps = [e[1] for e in events]
# Calculate time deltas
deltas = [
timestamps[i+1] - timestamps[i]
for i in range(len(timestamps) - 1)
]
avg_time = sum(deltas) / len(deltas) if deltas else 0
last_sparkle = timestamps[-1]
def humanize(seconds):
return str(datetime.timedelta(seconds=int(seconds)))
embed.add_field(
name="Timing",
value=(
f"**Average time between sparkles:** {humanize(avg_time)}\n"
f"**Last sparkle:** <t:{int(last_sparkle)}:R>"
),
inline=False
)
embed.set_footer(text="Message statistics are server-wide. Timing data from logged events.")
else:
embed.set_footer(text="All sparkles are from before event logging was added.")
await interaction.followup.send(embed=embed)
async def setup(bot):
await bot.add_cog(SparkleCommands(bot))