From 3651537c1feaafea781955a356f7e66b252033f6 Mon Sep 17 00:00:00 2001 From: VinceC <33974776+VinceC3@users.noreply.github.com> Date: Fri, 29 Nov 2024 04:27:09 -0600 Subject: [PATCH] feat: update match request embed format and notification handling --- src/services/NotificationService.js | 43 +++++++++++++++++++++++++++-- src/utils/embedBuilders.js | 1 + 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/services/NotificationService.js b/src/services/NotificationService.js index 0708aa2..ff1e1ee 100644 --- a/src/services/NotificationService.js +++ b/src/services/NotificationService.js @@ -49,6 +49,8 @@ class NotificationService { return res.status(400).json({ error: 'Invalid match data' }); } + matchData.match_date = `${matchData.match_date}T${matchData.match_time}Z`; + const { data: subscriptions, error } = await this.supabase .from('active_subscriptions') .select('*') @@ -59,9 +61,22 @@ class NotificationService { return res.status(500).json({ error: 'Failed to fetch subscriptions' }); } + const results = []; for (const subscription of (subscriptions || [])) { try { const channel = await this.bot.client.channels.fetch(subscription.notification_channel_id); + + // Check if bot has permissions to send messages in this channel + if (!channel.permissionsFor(this.bot.client.user).has(['SendMessages', 'ViewChannel'])) { + console.warn(`Missing permissions for channel ${subscription.notification_channel_id} in server ${channel.guild.name}`); + results.push({ + channelId: subscription.notification_channel_id, + status: 'failed', + error: 'Missing permissions' + }); + continue; + } + const embed = createMatchRequestEmbed(matchData); const actionRow = createMatchActionRow(matchData.game_name); @@ -69,12 +84,32 @@ class NotificationService { embeds: [embed], components: [actionRow] }); + + results.push({ + channelId: subscription.notification_channel_id, + status: 'success' + }); + } catch (error) { console.error(`Error sending notification to channel ${subscription.notification_channel_id}:`, error); + results.push({ + channelId: subscription.notification_channel_id, + status: 'failed', + error: error.message + }); } } - res.json({ success: true }); + res.json({ + success: true, + results, + summary: { + total: results.length, + successful: results.filter(r => r.status === 'success').length, + failed: results.filter(r => r.status === 'failed').length + } + }); + } catch (error) { console.error('Error handling match notification:', error); res.status(500).json({ error: 'Internal server error' }); @@ -89,12 +124,14 @@ class NotificationService { const requiredFields = [ 'game_name', 'game_id', + 'created_by_user_id', + 'status', 'team_size', 'match_type', 'region', 'match_date', - 'match_class', - 'status' + 'match_time', + 'match_class' ]; return requiredFields.every(field => matchData[field] !== undefined); diff --git a/src/utils/embedBuilders.js b/src/utils/embedBuilders.js index 1f66f38..48b43ed 100644 --- a/src/utils/embedBuilders.js +++ b/src/utils/embedBuilders.js @@ -147,6 +147,7 @@ class EmbedBuilders { } static createMatchRequestEmbed(matchData) { + // Parse the match date from the provided format const matchDateTime = new Date(matchData.match_date); const formattedDateTime = matchDateTime.toLocaleString();