from discord import app_commands import discord from GammaRPBot_Define import GammaRPBot import aiosqlite import base64 async def setup(bot:GammaRPBot): bot.tree.add_command(PersoCommands(bot)) class PersoCommands(app_commands.Group): bot:GammaRPBot def __init__(self,bot): super().__init__(name="perso",description="Commandes pour les personnages") self.bot=bot @app_commands.command(name="créer",description="Crée un personnage") @app_commands.describe(short_name="Le nom court du personnage (son ID)", full_name="Le nom complet du personnage", story="L'histoire du personnage",powers="Les pouvoirs du personnage",age="L'âge du personnage", sexe="Le sexe du personnage",guild_only="Personnage global ou uniquement sur ce serveur",image="L'image du personnage") @app_commands.choices(guild_only=[app_commands.Choice(name="Global",value="global"),app_commands.Choice(name="Ce serveur uniquement", value="guild")]) async def create_character(self,interaction:discord.Interaction,short_name:str,full_name:str,image:discord.Attachment,story:str,powers:str,age:int,sexe:str,guild_only:app_commands.Choice[str]): nom_court = short_name nom_complet= full_name if image.content_type!="image/png" and image.content_type!="image/jpeg": await interaction.response.send_message("L'image doit être un fichier PNG ou JPEG",ephemeral=True) return histoire = story pouvoirs = powers uuid=interaction.user.id guild_id = interaction.guild.id image_blob_raw=await image.read() image_blob_b64 = base64.b64encode(image_blob_raw).decode("utf-8") ext = image.filename.split(".")[-1] image_blob=f"data:image/{ext};base64,{image_blob_b64}" print(nom_court,nom_complet,histoire,pouvoirs,age,sexe,uuid,guild_id) print(image.filename) print(image.url) print(image_blob) try: res = await self.bot.db.execute("INSERT INTO personnages (nom_court,nom_complet,histoire,pouvoirs,age,sexe,uuid,guild_id,image) VALUES (?,?,?,?,?,?,?,?,?)",(nom_court,nom_complet,histoire,pouvoirs,age,sexe,uuid,guild_id,image_blob)) print(res) await res.close() except Exception as e: print(e) await interaction.response.send_message("Erreur lors de la création du personnage",ephemeral=True) return except aiosqlite.Warning as w: print(w) await interaction.response.send_message("Erreur lors de la création du personnage",ephemeral=True) return except aiosqlite.Error as e: print(e) await interaction.response.send_message("Erreur lors de la création du personnage",ephemeral=True) return await interaction.response.send_message("Personnage créé",ephemeral=True) @app_commands.command(name="lister",description="Liste tous les personnages") async def list_characters(self,interaction:discord.Interaction): print("perso list") res = await self.bot.db.execute("SELECT * FROM personnages") dict_rows = await res.fetchall() await res.close() print(dict_rows) message="" for row in dict_rows: print(row) message+=(f"Nom court: {row[0]}\nNom complet: {row[1]}\nHistoire: {row[2]}\nPouvoirs: {row[3]}\nAge: {row[4]}\nSexe: {row[5]}\nUUID: {row[6]}\nGuild ID: {row[7]}"+"\n\n") await interaction.response.send_message("Liste des personnages\n"+message,ephemeral=True)