Vicy_Publique/commandes/compte_banque_nationale.py
2026-08-16 19:37:15 +02:00

102 lines
4.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import discord
from discord import app_commands
from discord.ext import commands
import json
import math
BANQUE_NATIONALE_ROLE_ID = 1362031086959329470 # Remplace par l’ID réel du rôle banque_nationale
class CompteBanqueNationale(commands.Cog):
def __init__(self, bot):
self.bot = bot
def get_banque_nationale_data(self):
try:
with open("comptes_entreprises.json", "r+", encoding="utf-8") as f:
data = json.load(f)
# Crée la section si elle n'existe pas
if "banque_nationale" not in data:
data["banque_nationale"] = {
"BANQUE": {
"rib": "11111 22222 33333333333 44",
"solde": 0.0,
"transactions": [{
"date": "Création automatique",
"type": "Création de compte",
"source": "Système",
"montant": 0.0
}]
}
}
f.seek(0)
json.dump(data, f, indent=4, ensure_ascii=False)
f.truncate()
banque_data = data["banque_nationale"].get("BANQUE", {})
return banque_data.get("solde", 0.0), banque_data.get("transactions", [])
except Exception as e:
print(f"Erreur lecture ou création banque_nationale : {e}")
return 0.0, []
@app_commands.command(name="voir_banque_nationale", description="Voir solde et transactions du compte banque nationale")
async def voir_banque_nationale(self, interaction: discord.Interaction):
if not any(role.id == BANQUE_NATIONALE_ROLE_ID for role in interaction.user.roles):
await interaction.response.send_message("❌ Vous n'avez pas la permission d'accéder à ce compte.", ephemeral=True)
return
solde, transactions = self.get_banque_nationale_data()
if not transactions:
await interaction.response.send_message("📄 Aucune transaction trouvée sur le compte banque nationale.", ephemeral=True)
return
# Pagination
pages = []
per_page = 5
total_pages = math.ceil(len(transactions) / per_page)
for i in range(total_pages):
start = i * per_page
end = start + per_page
chunk = transactions[start:end]
embed = discord.Embed(
title="🏦 Banque Nationale - Historique",
description=f"Page {i+1}/{total_pages}",
color=discord.Color.blue()
)
embed.add_field(name="💰 Solde", value=f"{solde:.2f}€", inline=False)
for tx in chunk:
description = (
f"**Date**: {tx.get('date', 'N/A')}\n"
f"**Type**: {tx.get('type', 'N/A')}\n"
f"**Montant**: {tx.get('montant', 0)}€\n"
f"**Source**: {tx.get('source', 'N/A')}"
)
embed.add_field(name="📄 Transaction", value=description, inline=False)
pages.append(embed)
class Pagination(discord.ui.View):
def __init__(self):
super().__init__(timeout=None)
self.current = 0
@discord.ui.button(label="⬅️", style=discord.ButtonStyle.grey)
async def previous(self, interaction_btn: discord.Interaction, button: discord.ui.Button):
if self.current > 0:
self.current -= 1
await interaction_btn.response.edit_message(embed=pages[self.current], view=self)
@discord.ui.button(label="➡️", style=discord.ButtonStyle.grey)
async def next(self, interaction_btn: discord.Interaction, button: discord.ui.Button):
if self.current < len(pages) - 1:
self.current += 1
await interaction_btn.response.edit_message(embed=pages[self.current], view=self)
await interaction.response.send_message(embed=pages[0], view=Pagination(), ephemeral=True)
async def setup(bot):
await bot.add_cog(CompteBanqueNationale(bot))