Beta/commandes/salons/modifier.py

85 lines
3.5 KiB
Python
Raw Normal View History

2026-02-06 22:23:20 +01:00
import discord
2026-06-16 17:09:34 +00:00
async def _find_channel(guild, channel_params):
"""Find a channel by ID first, then by name (case-insensitive)."""
channel_id = channel_params.get('channel_id')
if channel_id:
try:
ch = guild.get_channel(int(channel_id))
if ch:
return ch
except (ValueError, TypeError):
pass
channel_name = channel_params.get('channel_name')
if not channel_name:
return None
return discord.utils.find(lambda c: c.name.casefold() == channel_name.casefold(), guild.channels)
async def _find_category(guild, category_params):
"""Find a category by ID first, then by name (case-insensitive)."""
category_id = category_params.get('new_category_id') or category_params.get('category_id')
if category_id:
try:
cat = guild.get_channel(int(category_id))
if cat and isinstance(cat, discord.CategoryChannel):
return cat
except (ValueError, TypeError):
pass
category_name = category_params.get('new_category_name') or category_params.get('category_name')
if not category_name:
return None
return discord.utils.find(lambda cat: cat.name.casefold() == category_name.casefold(), guild.categories)
2026-02-06 22:23:20 +01:00
async def execute(bot, params, message):
channels = params.get('channels', [])
if not channels:
# Fallback for single channel
channels = [params]
guild = message.guild
modified_count = 0
for channel_params in channels:
channel_name = channel_params.get('channel_name')
2026-06-16 17:09:34 +00:00
# Support both 'new_name' and 'new_channel_name' variants
new_name = channel_params.get('new_name') or channel_params.get('new_channel_name')
new_category_id = channel_params.get('new_category_id')
new_category_name = channel_params.get('new_category_name')
channel = await _find_channel(guild, channel_params)
2026-02-06 22:23:20 +01:00
if not channel:
2026-06-16 17:09:34 +00:00
label = channel_name or channel_params.get('channel_id', 'inconnu')
await message.channel.send(f"Salon '{label}' introuvable.")
continue
# Build edit kwargs based on what params are provided
edit_kwargs = {}
if new_name:
edit_kwargs['name'] = new_name
if new_category_id or new_category_name:
category = await _find_category(guild, channel_params)
if category:
edit_kwargs['category'] = category
else:
cat_label = new_category_name or new_category_id or 'inconnue'
await message.channel.send(f"Catégorie '{cat_label}' introuvable.")
continue
if not edit_kwargs:
await message.channel.send("Aucune modification à appliquer (ni nom, ni catégorie spécifiés).")
2026-02-06 22:23:20 +01:00
continue
2026-06-16 17:09:34 +00:00
2026-02-06 22:23:20 +01:00
try:
2026-06-16 17:09:34 +00:00
await channel.edit(**edit_kwargs)
2026-02-06 22:23:20 +01:00
modified_count += 1
except discord.Forbidden:
2026-06-16 17:09:34 +00:00
label = channel_name or channel_params.get('channel_id', 'inconnu')
await message.channel.send(f"Je n'ai pas les permissions pour modifier le salon '{label}'.")
except Exception as e:
label = channel_name or channel_params.get('channel_id', 'inconnu')
await message.channel.send(f"Erreur lors de la modification du salon '{label}': {e}")
2026-02-06 22:23:20 +01:00
if modified_count > 0:
2026-06-16 17:09:34 +00:00
await message.channel.send(f"{modified_count} salon(s) modifié(s).")