subscribe update
This commit is contained in:
121
src/Bot.js
121
src/Bot.js
@@ -1,8 +1,10 @@
|
||||
const { Client, GatewayIntentBits } = require('discord.js');
|
||||
const CommandHandler = require('./commands/CommandHandler.js');
|
||||
const PlayerService = require('./services/PlayerService.js');
|
||||
const NotificationService = require('./services/NotificationService.js');
|
||||
const MatchCommands = require('./commands/MatchCommands.js');
|
||||
const { Client, GatewayIntentBits } = require("discord.js");
|
||||
const CommandHandler = require("./commands/CommandHandler.js");
|
||||
const PlayerService = require("./services/PlayerService.js");
|
||||
const NotificationService = require("./services/NotificationService.js");
|
||||
const MatchCommands = require("./commands/MatchCommands.js");
|
||||
const SupabaseService = require("./services/SupabaseService.js");
|
||||
const SubscriptionCommands = require("./commands/SubscriptionCommands.js");
|
||||
|
||||
class Bot {
|
||||
constructor() {
|
||||
@@ -13,12 +15,20 @@ class Bot {
|
||||
GatewayIntentBits.MessageContent,
|
||||
],
|
||||
});
|
||||
|
||||
|
||||
this.playerService = new PlayerService();
|
||||
this.commandHandler = new CommandHandler(this.playerService);
|
||||
this.matchCommands = new MatchCommands(this.playerService);
|
||||
this.notificationService = new NotificationService(this);
|
||||
|
||||
|
||||
this.supabaseService = new SupabaseService();
|
||||
if (this.supabaseService.supabase) {
|
||||
this.subscriptionCommands = new SubscriptionCommands(this.supabaseService);
|
||||
} else {
|
||||
console.warn('SupabaseService not initialized. Subscription commands will be unavailable.');
|
||||
this.subscriptionCommands = null;
|
||||
}
|
||||
|
||||
this.setupEventHandlers();
|
||||
}
|
||||
|
||||
@@ -29,35 +39,35 @@ class Bot {
|
||||
}
|
||||
|
||||
async start(token) {
|
||||
console.log('Starting bot...');
|
||||
console.log("Starting bot...");
|
||||
try {
|
||||
await this.client.login(token);
|
||||
console.log('Login successful');
|
||||
|
||||
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('Startup failed:', error);
|
||||
console.error("Startup failed:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async stop() {
|
||||
console.log('Stopping bot...');
|
||||
console.log("Stopping bot...");
|
||||
try {
|
||||
await this.notificationService.stop();
|
||||
await this.client.destroy();
|
||||
console.log('Bot stopped successfully');
|
||||
console.log("Bot stopped successfully");
|
||||
} catch (error) {
|
||||
console.error('Error stopping bot:', error);
|
||||
console.error("Error stopping bot:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
handleError(error) {
|
||||
console.error('Discord client error:', error);
|
||||
console.error("Discord client error:", error);
|
||||
}
|
||||
|
||||
handleReady() {
|
||||
@@ -72,94 +82,115 @@ class Bot {
|
||||
await this.handleButton(interaction);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error handling interaction:', error);
|
||||
this.handleInteractionError(interaction, error);
|
||||
console.error("Error handling interaction:", error);
|
||||
await this.handleInteractionError(interaction, error);
|
||||
}
|
||||
}
|
||||
|
||||
async handleCommand(interaction) {
|
||||
try {
|
||||
// Defer the reply immediately for all commands
|
||||
await interaction.deferReply({ ephemeral: true });
|
||||
|
||||
switch (interaction.commandName) {
|
||||
case 'ping':
|
||||
case "ping":
|
||||
await this.commandHandler.handlePing(interaction);
|
||||
break;
|
||||
case 'finduser':
|
||||
case "finduser":
|
||||
await this.commandHandler.handleFindUser(interaction);
|
||||
break;
|
||||
case 'matchhistory':
|
||||
case "matchhistory":
|
||||
await this.commandHandler.handleMatchHistory(interaction);
|
||||
break;
|
||||
case "subscribe":
|
||||
if (this.subscriptionCommands) {
|
||||
await this.subscriptionCommands.handleSubscribe(interaction);
|
||||
} else {
|
||||
await interaction.editReply("Subscription commands are not available.");
|
||||
}
|
||||
break;
|
||||
case "unsubscribe":
|
||||
if (this.subscriptionCommands) {
|
||||
await this.subscriptionCommands.handleUnsubscribe(interaction);
|
||||
} else {
|
||||
await interaction.editReply("Subscription commands are not available.");
|
||||
}
|
||||
break;
|
||||
case "listsubscriptions":
|
||||
if (this.subscriptionCommands) {
|
||||
await this.subscriptionCommands.handleListSubscriptions(interaction);
|
||||
} else {
|
||||
await interaction.editReply("Subscription commands are not available.");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
await interaction.reply({
|
||||
content: 'Unknown command',
|
||||
ephemeral: true
|
||||
});
|
||||
await interaction.editReply("Unknown command");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Command error:', error);
|
||||
await this.handleCommandError(interaction, error);
|
||||
}
|
||||
}
|
||||
|
||||
async handleButton(interaction) {
|
||||
const [action, matchId] = interaction.customId.split('_match_');
|
||||
|
||||
const [action, matchId] = interaction.customId.split("_match_");
|
||||
|
||||
try {
|
||||
switch (action) {
|
||||
case 'accept':
|
||||
case "accept":
|
||||
await this.matchCommands.handleMatchAccept(interaction, matchId);
|
||||
break;
|
||||
case 'view':
|
||||
case "view":
|
||||
await this.matchCommands.handleViewDetails(interaction, matchId);
|
||||
break;
|
||||
default:
|
||||
await interaction.reply({
|
||||
content: 'Unknown button action',
|
||||
ephemeral: true
|
||||
content: "Unknown button action",
|
||||
ephemeral: true,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error handling button:', error);
|
||||
console.error("Error handling button:", error);
|
||||
await this.handleInteractionError(interaction, error);
|
||||
}
|
||||
}
|
||||
|
||||
async handleCommandError(interaction, error) {
|
||||
console.error('Command error:', error);
|
||||
|
||||
console.error("Command error:", error);
|
||||
|
||||
if (error.code === 10062) {
|
||||
console.log('Interaction expired');
|
||||
console.log("Interaction expired");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
const message = 'An error occurred while processing your command.';
|
||||
const message = "An error occurred while processing your command.";
|
||||
if (interaction.deferred) {
|
||||
await interaction.editReply({ content: message, ephemeral: true });
|
||||
await interaction.editReply({ content: message });
|
||||
} else if (!interaction.replied) {
|
||||
await interaction.reply({ content: message, ephemeral: true });
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Error while sending error message:', e);
|
||||
console.error("Error while sending error message:", e);
|
||||
}
|
||||
}
|
||||
|
||||
async handleInteractionError(interaction, error) {
|
||||
console.error('Interaction error:', 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
|
||||
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
|
||||
content: "An error occurred while processing your request.",
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Error while sending error message:', e);
|
||||
console.error("Error while sending error message:", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user