subscribe update

This commit is contained in:
VinceC
2024-11-24 03:15:04 -06:00
parent 12722b6fd4
commit 1cee965d00
8 changed files with 447 additions and 72 deletions

View File

@@ -5,12 +5,13 @@ const { createMatchRequestEmbed } = require('../utils/embedBuilders');
const { createMatchActionRow } = require('../utils/componentBuilders');
class NotificationService {
constructor(bot) {
this.bot = bot;
this.app = express();
this.app.use(express.json());
this.setupRoutes();
}
constructor(bot, supabaseService) {
this.bot = bot;
this.supabaseService = supabaseService;
this.app = express();
this.app.use(express.json());
this.setupRoutes();
}
setupRoutes() {
this.app.post('/api/match-notification', this.handleMatchNotification.bind(this));
@@ -49,26 +50,25 @@ class NotificationService {
return res.status(400).json({ error: 'Invalid match data' });
}
const notificationChannelId = process.env.NOTIFICATION_CHANNEL_ID;
if (!notificationChannelId) {
throw new Error('Notification channel ID not configured');
const subscriptions = await this.supabaseService.getSubscriptions();
const relevantSubscriptions = subscriptions.filter(sub => sub.game_name === matchData.game_name);
for (const subscription of relevantSubscriptions) {
try {
const channel = await this.bot.client.channels.fetch(subscription.channel_id);
const embed = createMatchRequestEmbed(matchData);
const actionRow = createMatchActionRow(matchData.game_id);
await channel.send({
embeds: [embed],
components: [actionRow]
});
} catch (error) {
console.error(`Error sending notification to channel ${subscription.channel_id}:`, error);
}
}
try {
const channel = await this.bot.client.channels.fetch(notificationChannelId);
const embed = createMatchRequestEmbed(matchData);
const actionRow = createMatchActionRow(matchData.game_id);
await channel.send({
embeds: [embed],
components: [actionRow]
});
res.json({ success: true });
} catch (error) {
console.error('Error fetching or sending to notification channel:', error);
throw new Error('Failed to send match notification');
}
res.json({ success: true });
} catch (error) {
console.error('Error handling match notification:', error);
res.status(500).json({ error: 'Internal server error' });