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 os
|
||||||
|
import json
|
||||||
import logging
|
import logging
|
||||||
import asyncio
|
import asyncio
|
||||||
import threading
|
import threading
|
||||||
@@ -26,6 +27,9 @@ DISCORD_BOT_TOKEN = os.getenv('DISCORD_BOT_TOKEN')
|
|||||||
# Configure logging
|
# Configure logging
|
||||||
logger = logging.getLogger(__name__)
|
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
|
# Emoji constants
|
||||||
CHECK_EMOJI = '✅' # Green checkmark
|
CHECK_EMOJI = '✅' # Green checkmark
|
||||||
CROSS_EMOJI = '❌' # Red X
|
CROSS_EMOJI = '❌' # Red X
|
||||||
@@ -53,8 +57,31 @@ class TeamTryoutsBot(commands.Bot):
|
|||||||
self.scheduler = AsyncIOScheduler()
|
self.scheduler = AsyncIOScheduler()
|
||||||
self.timezone = ZoneInfo('America/Toronto') # EDT timezone
|
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):
|
async def setup_hook(self):
|
||||||
"""Called when the bot is ready."""
|
"""Called when the bot is ready."""
|
||||||
|
self._load_pending()
|
||||||
logger.info(f'TeamTryoutsBot logged in as {self.user}')
|
logger.info(f'TeamTryoutsBot logged in as {self.user}')
|
||||||
|
|
||||||
async def on_ready(self):
|
async def on_ready(self):
|
||||||
@@ -208,6 +235,7 @@ class TeamTryoutsBot(commands.Bot):
|
|||||||
|
|
||||||
# Track this pending request
|
# Track this pending request
|
||||||
self.pending_requests[msg.id] = {'type': 'one_on_one', 'id': request_id}
|
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}")
|
logger.info(f"Sent One on One DM with reactions, message_id={msg.id}")
|
||||||
return msg.id
|
return msg.id
|
||||||
@@ -266,6 +294,7 @@ class TeamTryoutsBot(commands.Bot):
|
|||||||
|
|
||||||
# Track this pending request
|
# Track this pending request
|
||||||
self.pending_requests[msg.id] = {'type': 'schedule_addition', 'id': reference_id, 'event_type': event_type}
|
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}")
|
logger.info(f"Sent {event_type} schedule notification to {db_user.username}, message_id={msg.id}")
|
||||||
return msg.id
|
return msg.id
|
||||||
@@ -314,6 +343,7 @@ class TeamTryoutsBot(commands.Bot):
|
|||||||
approved=True
|
approved=True
|
||||||
)
|
)
|
||||||
del self.pending_requests[message_id]
|
del self.pending_requests[message_id]
|
||||||
|
self._save_pending()
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error handling approval: {e}\n{traceback.format_exc()}")
|
logger.error(f"Error handling approval: {e}\n{traceback.format_exc()}")
|
||||||
@@ -371,6 +401,7 @@ class TeamTryoutsBot(commands.Bot):
|
|||||||
refusal_note=refusal_note
|
refusal_note=refusal_note
|
||||||
)
|
)
|
||||||
del self.pending_requests[message_id]
|
del self.pending_requests[message_id]
|
||||||
|
self._save_pending()
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error handling rejection: {e}\n{traceback.format_exc()}")
|
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!")
|
await channel.send("✅ Your attendance has been confirmed!")
|
||||||
del self.pending_requests[message_id]
|
del self.pending_requests[message_id]
|
||||||
|
self._save_pending()
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error handling attendance confirmation: {e}\n{traceback.format_exc()}")
|
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.")
|
await channel.send("❌ Your attendance has been declined.")
|
||||||
del self.pending_requests[message_id]
|
del self.pending_requests[message_id]
|
||||||
|
self._save_pending()
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error handling attendance decline: {e}\n{traceback.format_exc()}")
|
logger.error(f"Error handling attendance decline: {e}\n{traceback.format_exc()}")
|
||||||
|
|||||||
@@ -374,6 +374,20 @@
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
<!-- Player self-presence toggle -->
|
||||||
|
{% if not can_edit and item.player_presence %}
|
||||||
|
{% for pp in item.player_presence %}
|
||||||
|
{% if pp.player_id == current_user.id %}
|
||||||
|
<div class="presence-players" style="margin-top:6px;">
|
||||||
|
<button class="btn btn-xs presence-toggle-btn {% if pp.attendance_confirmed %}presence-confirmed-btn{% else %}presence-pending-btn{% endif %}"
|
||||||
|
title="Toggle your attendance"
|
||||||
|
onclick="toggleTryoutPresence({{ m.id }}, {{ pp.participant_id }}, this)">
|
||||||
|
Me {% if pp.attendance_confirmed %}✅{% else %}⏳{% endif %}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
{% endfor %}
|
||||||
|
{% endif %}
|
||||||
{% else %}
|
{% else %}
|
||||||
<span class="text-muted">—</span>
|
<span class="text-muted">—</span>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
{}
|
||||||
Reference in New Issue
Block a user