November Update
A variety of bot updates, verify setup, and match request info
This commit is contained in:
86
src/Bot.js
86
src/Bot.js
@@ -1,8 +1,9 @@
|
||||
// src/bot.js
|
||||
const { Client, GatewayIntentBits } = require('discord.js');
|
||||
// src/Bot.js - Update your import statements
|
||||
const CommandHandler = require('./commands/CommandHandler.js'); // Add .js extension
|
||||
const PlayerService = require('./services/PlayerService.js'); // Add .js extension
|
||||
const CommandHandler = require('./commands/CommandHandler.js');
|
||||
const PlayerService = require('./services/PlayerService.js');
|
||||
const NotificationService = require('./services/NotificationService.js');
|
||||
const MatchCommands = require('./commands/MatchCommands.js');
|
||||
|
||||
class Bot {
|
||||
constructor() {
|
||||
this.client = new Client({
|
||||
@@ -15,6 +16,8 @@ class Bot {
|
||||
|
||||
this.playerService = new PlayerService();
|
||||
this.commandHandler = new CommandHandler(this.playerService);
|
||||
this.matchCommands = new MatchCommands(this.playerService);
|
||||
this.notificationService = new NotificationService(this);
|
||||
|
||||
this.setupEventHandlers();
|
||||
}
|
||||
@@ -30,8 +33,25 @@ class Bot {
|
||||
try {
|
||||
await this.client.login(token);
|
||||
console.log('Login successful');
|
||||
|
||||
// Start notification service after bot is logged in
|
||||
const port = process.env.NOTIFICATION_PORT || 3000;
|
||||
await this.notificationService.start(port);
|
||||
console.log(`Notification service started on port ${port}`);
|
||||
} catch (error) {
|
||||
console.error('Login failed:', error);
|
||||
console.error('Startup failed:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async stop() {
|
||||
console.log('Stopping bot...');
|
||||
try {
|
||||
await this.notificationService.stop();
|
||||
await this.client.destroy();
|
||||
console.log('Bot stopped successfully');
|
||||
} catch (error) {
|
||||
console.error('Error stopping bot:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -45,8 +65,19 @@ class Bot {
|
||||
}
|
||||
|
||||
async handleInteraction(interaction) {
|
||||
if (!interaction.isCommand()) return;
|
||||
try {
|
||||
if (interaction.isCommand()) {
|
||||
await this.handleCommand(interaction);
|
||||
} else if (interaction.isButton()) {
|
||||
await this.handleButton(interaction);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error handling interaction:', error);
|
||||
this.handleInteractionError(interaction, error);
|
||||
}
|
||||
}
|
||||
|
||||
async handleCommand(interaction) {
|
||||
try {
|
||||
switch (interaction.commandName) {
|
||||
case 'ping':
|
||||
@@ -69,6 +100,29 @@ class Bot {
|
||||
}
|
||||
}
|
||||
|
||||
async handleButton(interaction) {
|
||||
const [action, matchId] = interaction.customId.split('_match_');
|
||||
|
||||
try {
|
||||
switch (action) {
|
||||
case 'accept':
|
||||
await this.matchCommands.handleMatchAccept(interaction, matchId);
|
||||
break;
|
||||
case 'view':
|
||||
await this.matchCommands.handleViewDetails(interaction, matchId);
|
||||
break;
|
||||
default:
|
||||
await interaction.reply({
|
||||
content: 'Unknown button action',
|
||||
ephemeral: true
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error handling button:', error);
|
||||
await this.handleInteractionError(interaction, error);
|
||||
}
|
||||
}
|
||||
|
||||
async handleCommandError(interaction, error) {
|
||||
console.error('Command error:', error);
|
||||
|
||||
@@ -88,6 +142,26 @@ class Bot {
|
||||
console.error('Error while sending error message:', e);
|
||||
}
|
||||
}
|
||||
|
||||
async handleInteractionError(interaction, error) {
|
||||
console.error('Interaction error:', error);
|
||||
|
||||
try {
|
||||
if (!interaction.replied && !interaction.deferred) {
|
||||
await interaction.reply({
|
||||
content: 'An error occurred while processing your request.',
|
||||
ephemeral: true
|
||||
});
|
||||
} else if (interaction.deferred) {
|
||||
await interaction.editReply({
|
||||
content: 'An error occurred while processing your request.',
|
||||
ephemeral: true
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Error while sending error message:', e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Bot;
|
||||
Reference in New Issue
Block a user