Files
advanced-discord-bot
Code
73 5,832
advanced-discord-bot/src/index.js
require('dotenv').config();
const { Client, GatewayIntentBits, Partials, Collection } = require('discord.js');
const { Logger } = require('./utils/logger');
const { Database } = require('./utils/database');
const { Redis } = require('./utils/redis');
const { LanguageManager } = require('./utils/language');
const { PluginManager } = require('./utils/pluginManager');
const { EventLoader } = require('./utils/eventLoader');
const { CommandLoader } = require('./utils/commandLoader');
const { SecurityManager } = require('./utils/security');
const { PerformanceMonitor } = require('./utils/performance');
const path = require('path');
const fs = require('fs');
class AdvancedBot {
constructor() {
this.client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildModeration,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildIntegrations,
GatewayIntentBits.GuildWebhooks,
GatewayIntentBits.GuildInvites,
GatewayIntentBits.GuildScheduledEvents,
GatewayIntentBits.AutoModerationConfiguration,
GatewayIntentBits.AutoModerationExecution,
],
partials: [
Partials.Message,
Partials.Channel,
Partials.Reaction,
Partials.User,
Partials.GuildMember,
Partials.ThreadMember,
]
});
this.client.commands = new Collection();
this.client.slashCommands = new Collection();
this.client.cooldowns = new Collection();
this.client.premium = new Collection();
this.client.tempChannels = new Collection();
this.client.levelingCache = new Collection();
this.client.economyCache = new Collection();
this.client.giveaways = new Map();
this.client.pollTimers = new Map();
this.client.reminderTimers = new Map();
this.client.voiceConnections = new Map();
this.client.queues = new Map();
this.logger = new Logger();
this.db = new Database();
this.redis = new Redis();
this.language = new LanguageManager();
this.pluginManager = new PluginManager(this);
this.security = new SecurityManager(this);
this.performance = new PerformanceMonitor(this);
this.startTime = Date.now();
}
async initialize() {
this.logger.info('Initializing Advanced Discord Bot...');
// Connect to database
await this.db.initialize();
this.logger.info('Database connected.');
// Connect to Redis
await this.redis.initialize();
this.logger.info('Redis connected.');
// Load languages
await this.language.initialize();
this.logger.info('Languages loaded.');
// Load events
const eventLoader = new EventLoader(this);
await eventLoader.loadEvents();
this.logger.info('Events loaded.');
// Load slash commands
const commandLoader = new CommandLoader(this);
await commandLoader.loadCommands();
this.logger.info('Slash commands loaded.');
// Load plugins
await this.pluginManager.loadPlugins();
this.logger.info('Plugins loaded.');
// Initialize security
await this.security.initialize();
this.logger.info('Security initialized.');
// Start performance monitoring
this.performance.startMonitoring();
this.logger.info('Performance monitoring started.');
// Login to Discord
await this.client.login(process.env.DISCORD_TOKEN);
this.logger.info(`Bot logged in as ${this.client.user.tag}`);
}
async shutdown() {
this.logger.info('Shutting down bot...');
// Save all caches to database
await this.saveAllCaches();
// Cleanup voice connections
for (const [guildId, connection] of this.client.voiceConnections) {
connection.destroy();
}
// Clear all timers
for (const timer of this.client.pollTimers.values()) clearTimeout(timer);
for (const timer of this.client.reminderTimers.values()) clearTimeout(timer);
// Disconnect services
await this.redis.disconnect();
await this.db.close();
this.client.destroy();
this.logger.info('Bot shutdown complete.');
process.exit(0);
}
async saveAllCaches() {
// Save leveling cache
// Save economy cache
// Save premium cache
}
}
const bot = new AdvancedBot();
process.on('SIGINT', () => bot.shutdown());
process.on('SIGTERM', () => bot.shutdown());
process.on('unhandledRejection', (error) => {
bot.logger.error('Unhandled rejection:', error);
});
process.on('uncaughtException', (error) => {
bot.logger.error('Uncaught exception:', error);
});
bot.initialize().catch(error => {
bot.logger.error('Failed to initialize bot:', error);
process.exit(1);
});
module.exports = bot;