Migration de Kuby vers disnake

This commit is contained in:
Mathis 2026-05-06 17:07:09 +02:00
parent dc6e235f27
commit c8c579eefe
36 changed files with 1205 additions and 1253 deletions

View file

@ -5,9 +5,8 @@ import zipfile
import tempfile
import shutil
from typing import List
import discord
from discord import app_commands
from discord.ext import commands
import disnake
from disnake.ext import commands
BACKUPS_ROOT = "data/security_backups"
@ -43,7 +42,7 @@ class BackupRestore(commands.Cog):
elements.append(base)
return elements
async def element_autocomplete(self, interaction: discord.Interaction, current: str):
async def element_autocomplete(self, interaction: disnake.ApplicationCommandInteraction, current: str):
"""Complétion automatique pour l'option 'element'."""
guild = interaction.guild
if guild is None:
@ -60,9 +59,9 @@ class BackupRestore(commands.Cog):
backup_path = os.path.join(BACKUPS_ROOT, last_backup)
elements = await self.list_backup_elements(backup_path)
return [app_commands.Choice(name=e, value=e) for e in elements if current.lower() in e.lower()][:25]
return [e for e in elements if current.lower() in e.lower()][:25]
async def restore_roles(self, guild: discord.Guild, backup_path: str):
async def restore_roles(self, guild: disnake.Guild, backup_path: str):
"""Restaure les rôles à partir du backup."""
roles_file = os.path.join(backup_path, "roles.json")
if os.path.isfile(roles_file):
@ -72,16 +71,16 @@ class BackupRestore(commands.Cog):
try:
await guild.create_role(
name=r["name"],
permissions=discord.Permissions(r["permissions"]),
color=discord.Color(r.get("color", 0)),
permissions=disnake.Permissions(r["permissions"]),
color=disnake.Color(r.get("color", 0)),
hoist=r.get("hoist", False),
mentionable=r.get("mentionable", False),
reason="Restauration backup"
)
except discord.Forbidden:
except disnake.Forbidden:
continue
async def restore_channels(self, guild: discord.Guild, backup_path: str):
async def restore_channels(self, guild: disnake.Guild, backup_path: str):
"""Restaure les channels à partir du backup."""
channels_file = os.path.join(backup_path, "channels.json")
if os.path.isfile(channels_file):
@ -105,10 +104,10 @@ class BackupRestore(commands.Cog):
user_limit=c.get("user_limit", 0),
reason="Restauration backup"
)
except discord.Forbidden:
except disnake.Forbidden:
continue
async def restore_messages(self, channel: discord.TextChannel, backup_path: str):
async def restore_messages(self, channel: disnake.TextChannel, backup_path: str):
"""Restaure les messages à partir du backup (solution ZIP incluse)."""
if backup_path.endswith(".zip"):
temp_dir = tempfile.mkdtemp()
@ -133,17 +132,12 @@ class BackupRestore(commands.Cog):
for msg in messages:
await channel.send(msg["content"])
@app_commands.command(name="granulaire", description="Restaure un élément précis d'une sauvegarde")
@app_commands.describe(
backup="Nom ou ID de la sauvegarde",
element="Élément à restaurer (roles, channels, messages...)"
)
@app_commands.autocomplete(element=element_autocomplete)
@commands.slash_command(name="granulaire", description="Restaure un élément précis d'une sauvegarde")
async def restore_granulaire(
self,
interaction: discord.Interaction,
backup: str,
element: str
interaction: disnake.ApplicationCommandInteraction,
backup: str = commands.Param(description="Nom ou ID de la sauvegarde"),
element: str = commands.Param(description="Élément à restaurer (roles, channels, messages...)")
):
guild = interaction.guild
if guild is None:
@ -162,7 +156,7 @@ class BackupRestore(commands.Cog):
await interaction.response.send_message(f"❌ La sauvegarde `{backup}` est introuvable.", ephemeral=True)
return
await interaction.response.defer(ephemeral=True, thinking=True)
await interaction.response.defer(ephemeral=True)
available_elements = await self.list_backup_elements(backup_path)
if element not in available_elements:
@ -200,5 +194,9 @@ class BackupRestore(commands.Cog):
ephemeral=True
)
@restore_granulaire.autocomplete("element")
async def element_autocomplete_decorator(self, interaction: disnake.ApplicationCommandInteraction, current: str):
return await self.element_autocomplete(interaction, current)
async def setup(bot: commands.Bot):
await bot.add_cog(BackupRestore(bot))