From 36c8231b67381d9cd3ab06a713f0c45b908bf4f4 Mon Sep 17 00:00:00 2001 From: Legonzaur Date: Tue, 11 Aug 2026 19:44:37 +0200 Subject: [PATCH] fix: add commands --- Commands/leaderboard.py | 85 +++++++++++++++++++++++++++++++++++++++++ Commands/ping.py | 21 ++++++++++ Commands/profile.py | 62 ++++++++++++++++++++++++++++++ Commands/scan.py | 67 ++++++++++++++++++++++++++++++++ 4 files changed, 235 insertions(+) create mode 100755 Commands/leaderboard.py create mode 100755 Commands/ping.py create mode 100755 Commands/profile.py create mode 100755 Commands/scan.py diff --git a/Commands/leaderboard.py b/Commands/leaderboard.py new file mode 100755 index 0000000..bc92220 --- /dev/null +++ b/Commands/leaderboard.py @@ -0,0 +1,85 @@ +import discord +from discord.ext import commands +from discord import app_commands +import sqlite3 + +class Leaderboard(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @app_commands.command( + name="leaderboard", + description="Best players by accuracy" + ) + async def leaderboard(self, interaction: discord.Interaction): + conn = sqlite3.connect("Goonerdle.db") + cursor = conn.cursor() + + try: + cursor.execute(""" + SELECT + User_ID, + COUNT(*) as games_played, + SUM(Result) as total_correct + FROM Result + GROUP BY User_ID + """) + + rows = cursor.fetchall() + + if not rows: + await interaction.response.send_message("No data available.") + return + + leaderboard_data = [] + + # Calculate accuracy + for user_id, games_played, total_correct in rows: + total_possible = games_played * 10 + accuracy = (total_correct / total_possible) * 100 + + leaderboard_data.append((user_id, accuracy, games_played)) + + # Sort by accuracy descending + leaderboard_data.sort(key=lambda x: x[1], reverse=True) + + # Build leaderboard text + lines = [] + position = 1 + + for user_id, accuracy, games_played in leaderboard_data[:10]: + # Try cache first + user = self.bot.get_user(user_id) + + # Fetch if not cached + if user is None: + try: + user = await self.bot.fetch_user(user_id) + except e: + user = None + + # NO PINGS → use display name or username + if user: + name = user.display_name if hasattr(user, "display_name") else user.name + else: + name = f"User {user_id}" + + line = f"{position}. {name} — **{accuracy:.2f}%** ({games_played} games)" + lines.append(line) + + position += 1 + + msg = "🏆 **Accuracy Leaderboard** 🏆\n\n" + "\n".join(lines) + + await interaction.response.send_message(msg) + + except Exception as e: + print("Leaderboard error:", e) + await interaction.response.send_message("Error loading leaderboard.") + + finally: + conn.close() + + +async def setup(bot): + await bot.add_cog(Leaderboard(bot)) diff --git a/Commands/ping.py b/Commands/ping.py new file mode 100755 index 0000000..2e43c82 --- /dev/null +++ b/Commands/ping.py @@ -0,0 +1,21 @@ +import discord +from discord.ext import commands +from discord import app_commands + +class Ping(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @commands.Cog.listener() + async def on_ready(self): + print("✅ Ping command loaded") + + # ✅ Slash command + @app_commands.command(name="ping", description="Shows bot latency") + async def ping(self, interaction: discord.Interaction): + latency = round(self.bot.latency * 1000) + await interaction.response.send_message(f"Pong! {latency}ms") + + +async def setup(bot): + await bot.add_cog(Ping(bot)) \ No newline at end of file diff --git a/Commands/profile.py b/Commands/profile.py new file mode 100755 index 0000000..ae6b04e --- /dev/null +++ b/Commands/profile.py @@ -0,0 +1,62 @@ +import discord +from discord.ext import commands +from discord import app_commands +import sqlite3 + +class Profile(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @app_commands.command(name="profile", description="View your Rule34dle stats") + async def profile(self, interaction: discord.Interaction, user: discord.User = None): + if user is None: + user = interaction.user + + user_id = user.id + + conn = sqlite3.connect("Goonerdle.db") + cursor = conn.cursor() + + try: + cursor.execute(""" + SELECT COUNT(*), COALESCE(SUM(Result), 0) + FROM Result + WHERE User_ID = ? + """, (user_id,)) + + row = cursor.fetchone() + + times_played = row[0] + total_correct = row[1] + + if times_played == 0: + await interaction.response.send_message( + f"{user.mention} has not played yet!" + ) + return + + total_possible = times_played * 10 + total_incorrect = total_possible - total_correct + + percentage = (total_correct / total_possible) * 100 + + msg = ( + f"**{user.name}'s Profile**\n\n" + f"Games Played: **{times_played}**\n" + f"Correct: **{total_correct}**\n" + f"Incorrect: **{total_incorrect}**\n" + f"Accuracy: **{percentage:.2f}%**" + ) + + await interaction.response.send_message(msg) + + except Exception as e: + print("Profile error:", e) + await interaction.response.send_message("Error fetching profile.") + + finally: + conn.close() + + +async def setup(bot): + await bot.add_cog(Profile(bot)) diff --git a/Commands/scan.py b/Commands/scan.py new file mode 100755 index 0000000..9660ad0 --- /dev/null +++ b/Commands/scan.py @@ -0,0 +1,67 @@ +import discord +from discord.ext import commands +from database import save_result + +class Scan(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @commands.command() + async def scan(self, ctx, limit: int = 4500): + """ + Scan previous messages in the channel for Rule34dle posts + """ + + found = 0 + saved = 0 + + async for message in ctx.channel.history(limit=limit): + if message.author.bot: + continue + + content = message.content.strip() + + # Only process Rule34dle messages + if not content.startswith("Rule34dle"): + continue + + found += 1 + + try: + lines = content.split("\n") + + # Ensure correct format + if len(lines) < 3: + continue + + # Extract date from first line + # Example: "Rule34dle Daily 2026-06-12" + date_line = lines[0] + date = date_line.split(" ")[2] + + # Extract result + result = int(lines[1].split("/")[0]) + + # Extract squares + squares_line = lines[2].strip() + + # Validate squares + if not all(c in ["🟩", "🟥"] for c in squares_line): + continue + + # Convert to 0/1 + score_values = [1 if c == "🟩" else 0 for c in squares_line] + + user_id = message.author.id + + save_result(user_id, result, score_values, date) + saved += 1 + + except Exception as e: + print("Scan parse error:", e) + + await ctx.send(f"Scan complete!\nFound: {found}\nSaved: {saved}") + + +async def setup(bot): + await bot.add_cog(Scan(bot))