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

77 lines
3 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, tasks
import json
from datetime import datetime, timedelta
import os
EMS_ROLE_ID = 1278317212675084328 # Remplace par l’ID du rôle EMS # Remplace par l’ID du rôle EMS
ROLE_A_ATTRIBUER_ID = 1367198876548993045 # Remplace par l’ID du rôle visite médicale # Remplace par l’ID du rôle à attribuer
FICHIER_ROLES = "visite_médicale.json"
class AttribuerRole(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.roles_temporaire = self.charger_roles()
self.verification_roles.start()
def charger_roles(self):
if os.path.exists(FICHIER_ROLES):
with open(FICHIER_ROLES, "r", encoding="utf-8") as f:
return json.load(f)
return {}
def sauvegarder_roles(self):
with open(FICHIER_ROLES, "w", encoding="utf-8") as f:
json.dump(self.roles_temporaire, f, indent=4, ensure_ascii=False)
@app_commands.command(name="valider_visite_médicale", description="Attribue un rôle temporaire suite à une visite médicale (EMS uniquement).")
async def valider_visite(self, interaction: discord.Interaction, membre: discord.Member):
if not any(role.id == EMS_ROLE_ID for role in interaction.user.roles):
await interaction.response.send_message("❌ Seuls les EMS peuvent utiliser cette commande.", ephemeral=True)
return
role = interaction.guild.get_role(ROLE_A_ATTRIBUER_ID)
if not role:
await interaction.response.send_message("❌ Rôle à attribuer introuvable.", ephemeral=True)
return
await membre.add_roles(role)
expiration = (datetime.now() + timedelta(days=30)).strftime("%Y-%m-%d %H:%M:%S")
self.roles_temporaire[str(membre.id)] = {
"role_id": role.id,
"expiration": expiration
}
self.sauvegarder_roles()
await interaction.response.send_message(f"✅ Rôle temporaire attribué à {membre.mention} jusqu'au {expiration}.", ephemeral=True)
@tasks.loop(hours=1)
async def verification_roles(self):
now = datetime.now()
to_remove = []
for user_id, data in self.roles_temporaire.items():
expiration = datetime.strptime(data["expiration"], "%Y-%m-%d %H:%M:%S")
if now >= expiration:
to_remove.append(user_id)
for user_id in to_remove:
member = self.bot.get_user(int(user_id)) or await self.bot.fetch_user(int(user_id))
guild = discord.utils.get(self.bot.guilds)
member = guild.get_member(int(user_id))
role = guild.get_role(ROLE_A_ATTRIBUER_ID)
if member and role:
await member.remove_roles(role)
del self.roles_temporaire[user_id]
if to_remove:
self.sauvegarder_roles()
@verification_roles.before_loop
async def before_verification(self):
await self.bot.wait_until_ready()
async def setup(bot):
await bot.add_cog(AttribuerRole(bot))