régler problème avec mise a jour des status de message du discord bot
This commit is contained in:
@@ -7,6 +7,7 @@ This module provides a persistent bot that handles:
|
||||
"""
|
||||
|
||||
import os
|
||||
import json
|
||||
import logging
|
||||
import asyncio
|
||||
import threading
|
||||
@@ -26,6 +27,9 @@ DISCORD_BOT_TOKEN = os.getenv('DISCORD_BOT_TOKEN')
|
||||
# Configure logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# File for persisting pending requests across bot restarts
|
||||
PENDING_FILE = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'discord_pending.json')
|
||||
|
||||
# Emoji constants
|
||||
CHECK_EMOJI = '✅' # Green checkmark
|
||||
CROSS_EMOJI = '❌' # Red X
|
||||
@@ -53,8 +57,31 @@ class TeamTryoutsBot(commands.Bot):
|
||||
self.scheduler = AsyncIOScheduler()
|
||||
self.timezone = ZoneInfo('America/Toronto') # EDT timezone
|
||||
|
||||
def _load_pending(self):
|
||||
"""Load pending requests from the JSON file."""
|
||||
try:
|
||||
if os.path.exists(PENDING_FILE):
|
||||
with open(PENDING_FILE, 'r') as f:
|
||||
data = json.load(f)
|
||||
# Convert string keys back to int
|
||||
self.pending_requests = {int(k): v for k, v in data.items()}
|
||||
logger.info(f"Loaded {len(self.pending_requests)} pending requests from {PENDING_FILE}")
|
||||
else:
|
||||
logger.info("No pending requests file found, starting fresh.")
|
||||
except Exception as e:
|
||||
logger.error(f"Error loading pending requests: {e}")
|
||||
|
||||
def _save_pending(self):
|
||||
"""Save pending requests to the JSON file."""
|
||||
try:
|
||||
with open(PENDING_FILE, 'w') as f:
|
||||
json.dump(self.pending_requests, f, indent=2)
|
||||
except Exception as e:
|
||||
logger.error(f"Error saving pending requests: {e}")
|
||||
|
||||
async def setup_hook(self):
|
||||
"""Called when the bot is ready."""
|
||||
self._load_pending()
|
||||
logger.info(f'TeamTryoutsBot logged in as {self.user}')
|
||||
|
||||
async def on_ready(self):
|
||||
@@ -208,6 +235,7 @@ class TeamTryoutsBot(commands.Bot):
|
||||
|
||||
# Track this pending request
|
||||
self.pending_requests[msg.id] = {'type': 'one_on_one', 'id': request_id}
|
||||
self._save_pending()
|
||||
|
||||
logger.info(f"Sent One on One DM with reactions, message_id={msg.id}")
|
||||
return msg.id
|
||||
@@ -266,6 +294,7 @@ class TeamTryoutsBot(commands.Bot):
|
||||
|
||||
# Track this pending request
|
||||
self.pending_requests[msg.id] = {'type': 'schedule_addition', 'id': reference_id, 'event_type': event_type}
|
||||
self._save_pending()
|
||||
|
||||
logger.info(f"Sent {event_type} schedule notification to {db_user.username}, message_id={msg.id}")
|
||||
return msg.id
|
||||
@@ -314,6 +343,7 @@ class TeamTryoutsBot(commands.Bot):
|
||||
approved=True
|
||||
)
|
||||
del self.pending_requests[message_id]
|
||||
self._save_pending()
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error handling approval: {e}\n{traceback.format_exc()}")
|
||||
@@ -371,6 +401,7 @@ class TeamTryoutsBot(commands.Bot):
|
||||
refusal_note=refusal_note
|
||||
)
|
||||
del self.pending_requests[message_id]
|
||||
self._save_pending()
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error handling rejection: {e}\n{traceback.format_exc()}")
|
||||
@@ -399,6 +430,7 @@ class TeamTryoutsBot(commands.Bot):
|
||||
|
||||
await channel.send("✅ Your attendance has been confirmed!")
|
||||
del self.pending_requests[message_id]
|
||||
self._save_pending()
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error handling attendance confirmation: {e}\n{traceback.format_exc()}")
|
||||
@@ -427,6 +459,7 @@ class TeamTryoutsBot(commands.Bot):
|
||||
|
||||
await channel.send("❌ Your attendance has been declined.")
|
||||
del self.pending_requests[message_id]
|
||||
self._save_pending()
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error handling attendance decline: {e}\n{traceback.format_exc()}")
|
||||
|
||||
Reference in New Issue
Block a user