Initial commit: bot Discord Vicy V1.8
This commit is contained in:
commit
56958c5832
70 changed files with 8469 additions and 0 deletions
148
commandes/abonnement.py
Normal file
148
commandes/abonnement.py
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
import discord
|
||||
from discord import app_commands
|
||||
from discord.ext import commands, tasks
|
||||
import json
|
||||
import datetime
|
||||
|
||||
ABONNEMENTS_FILE = "abonnements.json"
|
||||
|
||||
BANQUES_DISPONIBLES = [
|
||||
"Victoria's Credit Bank",
|
||||
"NGC Bank"
|
||||
]
|
||||
|
||||
JOURS_SEMAINE = [
|
||||
"lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi", "dimanche"
|
||||
]
|
||||
|
||||
class Abonnement(commands.Cog):
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
self.abonnements = self.load_abonnements()
|
||||
self.executer_abonnements.start()
|
||||
|
||||
def load_abonnements(self):
|
||||
try:
|
||||
with open(ABONNEMENTS_FILE, 'r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
return []
|
||||
|
||||
def save_abonnements(self):
|
||||
with open(ABONNEMENTS_FILE, 'w', encoding='utf-8') as f:
|
||||
json.dump(self.abonnements, f, ensure_ascii=False, indent=4)
|
||||
|
||||
# 🎯 Auto-complétion pour les banques
|
||||
async def autocomplete_banque(self, interaction: discord.Interaction, current: str):
|
||||
return [
|
||||
app_commands.Choice(name=b, value=b)
|
||||
for b in BANQUES_DISPONIBLES if current.lower() in b.lower()
|
||||
]
|
||||
|
||||
# 🎯 Auto-complétion pour les jours
|
||||
async def autocomplete_jour(self, interaction: discord.Interaction, current: str):
|
||||
return [
|
||||
app_commands.Choice(name=day.capitalize(), value=day)
|
||||
for day in JOURS_SEMAINE if current.lower() in day
|
||||
]
|
||||
|
||||
@app_commands.command(name="abonnement", description="Crée un abonnement bancaire hebdomadaire.")
|
||||
@app_commands.describe(
|
||||
nom="Nom de l'abonnement",
|
||||
rib_emetteur="RIB de l'expéditeur",
|
||||
montant="Montant à envoyer",
|
||||
rib_receveur="RIB du bénéficiaire",
|
||||
banque="Nom de la banque",
|
||||
jour="Jour de la semaine du virement"
|
||||
)
|
||||
@app_commands.autocomplete(banque=autocomplete_banque, jour=autocomplete_jour)
|
||||
async def abonnement(
|
||||
self,
|
||||
interaction: discord.Interaction,
|
||||
nom: str,
|
||||
rib_emetteur: str,
|
||||
montant: float,
|
||||
rib_receveur: str,
|
||||
banque: str,
|
||||
jour: str
|
||||
):
|
||||
if jour.lower() not in JOURS_SEMAINE:
|
||||
await interaction.response.send_message("❌ Jour invalide. Choisissez un jour valide de la semaine.", ephemeral=True)
|
||||
return
|
||||
|
||||
abonnement = {
|
||||
"nom": nom,
|
||||
"rib_emetteur": rib_emetteur,
|
||||
"rib_receveur": rib_receveur,
|
||||
"montant": montant,
|
||||
"banque": banque,
|
||||
"jour": jour.lower()
|
||||
}
|
||||
|
||||
self.abonnements.append(abonnement)
|
||||
self.save_abonnements()
|
||||
|
||||
await interaction.response.send_message(f"✅ Abonnement **{nom}** enregistré. Virement automatique tous les **{jour}**.", ephemeral=True)
|
||||
|
||||
@tasks.loop(minutes=60)
|
||||
async def executer_abonnements(self):
|
||||
today = datetime.datetime.now().strftime("%A").lower()
|
||||
try:
|
||||
with open("comptes_bancaires.json", "r", encoding="utf-8") as f:
|
||||
comptes = json.load(f)
|
||||
except:
|
||||
comptes = {}
|
||||
|
||||
updated = False
|
||||
for abonnement in self.abonnements:
|
||||
if abonnement["jour"] != today:
|
||||
continue
|
||||
|
||||
banque = abonnement["banque"]
|
||||
rib_emetteur = abonnement["rib_emetteur"]
|
||||
rib_receveur = abonnement["rib_receveur"]
|
||||
montant = abonnement["montant"]
|
||||
|
||||
destinataire_id = None
|
||||
emetteur_id = None
|
||||
|
||||
for user_id, data in comptes.get(banque, {}).items():
|
||||
if data.get("rib") == rib_emetteur:
|
||||
emetteur_id = user_id
|
||||
if data.get("rib") == rib_receveur:
|
||||
destinataire_id = user_id
|
||||
|
||||
if not emetteur_id or not destinataire_id:
|
||||
continue
|
||||
|
||||
if comptes[banque][emetteur_id]["solde"] < montant:
|
||||
continue
|
||||
|
||||
comptes[banque][emetteur_id]["solde"] -= montant
|
||||
comptes[banque][emetteur_id]["transactions"].append({
|
||||
"date": datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S"),
|
||||
"type": "Virement automatique (abonnement)",
|
||||
"source": f"Vers RIB {rib_receveur}",
|
||||
"montant": -montant
|
||||
})
|
||||
|
||||
comptes[banque][destinataire_id]["solde"] += montant
|
||||
comptes[banque][destinataire_id]["transactions"].append({
|
||||
"date": datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S"),
|
||||
"type": "Virement reçu (abonnement)",
|
||||
"source": f"{rib_emetteur}",
|
||||
"montant": montant
|
||||
})
|
||||
|
||||
updated = True
|
||||
|
||||
if updated:
|
||||
with open("comptes_bancaires.json", 'w', encoding='utf-8') as f:
|
||||
json.dump(comptes, f, ensure_ascii=False, indent=4)
|
||||
|
||||
@executer_abonnements.before_loop
|
||||
async def before_abonnement_loop(self):
|
||||
await self.bot.wait_until_ready()
|
||||
|
||||
async def setup(bot):
|
||||
await bot.add_cog(Abonnement(bot))
|
||||
Loading…
Add table
Add a link
Reference in a new issue