109 lines
3.0 KiB
Python
Executable File
109 lines
3.0 KiB
Python
Executable File
import discord
|
|
import sqlite3
|
|
from discord.ext import commands, tasks
|
|
from datetime import datetime, timezone
|
|
from collections import defaultdict
|
|
|
|
class DailyRecap(commands.Cog):
|
|
def __init__(self, bot):
|
|
self.bot = bot
|
|
self.last_date = None
|
|
self.recap_task.start()
|
|
|
|
def cog_unload(self):
|
|
self.recap_task.cancel()
|
|
|
|
@tasks.loop(minutes=1)
|
|
async def recap_task(self):
|
|
now = datetime.now(timezone.utc)
|
|
|
|
# 00:00 UTC
|
|
if now.hour == 0 and now.minute == 0:
|
|
print("Running daily recap...")
|
|
|
|
thread = self.bot.get_channel(1506738270548267169)
|
|
|
|
if not thread:
|
|
print("Thread not found")
|
|
return
|
|
|
|
conn = sqlite3.connect("Goonerdle.db")
|
|
cursor = conn.cursor()
|
|
|
|
try:
|
|
# Get latest date
|
|
cursor.execute("SELECT MAX(DateScore) FROM Result")
|
|
latest_date = cursor.fetchone()[0]
|
|
|
|
if not latest_date:
|
|
print("No data found")
|
|
return
|
|
|
|
# Prevent duplicate send
|
|
if self.last_date == latest_date:
|
|
return
|
|
self.last_date = latest_date
|
|
|
|
# Get all scores that day
|
|
cursor.execute("""
|
|
SELECT User_ID, Result
|
|
FROM Result
|
|
WHERE DateScore = ?
|
|
""", (latest_date,))
|
|
|
|
rows = cursor.fetchall()
|
|
|
|
if not rows:
|
|
print("No results found")
|
|
return
|
|
|
|
# Group scores (HIGH → LOW)
|
|
scores = defaultdict(list)
|
|
|
|
for user_id, result in rows:
|
|
scores[result].append(user_id)
|
|
|
|
sorted_scores = sorted(scores.keys(), reverse=True)
|
|
|
|
# Build leaderboard
|
|
leaderboard_lines = []
|
|
position = 1
|
|
|
|
for score in sorted_scores:
|
|
users = scores[score]
|
|
|
|
mentions = [f"<@{uid}>" for uid in users]
|
|
|
|
# Score FIRST
|
|
line = f"{position}. **{score}/10** - {', '.join(mentions)}"
|
|
leaderboard_lines.append(line)
|
|
|
|
position += 1
|
|
|
|
leaderboard_text = "\n".join(leaderboard_lines)
|
|
|
|
# Send in thread
|
|
msg = (
|
|
f"**Rule34dle Daily Recap**\n"
|
|
f"{latest_date}\n\n"
|
|
f"{leaderboard_text}"
|
|
)
|
|
|
|
await thread.send(msg)
|
|
|
|
print("Recap sent!")
|
|
|
|
except Exception as e:
|
|
print("Recap error:", e)
|
|
|
|
finally:
|
|
conn.close()
|
|
|
|
@recap_task.before_loop
|
|
async def before_loop(self):
|
|
await self.bot.wait_until_ready()
|
|
|
|
|
|
async def setup(bot):
|
|
await bot.add_cog(DailyRecap(bot))
|