import discord class Extension: def __init__(self, core): @core.tree.command(name = "servers", description = "Показать статистику с серверов") @discord.app_commands.describe(server = "Название сервера") @discord.app_commands.choices(server = core.GetServersChoice()) async def get_servers( interaction: discord.Interaction, server: discord.app_commands.Choice[str] = None ): await interaction.response.defer(thinking=True) steam64 = await core.GetSteam64OfDiscord(interaction.user) embed = discord.Embed() if server: server = server.value if not server in core.stats.get("servers", {}).keys(): return await interaction.followup.send(f"Сервер с таким индификатором не существует, введи существующий из предложенных:\n{' '.join(core.stats.get('servers', {}).keys())}", ephemeral=False) embed = single_server(core.stats['servers'][server]) else: servers = [server for server in core.stats["servers"].values() if server['status'] == True and server['player_count'] > 0] if servers: embed.add_field(name = "Где сейчас играют", value = f'{core.stats["statistic"]["player_now"]} карликов', inline=True) for server in servers: embed.add_field(name = server["name"], value=str_server(server), inline=False) servers = [server for server in core.stats["servers"].values() if server['status'] == True and server['player_count'] == 0] if servers: embed.add_field(name = "Пустующие сервера", value = f"{len(servers)} штук", inline=True) for server in servers: embed.add_field(name = server["name"], value=str_server(server), inline=False) #не забудь потом сделать лямбды дурачек servers = [server for server in core.stats["servers"].values() if server['status'] == False] if servers: embed.add_field(name = "Неработающие сервера", value = "пук", inline=True) for server in servers: embed.add_field(name = server["name"], value=str_server(server), inline=False) return await interaction.followup.send(embed=embed, ephemeral=False) def str_server(data, with_profiles = False): message = "" addr = data['address'].split(":") message += f"https://{addr[0]}/connect/{addr[1]}\n" if data['status'] == False: return message + "Сервер не отвечает" message += f"Карта: {data['map']}\n" message += f"Игроков: {data['player_count']}/{data['max_players']}\n" message += "\n" for player in sorted(data['players'], key = lambda player:player["score"], reverse=True): message += f"{player['duration']:7} | {player['score']:3} | {player['name']}{(' | ' + player['steam']['community_url']) if with_profiles else ''}\n" return message def single_server(server) -> discord.Embed: addr = server['address'].split(":") embed = discord.Embed( title=server['name'], description=f"Карта: {server['map']}\nИгроков: {server['player_count']}/{server['max_players']}", url=f"https://{addr[0]}/connect/{addr[1]}") if len(server['players']) > 24: count = 0 value = "" for player in sorted(server['players'], key = lambda player:player["score"], reverse=True): if count >= 24: value += f"{player['duration']:7} | {player['score']:3} | {player['name']}\n{player['steam']['community_url']}\n\n" continue embed.add_field(name = f"{player['duration']:7} | {player['score']:3} | {player['name']}", value=player['steam']['community_url'], inline=False) count += 1 if value: embed.add_field(name = f"Прочие игроки", value=value, inline=False) else: for player in sorted(server['players'], key = lambda player:player["score"], reverse=True): embed.add_field(name = f"{player['duration']:7} | {player['score']:3} | {player['name']}", value=player['steam']['community_url'], inline=False) return embed