59 lines
No EOL
2.2 KiB
Python
59 lines
No EOL
2.2 KiB
Python
import discord
|
|
import re
|
|
|
|
def strip_emoji(text):
|
|
"""Remove emoji characters for fuzzy matching."""
|
|
emoji_pattern = re.compile("["
|
|
u"\U0001F600-\U0001F64F\U0001F300-\U0001F5FF\U0001F680-\U0001F6FF"
|
|
u"\U0001F1E0-\U0001F1FF\U00002702-\U000027B0\U000024C2-\U0001F251"
|
|
u"\U0000FE00-\U0000FE0F\U0000200D\U0000E0100-\U0000E01EFF"
|
|
u"\u20E3\u00A9\u00AE\u2122"
|
|
"]+", flags=re.UNICODE)
|
|
return emoji_pattern.sub('', text).strip()
|
|
|
|
async def find_category(guild, category_name_or_id):
|
|
"""Find a category by ID or name (exact then fuzzy)."""
|
|
# 1. Try by ID
|
|
try:
|
|
cat_id = int(category_name_or_id)
|
|
cat = guild.get_channel(cat_id)
|
|
if cat and isinstance(cat, discord.CategoryChannel):
|
|
return cat
|
|
except (ValueError, TypeError):
|
|
pass
|
|
|
|
# 2. Exact name
|
|
for cat in guild.categories:
|
|
if cat.name.casefold() == category_name_or_id.casefold():
|
|
return cat
|
|
|
|
# 3. Fuzzy (strip emoji)
|
|
clean_search = strip_emoji(category_name_or_id).lower().strip()
|
|
for cat in guild.categories:
|
|
clean_name = strip_emoji(cat.name).lower().strip()
|
|
if clean_name == clean_search or (clean_search and clean_search in clean_name):
|
|
return cat
|
|
|
|
return None
|
|
|
|
async def execute(bot, params, message):
|
|
categories = params.get('categories', [])
|
|
if not categories:
|
|
categories = [params]
|
|
guild = message.guild
|
|
deleted_count = 0
|
|
for category_params in categories:
|
|
category_name = category_params.get('category_name') or category_params.get('category_id')
|
|
if not category_name:
|
|
continue
|
|
category = await find_category(guild, category_name)
|
|
if not category:
|
|
await message.channel.send(f"Catégorie '{category_name}' introuvable.")
|
|
continue
|
|
try:
|
|
await category.delete(reason="Supprimée par le Superviseur")
|
|
deleted_count += 1
|
|
except discord.Forbidden:
|
|
await message.channel.send(f"Je n'ai pas les permissions pour supprimer la catégorie '{category_name}'.")
|
|
if deleted_count > 0:
|
|
await message.channel.send(f"{deleted_count} catégorie(s) supprimée(s).") |