subscribe update
This commit is contained in:
@@ -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' });
|
||||
|
||||
58
src/services/SupabaseService.js
Normal file
58
src/services/SupabaseService.js
Normal file
@@ -0,0 +1,58 @@
|
||||
const { createClient } = require('@supabase/supabase-js');
|
||||
|
||||
class SupabaseService {
|
||||
constructor() {
|
||||
const supabaseUrl = process.env.SUPABASE_URL;
|
||||
const supabaseKey = process.env.SUPABASE_KEY;
|
||||
|
||||
if (!supabaseUrl || !supabaseKey) {
|
||||
console.error('Supabase URL or key is missing. Please check your .env file.');
|
||||
this.supabase = null;
|
||||
} else {
|
||||
this.supabase = createClient(supabaseUrl, supabaseKey);
|
||||
}
|
||||
}
|
||||
|
||||
async getSubscriptions(guildId) {
|
||||
if (!this.supabase) {
|
||||
console.error('Supabase client is not initialized.');
|
||||
return [];
|
||||
}
|
||||
const { data, error } = await this.supabase
|
||||
.from('subscriptions')
|
||||
.select('*')
|
||||
.eq('guild_id', guildId);
|
||||
|
||||
if (error) throw error;
|
||||
return data;
|
||||
}
|
||||
|
||||
async addSubscription(guildId, gameName, channelId) {
|
||||
if (!this.supabase) {
|
||||
console.error('Supabase client is not initialized.');
|
||||
return null;
|
||||
}
|
||||
const { data, error } = await this.supabase
|
||||
.from('subscriptions')
|
||||
.insert({ guild_id: guildId, game_name: gameName, channel_id: channelId });
|
||||
|
||||
if (error) throw error;
|
||||
return data;
|
||||
}
|
||||
|
||||
async removeSubscription(guildId, gameName) {
|
||||
if (!this.supabase) {
|
||||
console.error('Supabase client is not initialized.');
|
||||
return null;
|
||||
}
|
||||
const { data, error } = await this.supabase
|
||||
.from('subscriptions')
|
||||
.delete()
|
||||
.match({ guild_id: guildId, game_name: gameName });
|
||||
|
||||
if (error) throw error;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = SupabaseService;
|
||||
Reference in New Issue
Block a user