Vicy_Publique/commandes/compte_urssaf.py

96 lines
3.8 KiB
Python
Raw Normal View History

2026-08-16 19:37:15 +02:00
import discord
from discord import app_commands
from discord.ext import commands
import json
import math
from utils.guild_config import has_role
2026-08-16 19:37:15 +02:00
class CompteUrssaf(commands.Cog):
def __init__(self, bot):
self.bot = bot
def get_all_transactions(self):
try:
with open("comptes_entreprises.json", "r", encoding="utf-8") as f:
data = json.load(f)
except Exception as e:
print(f"Erreur lecture comptes_entreprises.json : {e}")
return 0.0, []
# Si URSAFF n'existe pas, on le crée
if "ursaff" not in data:
data["ursaff"] = {
"URSSAFF": {
"rib": "00000 00000 00000000000 01",
"solde": 0.0,
"transactions": []
}
}
with open("comptes_entreprises.json", "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=4)
ursaff_data = data.get("ursaff", {}).get("URSAFF", {})
return ursaff_data.get("solde", 0.0), ursaff_data.get("transactions", [])
@app_commands.command(name="compte_ursaff", description="Affiche le solde et l'historique complet du compte URSAFF (réservé aux membres URSAFF).")
async def compte_ursaff(self, interaction: discord.Interaction):
# Vérifie que l'utilisateur a le rôle URSAFF
if not has_role(interaction.user, "ursaff"):
2026-08-16 19:37:15 +02:00
await interaction.response.send_message("❌ Vous n'avez pas la permission d'accéder à ce compte.", ephemeral=True)
return
solde, transactions = self.get_all_transactions()
# Pagination setup
pages = []
per_page = 5
total_pages = max(1, 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="📄 Historique URSSAF",
description=f"Page {i+1}/{total_pages}",
color=discord.Color.dark_purple()
)
embed.add_field(name="💼 Solde actuel", value=f"{solde:.2f}€", inline=False)
if chunk:
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)
else:
embed.add_field(name="📄 Transactions", value="Aucune transaction récente.", 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(CompteUrssaf(bot))