Beta/commandes/categories/deplacer.py

43 lines
1.7 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_category(guild, category_params):
"""Find a category by ID first, then by name (case-insensitive)."""
category_id = 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('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):
categories = params.get('categories', [])
if not categories:
# Fallback for single category
categories = [params]
guild = message.guild
moved_count = 0
for category_params in categories:
category_name = category_params.get('category_name')
position = category_params.get('position')
2026-06-16 17:09:34 +00:00
category = await _find_category(guild, category_params)
2026-02-06 22:23:20 +01:00
if not category:
2026-06-16 17:09:34 +00:00
label = category_name or category_params.get('category_id', 'inconnue')
await message.channel.send(f"Catégorie '{label}' introuvable.")
2026-02-06 22:23:20 +01:00
continue
try:
await category.edit(position=position)
moved_count += 1
except discord.Forbidden:
2026-06-16 17:09:34 +00:00
label = category_name or category_params.get('category_id', 'inconnue')
await message.channel.send(f"Je n'ai pas les permissions pour déplacer la catégorie '{label}'.")
2026-02-06 22:23:20 +01:00
if moved_count > 0:
2026-06-16 17:09:34 +00:00
await message.channel.send(f"{moved_count} catégorie(s) déplacée(s).")