Skip to content

Commit

Permalink
Optimized leaderboard cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
Ambratolm authored and Ambratolm committed Feb 25, 2025
1 parent da80a29 commit 7f1ed29
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ jobs:
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_SSH_KEY }}
script: |
pkill -f "uv"
cd /home/ubuntu/TACT/
sudo -u ubuntu kill -9 $(pgrep -o -f "uv") || true
sleep 3
git pull fork ft-rework
sleep 3
nohup uv run task app-prod &
47 changes: 41 additions & 6 deletions bot/cogs/board.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import asyncio

from discord import Color, Embed, Interaction, Member, User, app_commands
from discord.app_commands import command, guild_only
from discord.ext.commands import Cog
Expand Down Expand Up @@ -57,6 +59,12 @@ async def profile(
@app_commands.guild_only()
@app_commands.command(description="View leaderboard")
async def leaderboard(self, interaction: Interaction):
# Check guild (not needed but just for type-checking)
guild = interaction.guild
if not guild:
return

# Get top actors
await interaction.response.defer()
db = self.bot.get_db(interaction.guild)
actors = (
Expand All @@ -77,14 +85,41 @@ async def leaderboard(self, interaction: Interaction):
embed=EmbedX.info("", "No members found for the leaderboard.")
)
return

# Get associated members
members = []
for actor in actors:
member = guild.get_member(actor.id) # Try get from cache
if member:
members.append(member)
else:
members.append(None) # placeholder to fetch later concurrently

# Fetch missing members concurrently
members_futures = [
guild.fetch_member(actor.id)
for i, actor in enumerate(actors)
if members[i] is None
]
try:
fetched_members = await asyncio.gather(*members_futures)
except Exception as e:
await interaction.followup.send(
embed=EmbedX.error(description="Could not fetch members.")
)
return

# Update members list with fetched members
fetched_index = 0
for i, actor in enumerate(actors):
if members[i] is None:
members[i] = fetched_members[fetched_index]
fetched_index += 1

# Create embed
embed = Embed(title="🏆 Leaderboard", color=Color.blue())
for i, actor in enumerate(actors):
guild = interaction.guild
member = (
(guild.get_member(actor.id) or await guild.fetch_member(actor.id))
if guild
else None
)
member = members[i]
member_name = member.display_name if member else f"{actor.display_name} (⚠)"
embed.add_field(
name="",
Expand Down
2 changes: 1 addition & 1 deletion bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async def on_ready(self):
log.info("\n" + self.app_commands_info_text)
log.info("\n" + await self.app_commands_remote_info_text)
log.info("\n" + self.commands_info_text)
await self.sync_commands()
# await self.sync_commands()

@Cog.listener()
async def on_message(self, message: Message):
Expand Down

0 comments on commit 7f1ed29

Please sign in to comment.