Beta/commandes/salons/renommer.py

52 lines
2 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)
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
renamed_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')
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
if not new_name:
await message.channel.send("Aucun nouveau nom spécifié pour le renommage.")
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:
await channel.edit(name=new_name)
renamed_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 renommer le salon '{label}'.")
except Exception as e:
label = channel_name or channel_params.get('channel_id', 'inconnu')
await message.channel.send(f"Erreur lors du renommage du salon '{label}': {e}")
2026-02-06 22:23:20 +01:00
if renamed_count > 0:
2026-06-16 17:09:34 +00:00
await message.channel.send(f"{renamed_count} salon(s) renommé(s).")