November Update

A variety of bot updates, verify setup, and match request info
This commit is contained in:
VinceC
2024-11-23 22:25:10 -06:00
parent bb06fb7d9b
commit 530ecc8e17
11 changed files with 1984 additions and 8 deletions

View File

@@ -0,0 +1,98 @@
// src/services/NotificationService.js
const express = require('express');
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();
}
setupRoutes() {
this.app.post('/api/match-notification', this.handleMatchNotification.bind(this));
}
async start(port = 3000) {
return new Promise((resolve, reject) => {
try {
this.server = this.app.listen(port, () => {
console.log(`Notification service listening on port ${port}`);
resolve();
});
} catch (error) {
reject(error);
}
});
}
async stop() {
if (this.server) {
return new Promise((resolve) => {
this.server.close(resolve);
});
}
}
async handleMatchNotification(req, res) {
try {
const authToken = req.headers['x-webhook-token'];
if (authToken !== process.env.WEBHOOK_SECRET) {
return res.status(401).json({ error: 'Unauthorized' });
}
const matchData = req.body;
if (!this.validateMatchData(matchData)) {
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');
}
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');
}
} catch (error) {
console.error('Error handling match notification:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
validateMatchData(matchData) {
if (matchData.type !== 'match_request') {
return false;
}
const requiredFields = [
'game_name',
'game_id',
'team_size',
'match_type',
'region',
'match_date',
'match_class',
'status'
];
return requiredFields.every(field => matchData[field] !== undefined);
}
}
module.exports = NotificationService;