GammaRPBot/perso_commands.py

130 lines
6.8 KiB
Python
Raw Normal View History

from discord import app_commands
import discord
from GammaRPBot_Define import GammaRPBot
2024-12-15 15:11:15 +01:00
import aiosqlite
import base64
import io
2024-12-16 13:29:58 +01:00
from public_env import *
_sql_table="""CREATE TABLE personnages (
nom_court varchar(255),
nom_complet text,
image text,
histoire text,
pouvoirs text,
uuid TEXT,
age INTEGER,
sexe TEXT,
guild_id INTEGER NULL
)"""
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",
2024-12-15 16:45:23 +01:00
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
if " " in nom_court:
await interaction.response.send_message("Le nom court ne doit pas contenir d'espaces",ephemeral=True)
return
2024-12-16 13:37:30 +01:00
check_exist = await self.bot.db.execute("SELECT * FROM personnages WHERE nom_court=?", (nom_court,))
if check_exist.rowcount>0 or await check_exist.fetchone() is not None:
await interaction.response.send_message("Ce nom court est déjà utilisé",ephemeral=True)
return
histoire = story
pouvoirs = powers
uuid=interaction.user.id
guild_id = interaction.guild.id
image_blob_raw=await image.read()
a = await self.bot.get_guild(1037663859621765160).get_channel(1317876780635394068).send(file=discord.File(io.BytesIO(image_blob_raw),filename=image.filename))
perso_image_url = a.attachments[0].url
print(nom_court,nom_complet,histoire,pouvoirs,age,sexe,uuid,guild_id)
print(image.filename)
2024-12-15 15:11:15 +01:00
print(image.url)
2024-12-15 15:04:42 +01:00
try:
2024-12-15 16:58:09 +01:00
res = await self.bot.db.execute("INSERT INTO personnages (nom_court,nom_complet,histoire,pouvoirs,age,sexe,uuid,image) VALUES (?,?,?,?,?,?,?,?)",(nom_court,nom_complet,histoire,pouvoirs,age,sexe,uuid,perso_image_url))
2024-12-15 15:04:42 +01:00
print(res)
await res.close()
await self.bot.db.commit()
2024-12-15 15:04:42 +01:00
except Exception as e:
print(e)
await interaction.response.send_message("Erreur lors de la création du personnage",ephemeral=True)
return
2024-12-15 15:11:15 +01:00
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
2024-12-15 15:08:07 +01:00
await interaction.response.send_message("Personnage créé",ephemeral=True)
2024-12-15 16:45:23 +01:00
@app_commands.command(name="afficher",description="Affiche un personnage")
@app_commands.describe(short_name="Le nom court du personnage")
async def show_character(self,interaction:discord.Interaction,short_name:str):
res = await self.bot.db.execute("SELECT * FROM personnages WHERE nom_court=? AND uuid=? AND (guild_id=? OR guild_id IS NULL)",(short_name,interaction.user.id,interaction.guild.id))
row = await res.fetchone()
await res.close()
if row is None:
await interaction.response.send_message("Personnage non trouvé",ephemeral=True)
return
embed = await perso_embed(self.bot,row)
2024-12-16 13:27:12 +01:00
await interaction.response.send_message(f"Affichage de {row['nom_complet']}({row['nom_court']})",embed=embed)
@show_character.autocomplete(name="short_name")
async def autocomplete_short_name(self,interaction:discord.Interaction,current:str):
res = await self.bot.db.execute("SELECT nom_court FROM personnages WHERE uuid=? AND (guild_id=? OR guild_id IS NULL)",(interaction.user.id,interaction.guild.id))
rows = await res.fetchall()
await res.close()
return [app_commands.OptionChoice(name=row["nom_court"],value=row["nom_court"]) for row in rows]
2024-12-16 13:40:56 +01:00
@app_commands.command(name="lister",description="Liste les personnages sur le serveur")
async def list_characters(self,interaction:discord.Interaction,output_hidden:bool=True):
res = await self.bot.db.execute("SELECT * FROM personnages WHERE guild_id=? OR guild_id IS NULL",(interaction.guild.id,))
rows = await res.fetchall()
await res.close()
embeds = []
for row in rows:
embeds.append(await perso_embed(self.bot,row))
await interaction.response.send_message("Liste des personnages",embeds=embeds,ephemeral=output_hidden)
@app_commands.command(name="lister_tout",description="Liste tous les personnages")
async def list_characters(self,interaction:discord.Interaction,output_hidden:bool=True):
#admin
2024-12-16 13:29:58 +01:00
if interaction.user.id !=super_owner_id:
await interaction.response.send_message("Vous n'avez pas la permission d'exécuter cette commande",ephemeral=True)
return
2024-12-15 15:11:15 +01:00
print("perso list")
2024-12-15 15:08:07 +01:00
res = await self.bot.db.execute("SELECT * FROM personnages")
rows = await res.fetchall()
2024-12-15 15:08:07 +01:00
await res.close()
2024-12-15 16:29:51 +01:00
embeds = []
for row in rows:
2024-12-15 16:36:05 +01:00
embeds.append(await perso_embed(self.bot,row))
2024-12-16 13:26:26 +01:00
await interaction.response.send_message("Liste des personnages\n",embeds=embeds,ephemeral=output_hidden)
2024-12-15 16:29:51 +01:00
async def perso_embed(bot:GammaRPBot,perso_datas)->discord.embeds.Embed:
2024-12-15 16:29:51 +01:00
embed = discord.Embed(title=perso_datas["nom_complet"],description=perso_datas["histoire"])
embed.add_field(name="Pouvoirs",value=perso_datas["pouvoirs"])
embed.add_field(name="Age",value=perso_datas["age"])
embed.add_field(name="Sexe",value=perso_datas["sexe"])
embed.set_footer(text=f"Nom court (ID): {perso_datas['nom_court']}")
embed.set_thumbnail(url=perso_datas["image"])
2024-12-15 16:29:51 +01:00
return embed