|
| 1 | +"""Spectacle Studios Discord Servers - XP Leaderboard Plugin.""" |
| 2 | + |
| 3 | +import discord |
| 4 | +from discord import app_commands |
| 5 | +from discord.ext import commands |
| 6 | + |
| 7 | +from tux.core.base_cog import BaseCog |
| 8 | +from tux.core.bot import Tux |
| 9 | +from tux.modules.features.levels import LevelsService |
| 10 | +from tux.ui.embeds import EmbedCreator, EmbedType |
| 11 | + |
| 12 | + |
| 13 | +class Leaderboard(BaseCog): |
| 14 | + """Manage and expose the XP leaderboard functionality. |
| 15 | +
|
| 16 | + This cog provides commands that allow users to view XP rankings and related |
| 17 | + leaderboard information within the Discord server. |
| 18 | + """ |
| 19 | + |
| 20 | + def __init__(self, bot: Tux) -> None: |
| 21 | + super().__init__(bot) |
| 22 | + |
| 23 | + self.levels_service = LevelsService(bot) |
| 24 | + |
| 25 | + @commands.command(name="leaderboard", aliases=["lb", "top"]) |
| 26 | + @app_commands.guild_only() |
| 27 | + async def leaderboard(self, ctx: commands.Context[Tux]) -> None: |
| 28 | + """Show the XP leaderboard for the current Discord server. |
| 29 | +
|
| 30 | + This command responds with a placeholder message until the leaderboard |
| 31 | + functionality is fully implemented. |
| 32 | + """ |
| 33 | + await ctx.defer() |
| 34 | + |
| 35 | + top_members = await self.levels_service.db.levels.get_top_members(0, 10) |
| 36 | + embed = EmbedCreator.create_embed( |
| 37 | + embed_type=EmbedType.INFO, |
| 38 | + title="XP Leaderboard", |
| 39 | + message_timestamp=discord.utils.utcnow(), |
| 40 | + ) |
| 41 | + for member in top_members: |
| 42 | + try: |
| 43 | + user = await self.bot.fetch_user(member.member_id) |
| 44 | + embed.add_field( |
| 45 | + name=f"{user.name}", |
| 46 | + value=f"{int(member.xp):,d}", |
| 47 | + inline=False, |
| 48 | + ) |
| 49 | + except discord.NotFound: |
| 50 | + embed.add_field( |
| 51 | + name="Unknown user", |
| 52 | + value=f"{int(member.xp):,d}", |
| 53 | + inline=False, |
| 54 | + ) |
| 55 | + |
| 56 | + await ctx.send(embed=embed) |
| 57 | + |
| 58 | + |
| 59 | +async def setup(bot: Tux) -> None: |
| 60 | + """Register the leaderboard cog with the bot. |
| 61 | +
|
| 62 | + This function initializes the leaderboard plugin and adds it to the running bot instance. |
| 63 | + """ |
| 64 | + await bot.add_cog(Leaderboard(bot)) |
0 commit comments