first commit

This commit is contained in:
2026-06-23 15:04:46 +02:00
committed by Nathan Tien You
commit e1d64997c7
11 changed files with 607 additions and 0 deletions
Binary file not shown.
Binary file not shown.
+108
View File
@@ -0,0 +1,108 @@
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))
+45
View File
@@ -0,0 +1,45 @@
from discord.ext import commands
from database import save_result
class MessageListener(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_message(self, message):
if message.author.bot:
return
content = message.content.strip()
# Only process Rule34dle messages
if not content.startswith("Rule34dle"):
return
lines = content.split("\n")
try:
date_line = lines[0]
date = date_line.split(" ")[2]
result_line = lines[1]
result = int(result_line.split("/")[0])
squares_line = lines[2].strip()
score_values = [
1 if char == "🟩" else 0
for char in squares_line
]
user_id = message.author.id
save_result(user_id, result, score_values, date)
print(f"Saved score for user {user_id}")
except Exception as e:
print("Parsing error:", e)
async def setup(bot):
await bot.add_cog(MessageListener(bot))