bug fix:
Manager ne pouvait pas voir les tryouts. probleme avec discord bot
This commit is contained in:
+43
-28
@@ -114,21 +114,36 @@ class TeamTryoutsBot(commands.Bot):
|
||||
"""Check if a channel is a DM channel."""
|
||||
return hasattr(channel, 'recipient') or hasattr(channel, 'recipients')
|
||||
|
||||
async def on_reaction_add(self, reaction, user):
|
||||
"""Handle when a reaction is added to a message."""
|
||||
if user.bot:
|
||||
async def on_raw_reaction_add(self, payload):
|
||||
"""Handle when a reaction is added to a message (works even after bot restart)."""
|
||||
# Ignore bot's own reactions
|
||||
if payload.user_id == self.user.id:
|
||||
return
|
||||
|
||||
if not self.is_dm_channel(reaction.message.channel):
|
||||
# Check if this is a pending request we're tracking
|
||||
if payload.message_id not in self.pending_requests:
|
||||
return
|
||||
|
||||
message_id = reaction.message.id
|
||||
|
||||
if message_id not in self.pending_requests:
|
||||
# Fetch the channel and check if it's a DM
|
||||
try:
|
||||
channel = await self.fetch_channel(payload.channel_id)
|
||||
except Exception:
|
||||
return
|
||||
|
||||
request_info = self.pending_requests[message_id]
|
||||
emoji_str = str(reaction.emoji)
|
||||
if not self.is_dm_channel(channel):
|
||||
return
|
||||
|
||||
# Fetch the user who reacted
|
||||
try:
|
||||
user = await self.fetch_user(payload.user_id)
|
||||
except Exception:
|
||||
return
|
||||
|
||||
if user is None:
|
||||
return
|
||||
|
||||
request_info = self.pending_requests[payload.message_id]
|
||||
emoji_str = str(payload.emoji)
|
||||
|
||||
handler_type = request_info.get('type')
|
||||
request_id = request_info.get('id')
|
||||
@@ -137,28 +152,28 @@ class TeamTryoutsBot(commands.Bot):
|
||||
if handler_type == 'one_on_one':
|
||||
if self.flask_app:
|
||||
with self.flask_app.app_context():
|
||||
await self.handle_one_on_one_approve(user, message_id, request_id, reaction.message)
|
||||
await self.handle_one_on_one_approve(user, payload.message_id, request_id, channel)
|
||||
else:
|
||||
await self.handle_one_on_one_approve(user, message_id, request_id, reaction.message)
|
||||
await self.handle_one_on_one_approve(user, payload.message_id, request_id, channel)
|
||||
elif handler_type == 'schedule_addition':
|
||||
if self.flask_app:
|
||||
with self.flask_app.app_context():
|
||||
await self.handle_attendance_confirm(user, message_id, request_id, reaction.message)
|
||||
await self.handle_attendance_confirm(user, payload.message_id, request_id, channel)
|
||||
else:
|
||||
await self.handle_attendance_confirm(user, message_id, request_id, reaction.message)
|
||||
await self.handle_attendance_confirm(user, payload.message_id, request_id, channel)
|
||||
elif emoji_str == CROSS_EMOJI:
|
||||
if handler_type == 'one_on_one':
|
||||
if self.flask_app:
|
||||
with self.flask_app.app_context():
|
||||
await self.handle_one_on_one_reject(user, message_id, request_id, reaction.message)
|
||||
await self.handle_one_on_one_reject(user, payload.message_id, request_id, channel)
|
||||
else:
|
||||
await self.handle_one_on_one_reject(user, message_id, request_id, reaction.message)
|
||||
await self.handle_one_on_one_reject(user, payload.message_id, request_id, channel)
|
||||
elif handler_type == 'schedule_addition':
|
||||
if self.flask_app:
|
||||
with self.flask_app.app_context():
|
||||
await self.handle_attendance_decline(user, message_id, request_id, reaction.message)
|
||||
await self.handle_attendance_decline(user, payload.message_id, request_id, channel)
|
||||
else:
|
||||
await self.handle_attendance_decline(user, message_id, request_id, reaction.message)
|
||||
await self.handle_attendance_decline(user, payload.message_id, request_id, channel)
|
||||
|
||||
async def _send_one_on_one_dm(self, coach_name: str, coach_discord_id: str, player_name: str,
|
||||
team_name: str, date_str: str, start_time: str, end_time: str,
|
||||
@@ -259,7 +274,7 @@ class TeamTryoutsBot(commands.Bot):
|
||||
logger.error(f"Error sending schedule notification: {e}")
|
||||
return None
|
||||
|
||||
async def handle_one_on_one_approve(self, coach, message_id, request_id, original_message):
|
||||
async def handle_one_on_one_approve(self, coach, message_id, request_id, channel):
|
||||
"""Handle coach approving a One on One request."""
|
||||
try:
|
||||
from app.models import OneOnOneRequest
|
||||
@@ -270,7 +285,7 @@ class TeamTryoutsBot(commands.Bot):
|
||||
return
|
||||
|
||||
if request.coach.discord_user_id != str(coach.id):
|
||||
await original_message.channel.send("⚠️ You are not the intended recipient.")
|
||||
await channel.send("⚠️ You are not the intended recipient.")
|
||||
return
|
||||
|
||||
# Re-attach to current session (object may be detached across app contexts)
|
||||
@@ -285,7 +300,7 @@ class TeamTryoutsBot(commands.Bot):
|
||||
request.responded_at = datetime.utcnow()
|
||||
db.session.commit()
|
||||
|
||||
await original_message.channel.send(
|
||||
await channel.send(
|
||||
f"✅ You have **approved** the One on One session with {player_full_name}."
|
||||
)
|
||||
|
||||
@@ -303,7 +318,7 @@ class TeamTryoutsBot(commands.Bot):
|
||||
except Exception as e:
|
||||
logger.error(f"Error handling approval: {e}\n{traceback.format_exc()}")
|
||||
|
||||
async def handle_one_on_one_reject(self, coach, message_id, request_id, original_message):
|
||||
async def handle_one_on_one_reject(self, coach, message_id, request_id, channel):
|
||||
"""Handle coach rejecting a One on One request."""
|
||||
try:
|
||||
from app.models import OneOnOneRequest
|
||||
@@ -314,7 +329,7 @@ class TeamTryoutsBot(commands.Bot):
|
||||
return
|
||||
|
||||
if request.coach.discord_user_id != str(coach.id):
|
||||
await original_message.channel.send("⚠️ You are not the intended recipient.")
|
||||
await channel.send("⚠️ You are not the intended recipient.")
|
||||
return
|
||||
|
||||
# Re-attach to current session (object may be detached across app contexts)
|
||||
@@ -326,7 +341,7 @@ class TeamTryoutsBot(commands.Bot):
|
||||
|
||||
refusal_note = None
|
||||
try:
|
||||
async for reply in original_message.channel.history(limit=20):
|
||||
async for reply in channel.history(limit=20):
|
||||
if reply.author.id == coach.id and reply.reference and reply.reference.message_id == message_id:
|
||||
refusal_note = reply.content
|
||||
break
|
||||
@@ -345,7 +360,7 @@ class TeamTryoutsBot(commands.Bot):
|
||||
else:
|
||||
rejection_msg += "\n\nℹ️ The player has been notified that you are not available."
|
||||
|
||||
await original_message.channel.send(rejection_msg)
|
||||
await channel.send(rejection_msg)
|
||||
if player_discord_id:
|
||||
await self.notify_player_about_one_on_one_direct(
|
||||
player_discord_id=player_discord_id,
|
||||
@@ -360,7 +375,7 @@ class TeamTryoutsBot(commands.Bot):
|
||||
except Exception as e:
|
||||
logger.error(f"Error handling rejection: {e}\n{traceback.format_exc()}")
|
||||
|
||||
async def handle_attendance_confirm(self, player, message_id, reference_id, original_message):
|
||||
async def handle_attendance_confirm(self, player, message_id, reference_id, channel):
|
||||
"""Handle player confirming attendance for a match/tryout."""
|
||||
try:
|
||||
from app.models import MatchParticipant, TryoutRegistration, Match, Tryout
|
||||
@@ -382,13 +397,13 @@ class TeamTryoutsBot(commands.Bot):
|
||||
|
||||
db.session.commit()
|
||||
|
||||
await original_message.channel.send("✅ Your attendance has been confirmed!")
|
||||
await channel.send("✅ Your attendance has been confirmed!")
|
||||
del self.pending_requests[message_id]
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error handling attendance confirmation: {e}\n{traceback.format_exc()}")
|
||||
|
||||
async def handle_attendance_decline(self, player, message_id, reference_id, original_message):
|
||||
async def handle_attendance_decline(self, player, message_id, reference_id, channel):
|
||||
"""Handle player declining attendance for a match/tryout."""
|
||||
try:
|
||||
from app.models import MatchParticipant, TryoutRegistration, Match, Tryout
|
||||
@@ -410,7 +425,7 @@ class TeamTryoutsBot(commands.Bot):
|
||||
|
||||
db.session.commit()
|
||||
|
||||
await original_message.channel.send("❌ Your attendance has been declined.")
|
||||
await channel.send("❌ Your attendance has been declined.")
|
||||
del self.pending_requests[message_id]
|
||||
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user