Beta/commandes/salons/supprimer.py

74 lines
2.8 KiB
Python
Raw Permalink Normal View History

2026-02-06 22:23:20 +01:00
import discord
import re
def strip_emoji(text):
"""Remove emoji characters from channel/role names for fuzzy matching."""
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F" # emoticons
u"\U0001F300-\U0001F5FF" # symbols & pictographs
u"\U0001F680-\U0001F6FF" # transport & map symbols
u"\U0001F1E0-\U0001F1FF" # flags (iOS)
u"\U00002702-\U000027B0" # dingbats
u"\U000024C2-\U0001F251" # misc
u"\U0000FE00-\U0000FE0F" # variation selectors
u"\U0000200D" # zero width joiner
u"\U0000E0100-\U0000E01EFF" # variation selectors supplement
u"\u20E3" # combining enclosing keycap
u"\u00A9\u00AE\u2122" # copyright, registered, trademark
"]+", flags=re.UNICODE)
return emoji_pattern.sub('', text).strip()
async def find_channel(guild, channel_name_or_id):
"""Find a channel by ID first, then by name (exact, then flexible)."""
# 1. Try by ID
try:
ch_id = int(channel_name_or_id)
channel = guild.get_channel(ch_id)
if channel:
return channel
except (ValueError, TypeError):
pass
# 2. Try exact name match (case-insensitive)
for ch in guild.channels:
if ch.name.casefold() == channel_name_or_id.casefold():
return ch
# 3. Try fuzzy match (strip emojis from both sides)
clean_search = strip_emoji(channel_name_or_id).lower().strip()
for ch in guild.channels:
clean_name = strip_emoji(ch.name).lower().strip()
if clean_name == clean_search:
return ch
# 4. If the search name is contained in the channel name
if clean_search and clean_search in clean_name:
return ch
# 5. Or the channel name is contained in the search
if clean_search and clean_name in clean_search:
return ch
return None
2026-02-06 22:23:20 +01:00
async def execute(bot, params, message):
channels = params.get('channels', [])
if not channels:
channels = [params]
guild = message.guild
deleted_count = 0
for channel_params in channels:
# Support both channel_id and channel_name
channel_name = channel_params.get('channel_name') or channel_params.get('channel_id')
if not channel_name:
continue # skip if no name or id
channel = await find_channel(guild, channel_name)
2026-02-06 22:23:20 +01:00
if not channel:
await message.channel.send(f"Salon '{channel_name}' introuvable.")
continue
try:
await channel.delete(reason="Supprimé par le Superviseur")
deleted_count += 1
except discord.Forbidden:
await message.channel.send(f"Je n'ai pas les permissions pour supprimer le salon '{channel_name}'.")
if deleted_count > 0:
await message.channel.send(f"{deleted_count} salon(s) supprimé(s).")