Skip to content

Commit 4df85e8

Browse files
committed
Merge branch 'v0.1.2'
Release v0.1.2 with a leaderboard command patch
2 parents 1b3692f + 80bb4ac commit 4df85e8

File tree

5 files changed

+63
-40
lines changed

5 files changed

+63
-40
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ Prefix: `=`
129129
| `=fetch` | Yes | `contest_name year [contest id, optional] problem_number` | Fetches a problem from the specified path |
130130
| `=last5` | Yes | `contest_name` | Returns the last 5 questions from a specified contest |
131131
| `=random` | No | | Returns a random problem |
132+
| `=leaderboard` | Yes | `amount of users` | Shows the leaderboard for the server with a specified amount of users. If it is not given, the default is 10. |
132133

133134
<u>Miscellaneous</u>
134135
<br>

bot/src/cogs/math/leaderboard.py

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import discord
22
from discord.ext import commands
33
from ..utility.db import (
4-
get_user_db,
4+
get_guild_db,
55
)
66

77
# leaderboard help
88
leaderboard_help = {
99
"name": "=leaderboard help info",
1010
"description_name": "Description",
11-
"description": "View your server's leaderboard and see who has the most points. Retrieves the top 10 users.",
11+
"description": "View your server's leaderboard and see who has the most points. Retrieves the top 10 users by default.",
1212
"usage_name": "Usage",
13-
"usage_description": "`=leaderboard`",
13+
"usage_description": "`=leaderboard <number of users>`",
1414
"alias_name": "Aliases",
1515
"alias_description": "`rank`, `lb`",
1616
"usage_syntax_name": "Usage syntax",
@@ -29,51 +29,56 @@ async def on_ready(self):
2929
print("Leaderboard cog has been loaded sucessfully")
3030

3131
@commands.command(aliases=["rank", "lb"])
32-
async def leaderboard(self, ctx):
32+
async def leaderboard(self, ctx, top_users=None):
33+
34+
if top_users is None:
35+
top_users = 10
36+
else:
37+
pass
3338

3439
user_guild_id = ctx.guild.id
40+
users = await get_guild_db(user_guild_id)
3541

36-
leaderboard = []
42+
leaderboard_total = []
43+
leaderboard = {}
3744

38-
for user in ctx.guild.members:
39-
score = await get_user_db(user_guild_id, user.id)
40-
points = score.get("total_weighted_points")
41-
if points is None:
42-
points = 0
43-
leaderboard.append((user.name, points))
44-
else:
45-
leaderboard.append((user.name, points))
45+
for user in users:
46+
user_dict = user.to_dict()
47+
user_name_id = user.id
48+
total = user_dict.get("total_weighted_points")
49+
if total is None or total == 0:
50+
total = 0
51+
leaderboard[total] = user_name_id
52+
print(leaderboard[total])
53+
leaderboard_total.append(total)
4654

47-
sorted_leaderboard = sorted(
48-
leaderboard, key=lambda x: (x[1], x[1]), reverse=True
49-
)
55+
leaderboard_total = sorted(leaderboard_total, reverse=True)
56+
print("LEADERBOARD USERS:" + str(leaderboard_total))
5057

51-
sorted_names = [i[0] for i in sorted_leaderboard]
52-
sorted_scores = [i[1] for i in sorted_leaderboard]
53-
sorted_amount = len([i[1] for i in sorted_leaderboard])
54-
55-
description_string = ""
56-
if sorted_amount > 10:
57-
for i in range(10):
58-
description_string += (
59-
f"**{sorted_names[i]}** - `{sorted_scores[i]}` points\n"
60-
)
61-
elif sorted_amount <= 10:
62-
for i in range(sorted_amount):
63-
description_string += (
64-
f"**{sorted_names[i]}** - `{sorted_scores[i]}` points\n"
65-
)
66-
67-
# embed
6858
leaderboard_embed = discord.Embed(
6959
title=f"Leaderboard for guild: `{ctx.guild.name}`",
60+
description=f"Showing the top {top_users} users",
7061
color=0xA4D0DA,
71-
description=description_string,
72-
)
73-
leaderboard_embed.set_footer(
74-
text="Such scores are not indicative of a user's skill level or aptitude in mathematics or logical reasoning."
7562
)
76-
leaderboard_embed.set_thumbnail(url=ctx.guild.icon_url)
63+
64+
index = 1
65+
66+
for score in leaderboard_total:
67+
name_id = leaderboard[score]
68+
print("USER ID:" + str(name_id))
69+
user_object = await self.bot.fetch_user(name_id)
70+
user_name = user_object.name
71+
print(user_name)
72+
if score == 0:
73+
break
74+
leaderboard_embed.add_field(
75+
name=f"{index}. {user_name}", value=f"`{score}` points", inline=False
76+
)
77+
if index == top_users:
78+
break
79+
else:
80+
index += 1
81+
7782
await ctx.send(embed=leaderboard_embed)
7883

7984

bot/src/cogs/math/stats.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import discord
44
from discord.ext import commands
5-
from firebase_admin import firestore
65

76
from ..math.contest_problems import amc10_weight, amc12_weight
87
from ..utility.db import (
@@ -72,7 +71,7 @@ def parse_user(raw_user):
7271
user_collection_ref = db.collection(str(user_guild_id)).document(str(user_id))
7372

7473
user_collection_ref.update(
75-
{total_weighted_points_string: firestore.Increment(total_weighted_points)}
74+
{total_weighted_points_string: total_weighted_points}
7675
)
7776

7877
user_object = self.bot.get_user(user_id)

bot/src/cogs/utility/db.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,12 @@ async def get_user_db(guild_id, user_id):
131131
(db.collection(str(guild_id)).document(str(user_id))).get()
132132
).to_dict()
133133
return new_user_db_dict
134+
135+
136+
async def get_guild_db(guild_id):
137+
guild_collection = ""
138+
try:
139+
guild_collection = db.collection(str(guild_id)).get()
140+
return guild_collection
141+
except Exception:
142+
pass

bot/src/main.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import discord
33
from discord.ext import commands
4+
from discord.ext.commands.core import command
45
from dotenv import load_dotenv
56

67
intents = discord.Intents.default()
@@ -75,5 +76,13 @@ async def unloadcog(ctx, cogname=None):
7576
async def hello(ctx):
7677
await ctx.send("Hello, World!")
7778

79+
@bot.command()
80+
@commands.is_owner()
81+
async def announcement(ctx, *, announcement_message):
82+
for guild in bot.guilds:
83+
for channel in guild.channels:
84+
if isinstance(channel, discord.TextChannel):
85+
await channel.send(announcement_message)
86+
await ctx.send("It was successful.")
7887

7988
bot.run(token)

0 commit comments

Comments
 (0)