93 lines
3.3 KiB
Python
93 lines
3.3 KiB
Python
"""Context building utilities for Superviseur bot."""
|
|
|
|
import discord
|
|
from typing import Tuple
|
|
|
|
|
|
def build_server_context(guild: discord.Guild) -> str:
|
|
"""Build a comprehensive context string for the server."""
|
|
if not guild:
|
|
return "\nContexte : Aucun serveur (Message Direct)."
|
|
|
|
context = f"\nInformations sur le serveur '{guild.name}' (ID: {guild.id}):\n"
|
|
context += f"- Membres: {guild.member_count}\n"
|
|
context += f"- Propriétaire: {guild.owner.display_name if guild.owner else 'Inconnu'}\n"
|
|
context += f"- Créé le: {guild.created_at.strftime('%Y-%m-%d %H:%M:%S')} UTC\n"
|
|
|
|
# Channels
|
|
text_channels = [ch.name for ch in guild.channels if isinstance(ch, discord.TextChannel)]
|
|
voice_channels = [ch.name for ch in guild.channels if isinstance(ch, discord.VoiceChannel)]
|
|
categories = [cat.name for cat in guild.channels if isinstance(cat, discord.CategoryChannel)]
|
|
|
|
if text_channels:
|
|
context += f"- Salons textes: {len(text_channels)} ({', '.join(text_channels[:5])}...)\n"
|
|
else:
|
|
context += "- Salons textes: 0\n"
|
|
|
|
context += f"- Salons vocaux: {len(voice_channels)}\n"
|
|
|
|
if categories:
|
|
context += f"- Catégories: {len(categories)} ({', '.join(categories[:3])}...)\n"
|
|
else:
|
|
context += "- Catégories: 0\n"
|
|
|
|
# Roles (except @everyone)
|
|
roles = [role.name for role in guild.roles[1:]] # Skip @everyone
|
|
if roles:
|
|
context += f"- Rôles: {len(roles)}\n"
|
|
for role_name in roles:
|
|
context += f" - {role_name}\n"
|
|
else:
|
|
context += "- Rôles: 0\n"
|
|
|
|
# Liste des membres (si le serveur n'est pas trop grand)
|
|
if len(guild.members) <= 200:
|
|
members_list = [m.display_name for m in guild.members]
|
|
context += f"- Tous les membres ({len(members_list)}): {', '.join(members_list)}\n"
|
|
else:
|
|
recent_members = sorted(guild.members, key=lambda m: m.joined_at or m.created_at, reverse=True)[:5]
|
|
context += f"- Membres récents: {', '.join([m.display_name for m in recent_members])}\n"
|
|
|
|
return context
|
|
|
|
|
|
def build_channels_list(guild: discord.Guild) -> str:
|
|
"""Build a formatted list of all channels with IDs."""
|
|
if not guild:
|
|
return ""
|
|
|
|
lines = ["\nListe des salons (nom | id | type):"]
|
|
for ch in guild.channels:
|
|
if isinstance(ch, discord.TextChannel):
|
|
ch_type = 'textuel'
|
|
elif isinstance(ch, discord.VoiceChannel):
|
|
ch_type = 'vocal'
|
|
else:
|
|
ch_type = 'catégorie'
|
|
lines.append(f"- {ch.name} | {ch.id} | {ch_type}")
|
|
return "\n".join(lines)
|
|
|
|
|
|
def build_context_for_message(
|
|
guild: discord.Guild,
|
|
channels_list_cache: dict,
|
|
server_context_cache: dict
|
|
) -> Tuple[str, str]:
|
|
"""Build channels list and server context with caching."""
|
|
if not guild:
|
|
return "", build_server_context(None)
|
|
|
|
guild_id = guild.id
|
|
|
|
# Build channels list with caching
|
|
if guild_id not in channels_list_cache:
|
|
channels_list_cache[guild_id] = build_channels_list(guild)
|
|
channels_list = channels_list_cache[guild_id]
|
|
|
|
# Build server context with caching
|
|
if guild_id not in server_context_cache:
|
|
server_context_cache[guild_id] = build_server_context(guild)
|
|
server_context = server_context_cache[guild_id]
|
|
|
|
return channels_list, server_context
|
|
|