Files
pixelpoebel/src/index.ts
coding d546035bb7
Some checks failed
Auto Build and Push Docker Image / build (push) Failing after 14s
Optimize database, implement automatic command deployment, and improve project-wide type safety
2026-04-29 19:50:50 +02:00

72 lines
2.6 KiB
TypeScript

import 'dotenv/config';
import { ExtendedClient } from './structures/ExtendedClient.js';
import { readdirSync } from 'node:fs';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { dirname, join } from 'node:path';
import { Command } from './structures/Command.js';
import { DB } from './structures/Database.js';
import { TwitchManager } from './structures/TwitchManager.js';
import { ReminderManager } from './structures/ReminderManager.js';
import { Deployer } from './structures/Deployer.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Initialize Database
DB.init();
const client = new ExtendedClient();
// Load Commands
const commandsPath = join(__dirname, 'commands');
const commandFolders = readdirSync(commandsPath);
const commandData: any[] = [];
for (const folder of commandFolders) {
const folderPath = join(commandsPath, folder);
const commandFiles = readdirSync(folderPath).filter(file => file.endsWith('.ts') || file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = join(folderPath, file);
const fileUrl = pathToFileURL(filePath).href;
const command: Command = (await import(fileUrl)).default;
if (command && 'data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
commandData.push(command.data.toJSON());
console.log(`[COMMAND] Loaded: ${command.data.name}`);
} else {
console.warn(`[COMMAND] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
// Auto-deploy commands if configured (default: true)
if (process.env.AUTO_DEPLOY !== 'false' && process.env.CLIENT_ID && process.env.DISCORD_TOKEN) {
await Deployer.deployCommands(process.env.CLIENT_ID, process.env.DISCORD_TOKEN, commandData);
}
// Load Events
const eventsPath = join(__dirname, 'events');
const eventFiles = readdirSync(eventsPath).filter(file => file.endsWith('.ts') || file.endsWith('.js'));
for (const file of eventFiles) {
const filePath = join(eventsPath, file);
const fileUrl = pathToFileURL(filePath).href;
const event = (await import(fileUrl)).default;
if (event.once) {
client.once(event.name, (...args) => event.execute(...args, client));
} else {
client.on(event.name, (...args) => event.execute(...args, client));
}
console.log(`[EVENT] Loaded: ${event.name}`);
}
// Start Twitch Polling
TwitchManager.startPolling(client);
// Start Reminder Polling
ReminderManager.startPolling(client);
client.login(process.env.DISCORD_TOKEN);