From 014025f547e16c7b655eb044011669c3b0238c7f Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Fri, 15 Mar 2024 16:22:10 -0400 Subject: [PATCH] feat(pterodactyl): added a players command --- pterodactyl/pterodactyl.py | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/pterodactyl/pterodactyl.py b/pterodactyl/pterodactyl.py index f6f1d37..bc477fa 100644 --- a/pterodactyl/pterodactyl.py +++ b/pterodactyl/pterodactyl.py @@ -1,6 +1,6 @@ import asyncio import json -from typing import Mapping, Optional, Union +from typing import Mapping, Optional, Tuple, Union import discord import websockets @@ -137,6 +137,24 @@ class Pterodactyl(commands.Cog): command = command.replace('.$' + key, value) return command + async def get_player_list(self) -> Optional[Tuple[str, list]]: + if await config.api_endpoint() == "minecraft": + status, response = await mcsrvstatus.get_status(await config.topic_hostname(), await config.topic_port()) + if status and 'list' in response['players']: + output_str = '' + for player in response['players']['list']: + output_str += f"{player['name']}\n" + return output_str, response['players']['list'] + return None + + async def get_player_list_embed(self, ctx: Union[commands.Context, discord.Interaction]) -> Optional[discord.Embed]: + player_list = await self.get_player_list() + if player_list: + embed = discord.Embed(color=await self.bot.get_embed_color(ctx.channel), title="Players Online") + embed.description = player_list[0] + return embed + return None + async def power(self, ctx: Union[discord.Interaction, commands.Context], action: str, action_ing: str, warning: str = '') -> None: if isinstance(ctx, discord.Interaction): author = ctx.user @@ -230,6 +248,15 @@ class Pterodactyl(commands.Cog): The command to send to the server.""" return await self.send_command(interaction, command) + @slash_pterodactyl.command(name = "players", description = "Retrieve a list of players on the server.") + async def slash_pterodactyl_players(self, interaction: discord.Interaction) -> None: + """Retrieve a list of players on the server.""" + e = await self.get_player_list_embed(interaction) + if e: + await interaction.response.send_message(embed=e, ephemeral=True) + else: + await interaction.response.send_message("No players online.", ephemeral=True) + @slash_pterodactyl.command(name = "power", description = "Send power actions to the server.") @app_commands.choices(action=[ Choice(name="Start", value="start"), @@ -254,6 +281,15 @@ class Pterodactyl(commands.Cog): async def pterodactyl(self, ctx: commands.Context) -> None: """Pterodactyl allows you to manage your Pterodactyl Panel from Discord.""" + @pterodactyl.command(name = "players", aliases=["list", "online", "playerlist", "who"]) + async def pterodactyl_players(self, ctx: commands.Context) -> None: + """Retrieve a list of players on the server.""" + e = await self.get_player_list_embed(ctx) + if e: + await ctx.send(embed=e) + else: + await ctx.send("No players online.") + @pterodactyl.command(name = "command", aliases = ["cmd", "execute", "exec"]) @commands.admin() async def pterodactyl_command(self, ctx: commands.Context, *, command: str) -> None: