import json import os import random import string import discord from discord import app_commands from discord.ext import commands from utils.guild_config import has_role COMPTES_ENTREPRISES_PATH = "comptes_entreprises.json" def generer_rib(): return " ".join(["".join(random.choices(string.digits, k=5)) for _ in range(4)]) def generer_mdp(): return "".join(random.choices(string.ascii_letters + string.digits, k=8)) class OuvrirCompteEntreprise(commands.Cog): def __init__(self, bot): self.bot = bot banque_choix = [ app_commands.Choice(name="Victoria's Credit Bank", value="Victoria's Credit Bank"), app_commands.Choice(name="NGC Bank", value="NGC Bank"), ] tva_choix = [ app_commands.Choice(name="2%", value=2), app_commands.Choice(name="5%", value=5), app_commands.Choice(name="10%", value=10), app_commands.Choice(name="20%", value=20), ] @app_commands.command(name="ouvrir_compte_entreprise", description="Ouvre un compte bancaire pour une entreprise") @app_commands.choices(banque=banque_choix, tva=tva_choix) async def ouvrir_compte_entreprise(self, interaction: discord.Interaction, entreprise: str, banque: app_commands.Choice[str], tva: app_commands.Choice[int]): # ✅ Vérifie que l'utilisateur est banquier if not has_role(interaction.user, "banquier"): await interaction.response.send_message("❌ Vous n'avez pas la permission d'exécuter cette commande.", ephemeral=True) return banque_name = banque.value entreprise = entreprise.strip() # ✅ Chargement sécurisé du fichier if os.path.exists(COMPTES_ENTREPRISES_PATH): try: with open(COMPTES_ENTREPRISES_PATH, "r", encoding="utf-8") as f: comptes = json.load(f) except json.JSONDecodeError: comptes = {} else: comptes = {} if banque_name not in comptes: comptes[banque_name] = {} if entreprise in comptes[banque_name]: await interaction.response.send_message(f"❌ Un compte existe déjà pour l'entreprise **{entreprise}** dans la banque **{banque_name}**.", ephemeral=True) return rib = generer_rib() mdp = generer_mdp() comptes[banque_name][entreprise] = { "rib": rib, "solde": 0, "mdp": mdp, "tva": tva.value, "transactions": [] } with open(COMPTES_ENTREPRISES_PATH, "w", encoding="utf-8") as f: json.dump(comptes, f, ensure_ascii=False, indent=4) # ✅ MP à l'utilisateur avec le RIB et le mot de passe try: await interaction.user.send( f"🏦 Compte de l'entreprise **{entreprise}** ouvert avec succès dans la banque **{banque_name}** !\n" f"💳 RIB : `{rib}`\n" f"🔐 Mot de passe : `{mdp}`\n" f"💼 Taux de TVA : `{tva.value}%`\n\n" f"Conservez bien ces informations." ) except discord.Forbidden: await interaction.response.send_message("⚠️ Impossible de vous envoyer un message privé avec les informations du compte.", ephemeral=True) return await interaction.response.send_message(f"✅ Le compte de l'entreprise **{entreprise}** a été créé avec succès.", ephemeral=True) async def setup(bot): await bot.add_cog(OuvrirCompteEntreprise(bot))