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:
@@ -11,7 +11,7 @@ intents.message_content = True
|
|||||||
# Create bot
|
# Create bot
|
||||||
client = commands.Bot(command_prefix="!", intents=intents)
|
client = commands.Bot(command_prefix="!", intents=intents)
|
||||||
client.remove_command("help")
|
client.remove_command("help")
|
||||||
|
client.player_cache = {}
|
||||||
|
|
||||||
# Bot ready
|
# Bot ready
|
||||||
@client.event
|
@client.event
|
||||||
|
|||||||
+77
-77
@@ -26,17 +26,19 @@ class Leaderboard(commands.Cog):
|
|||||||
self,
|
self,
|
||||||
interaction: discord.Interaction,
|
interaction: discord.Interaction,
|
||||||
leaderboard_type: str,
|
leaderboard_type: str,
|
||||||
period: str
|
period: str = "alltime"
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
if leaderboard_type == "accuracy":
|
if leaderboard_type == "accuracy":
|
||||||
msg = get_accuracy_leaderboard(
|
msg = Leaderboard.get_accuracy_leaderboard(
|
||||||
|
self,
|
||||||
period,
|
period,
|
||||||
interaction.guild
|
interaction.guild
|
||||||
)
|
)
|
||||||
|
|
||||||
elif leaderboard_type == "perfect":
|
elif leaderboard_type == "perfect":
|
||||||
msg = get_perfect_score_leaderboard(
|
msg = Leaderboard.get_perfect_score_leaderboard(
|
||||||
|
self,
|
||||||
period,
|
period,
|
||||||
interaction.guild
|
interaction.guild
|
||||||
)
|
)
|
||||||
@@ -49,71 +51,31 @@ class Leaderboard(commands.Cog):
|
|||||||
"Error loading leaderboard."
|
"Error loading leaderboard."
|
||||||
)
|
)
|
||||||
|
|
||||||
async def setup(bot):
|
def get_playername(self, user_id : int):
|
||||||
await bot.add_cog(Leaderboard(bot))
|
member = self.bot.player_cache.get(user_id)
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
if member:
|
if member:
|
||||||
name = member.display_name
|
name = member.display_name
|
||||||
else:
|
else:
|
||||||
name = f"User {user_id}"
|
name = f"User {user_id}"
|
||||||
|
|
||||||
lines.append(
|
return name
|
||||||
f"{position}. {name} — **{accuracy:.2f}%** ({games_played} games)"
|
|
||||||
)
|
|
||||||
|
|
||||||
return "🏆 **Accuracy Leaderboard** 🏆\n\n" + "\n".join(lines)
|
def get_accuracy_leaderboard(self, period, guild):
|
||||||
|
conn = sqlite3.connect("Goonerdle.db")
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
def get_perfect_score_leaderboard(period, guild):
|
cursor.execute("""
|
||||||
conn = sqlite3.connect("Goonerdle.db")
|
SELECT
|
||||||
cursor = conn.cursor()
|
User_ID,
|
||||||
|
COUNT(*) AS games_played,
|
||||||
cursor.execute("""
|
ROUND(
|
||||||
SELECT
|
(SUM(Result) * 100.0) /
|
||||||
User_ID,
|
(COUNT(*) * 10),
|
||||||
COUNT(*) AS perfect_games
|
2
|
||||||
FROM Result
|
) AS accuracy
|
||||||
WHERE
|
FROM Result
|
||||||
Result = 10
|
WHERE
|
||||||
AND (
|
|
||||||
? = 'alltime'
|
? = 'alltime'
|
||||||
OR (
|
OR (
|
||||||
? = 'monthly'
|
? = 'monthly'
|
||||||
@@ -123,28 +85,66 @@ def get_perfect_score_leaderboard(period, guild):
|
|||||||
? = 'yearly'
|
? = 'yearly'
|
||||||
AND DateScore >= date('now', 'start of year')
|
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()
|
return f"🏆 **Accuracy Leaderboard {period}** 🏆\n\n" + "\n".join(lines)
|
||||||
conn.close()
|
|
||||||
|
|
||||||
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:
|
lines = []
|
||||||
name = member.display_name
|
|
||||||
else:
|
|
||||||
name = f"User {user_id}"
|
|
||||||
|
|
||||||
lines.append(
|
for position, (user_id, perfect_games) in enumerate(rows, start=1):
|
||||||
f"{position}. {name} — **{perfect_games}** perfect games"
|
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))
|
||||||
@@ -5,6 +5,17 @@ class MessageListener(commands.Cog):
|
|||||||
def __init__(self, bot):
|
def __init__(self, bot):
|
||||||
self.bot = 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()
|
@commands.Cog.listener()
|
||||||
async def on_message(self, message):
|
async def on_message(self, message):
|
||||||
if message.author.bot:
|
if message.author.bot:
|
||||||
@@ -34,6 +45,9 @@ class MessageListener(commands.Cog):
|
|||||||
|
|
||||||
user_id = message.author.id
|
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)
|
save_result(user_id, result, score_values, date)
|
||||||
|
|
||||||
print(f"Saved score for user {user_id}")
|
print(f"Saved score for user {user_id}")
|
||||||
|
|||||||
Reference in New Issue
Block a user