Files
SkrzynkaImpostora/bot/backend/index.js
2025-07-21 00:45:28 +02:00

114 lines
3.7 KiB
JavaScript

const { Client, GatewayIntentBits, REST, Routes, EmbedBuilder } = require('discord.js');
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const morgan = require('morgan');
const compression = require('compression');
require('dotenv').config();
const { DatabaseManager } = require('./database/DatabaseManager');
const { SlashCommands } = require('./commands');
const { WebPanel } = require('./web/server');
class SkrzynkaImpostoraBot {
constructor() {
this.client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers
]
});
this.db = new DatabaseManager();
this.commands = new SlashCommands(this.db);
this.webPanel = new WebPanel(this.db);
this.setupEventHandlers();
}
setupEventHandlers() {
this.client.once('ready', () => {
console.log(`✅ Bot zalogowany jako ${this.client.user.tag}`);
this.client.user.setActivity('Zarządzanie wiadomościami powiatalnymi', { type: 'WATCHING' });
});
this.client.on('guildCreate', async (guild) => {
console.log(`Dodano do nowego serwera: ${guild.name} (${guild.id})`);
await this.db.addGuild(guild.id, guild.name);
});
this.client.on('interactionCreate', async (interaction) => {
if (!interaction.isChatInputCommand()) return;
try {
await this.commands.handleCommand(interaction);
} catch (error) {
console.error('Błąd podczas wykonywania komendy:', error);
const errorEmbed = new EmbedBuilder()
.setColor(0xFF0000)
.setTitle('❌ Błąd')
.setDescription('Wystąpił błąd podczas wykonywania komendy.')
.setTimestamp();
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ embeds: [errorEmbed], ephemeral: true });
} else {
await interaction.reply({ embeds: [errorEmbed], ephemeral: true });
}
}
});
this.client.on('error', console.error);
}
async start() {
try {
// Inicjalizacja bazy danych
await this.db.initialize();
console.log('✅ Baza danych zainicjalizowana');
// Uruchomienie panelu web
await this.webPanel.start();
console.log('✅ Panel web uruchomiony');
// Logowanie bota
await this.client.login(process.env.DISCORD_TOKEN);
console.log('✅ Bot Discord uruchomiony');
} catch (error) {
console.error('❌ Błąd podczas uruchamiania bota:', error);
process.exit(1);
}
}
async deployCommands() {
const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN);
try {
console.log('Rozpoczęto odświeżanie komend slash...');
const commands = this.commands.getCommandsData();
await rest.put(
Routes.applicationCommands(process.env.DISCORD_CLIENT_ID),
{ body: commands }
);
console.log('✅ Komendy slash zostały pomyślnie odświeżone.');
} catch (error) {
console.error('❌ Błąd podczas odświeżania komend:', error);
}
}
}
// Uruchomienie bota
if (require.main === module) {
const bot = new SkrzynkaImpostoraBot();
bot.start();
}
module.exports = { SkrzynkaImpostoraBot };