Files
BluBot/index.js
2025-06-21 15:13:58 -05:00

74 lines
2.4 KiB
JavaScript

const fs = require('node:fs');
const path = require('node:path')
const {Client, Events, GatewayIntentBits, Collection, InteractionResponse } = require('discord.js');
require('dotenv').config();
const { connectDB, closeDB } = require('./db')
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildModeration, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.GuildPresences ] });
client.commands= new Collection();
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
function loadEvents(dir, client) {
const files = fs.readdirSync(dir, { withFileTypes: true });
for (const file of files) {
const fullPath = path.join(dir, file.name);
if (file.isDirectory()) {
// Recursively load events from subdirectories
loadEvents(fullPath, client);
} else if (file.isFile() && file.name.endsWith('.js')) {
// Load the event file
const event = require(fullPath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
//console.log(`Loaded event: ${event.name}`);
}
}
}
// Call the function to load all events
const eventsPath = path.join(__dirname, 'events');
loadEvents(eventsPath, client);
connectDB().then(() => {
client.login(process.env.TOKEN).catch(console.error)
});
process.on('SIGINT', async () => {
console.log('Bot is shutting down...')
closeDB();
process.exit(0)
});
process.on('SIGTERM', async () => {
console.log('Bot received termination signal...');
closeDB();
process.exit(0)
})