added playername cache

Made period optional
added player cache

should fix user user_id on lb instead of usernames
This commit was merged in pull request #2.
This commit is contained in:
2026-08-12 13:54:09 +02:00
parent fe93b27eb5
commit 2a3a437dc4
3 changed files with 94 additions and 80 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ intents.message_content = True
# Create bot
client = commands.Bot(command_prefix="!", intents=intents)
client.remove_command("help")
client.player_cache = {}
# Bot ready
@client.event
+78 -78
View File
@@ -26,17 +26,19 @@ class Leaderboard(commands.Cog):
self,
interaction: discord.Interaction,
leaderboard_type: str,
period: str
period: str = "alltime"
):
try:
if leaderboard_type == "accuracy":
msg = get_accuracy_leaderboard(
msg = Leaderboard.get_accuracy_leaderboard(
self,
period,
interaction.guild
)
elif leaderboard_type == "perfect":
msg = get_perfect_score_leaderboard(
msg = Leaderboard.get_perfect_score_leaderboard(
self,
period,
interaction.guild
)
@@ -49,71 +51,31 @@ class Leaderboard(commands.Cog):
"Error loading leaderboard."
)
async def setup(bot):
await bot.add_cog(Leaderboard(bot))
def get_accuracy_leaderboard(period, guild):
conn = sqlite3.connect("Goonerdle.db")
cursor = conn.cursor()
cursor.execute("""
SELECT
User_ID,
COUNT(*) AS games_played,
ROUND(
(SUM(Result) * 100.0) /
(COUNT(*) * 10),
2
) AS accuracy
FROM Result
WHERE
? = 'alltime'
OR (
? = 'monthly'
AND DateScore >= date('now', 'start of month')
)
OR (
? = 'yearly'
AND DateScore >= date('now', 'start of year')
)
GROUP BY User_ID
HAVING COUNT(*) >= 5
ORDER BY accuracy DESC
LIMIT 10
""", (period, period, period))
rows = cursor.fetchall()
conn.close()
lines = []
for position, (user_id, games_played, accuracy) in enumerate(rows, start=1):
member = guild.get_member(user_id)
def get_playername(self, user_id : int):
member = self.bot.player_cache.get(user_id)
if member:
name = member.display_name
else:
name = f"User {user_id}"
return name
lines.append(
f"{position}. {name} — **{accuracy:.2f}%** ({games_played} games)"
)
def get_accuracy_leaderboard(self, period, guild):
conn = sqlite3.connect("Goonerdle.db")
cursor = conn.cursor()
return "🏆 **Accuracy Leaderboard** 🏆\n\n" + "\n".join(lines)
def get_perfect_score_leaderboard(period, guild):
conn = sqlite3.connect("Goonerdle.db")
cursor = conn.cursor()
cursor.execute("""
SELECT
User_ID,
COUNT(*) AS perfect_games
FROM Result
WHERE
Result = 10
AND (
cursor.execute("""
SELECT
User_ID,
COUNT(*) AS games_played,
ROUND(
(SUM(Result) * 100.0) /
(COUNT(*) * 10),
2
) AS accuracy
FROM Result
WHERE
? = 'alltime'
OR (
? = 'monthly'
@@ -123,28 +85,66 @@ def get_perfect_score_leaderboard(period, guild):
? = 'yearly'
AND DateScore >= date('now', 'start of year')
)
GROUP BY User_ID
HAVING COUNT(*) >= 5
ORDER BY accuracy DESC
LIMIT 10
""", (period, period, period))
rows = cursor.fetchall()
conn.close()
lines = []
for position, (user_id, games_played, accuracy) in enumerate(rows, start=1):
name = Leaderboard.get_playername(self, user_id)
lines.append(
f"{position}. {name} — **{accuracy:.2f}%** ({games_played} games)"
)
GROUP BY User_ID
ORDER BY perfect_games DESC
LIMIT 10
""", (period, period, period))
rows = cursor.fetchall()
conn.close()
return f"🏆 **Accuracy Leaderboard {period}** 🏆\n\n" + "\n".join(lines)
lines = []
def get_perfect_score_leaderboard(self, period, guild):
conn = sqlite3.connect("Goonerdle.db")
cursor = conn.cursor()
for position, (user_id, perfect_games) in enumerate(rows, start=1):
cursor.execute("""
SELECT
User_ID,
COUNT(*) AS perfect_games
FROM Result
WHERE
Result = 10
AND (
? = 'alltime'
OR (
? = 'monthly'
AND DateScore >= date('now', 'start of month')
)
OR (
? = 'yearly'
AND DateScore >= date('now', 'start of year')
)
)
GROUP BY User_ID
ORDER BY perfect_games DESC
LIMIT 10
""", (period, period, period))
member = guild.get_member(user_id)
rows = cursor.fetchall()
conn.close()
if member:
name = member.display_name
else:
name = f"User {user_id}"
lines = []
lines.append(
f"{position}. {name} — **{perfect_games}** perfect games"
)
for position, (user_id, perfect_games) in enumerate(rows, start=1):
name = Leaderboard.get_playername(self, user_id)
return "🏆 **Perfect Game Leaderboard** 🏆\n\n" + "\n".join(lines)
lines.append(
f"{position}. {name} — **{perfect_games}** perfect games"
)
return f"🏆 **Perfect Game Leaderboard {period}** 🏆\n\n" + "\n".join(lines)
async def setup(bot):
await bot.add_cog(Leaderboard(bot))
+15 -1
View File
@@ -5,6 +5,17 @@ class MessageListener(commands.Cog):
def __init__(self, bot):
self.bot = bot
async def cog_load(self):
guild = self.bot.get_guild(985160153865281596)
if guild:
print("Loading guild members...")
async for member in guild.fetch_members(limit=None):
self.bot.player_cache[member.id] = member
print(f"Loaded {len(self.bot.player_cache)} members")
@commands.Cog.listener()
async def on_message(self, message):
if message.author.bot:
@@ -34,6 +45,9 @@ class MessageListener(commands.Cog):
user_id = message.author.id
# Keep the cached member up to date
self.bot.player_cache[user_id] = message.author
save_result(user_id, result, score_values, date)
print(f"Saved score for user {user_id}")
@@ -42,4 +56,4 @@ class MessageListener(commands.Cog):
print("Parsing error:", e)
async def setup(bot):
await bot.add_cog(MessageListener(bot))
await bot.add_cog(MessageListener(bot))