Beta/commandes/categories/deplacer.py
2026-06-16 17:09:34 +00:00

43 lines
No EOL
1.7 KiB
Python

import discord
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)
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')
category = await _find_category(guild, category_params)
if not category:
label = category_name or category_params.get('category_id', 'inconnue')
await message.channel.send(f"Catégorie '{label}' introuvable.")
continue
try:
await category.edit(position=position)
moved_count += 1
except discord.Forbidden:
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}'.")
if moved_count > 0:
await message.channel.send(f"{moved_count} catégorie(s) déplacée(s).")