Files
src
.gitignore
package.json
README.md
Code
27 2,713
src/index.js
import { Client, GatewayIntentBits, Partials, Collection } from 'discord.js';
import { config } from 'dotenv';
import mongoose from 'mongoose';
import { readdirSync } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import chalk from 'chalk';
config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildInvites,
GatewayIntentBits.GuildModeration
],
partials: [
Partials.Channel,
Partials.Message,
Partials.User,
Partials.GuildMember
]
});
client.commands = new Collection();
client.cooldowns = new Collection();
async function loadCommands() {
const commandFolders = readdirSync(join(__dirname, 'commands'));
for (const folder of commandFolders) {
const commandFiles = readdirSync(join(__dirname, 'commands', folder)).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = await import(`./commands/${folder}/${file}`);
if (command.default?.data && command.default?.execute) {
client.commands.set(command.default.data.name, command.default);
console.log(chalk.green(`โ Loaded command: ${command.default.data.name}`));
}
}
}
}
async function loadEvents() {
const eventFiles = readdirSync(join(__dirname, 'events')).filter(file => file.endsWith('.js'));
for (const file of eventFiles) {
const event = await import(`./events/${file}`);
if (event.default?.name) {
if (event.default.once) {
client.once(event.default.name, (...args) => event.default.execute(...args, client));
} else {
client.on(event.default.name, (...args) => event.default.execute(...args, client));
}
console.log(chalk.blue(`โ Loaded event: ${event.default.name}`));
}
}
}
async function connectDatabase() {
try {
await mongoose.connect(process.env.MONGODB_URI);
console.log(chalk.magenta('โ Connected to MongoDB'));
} catch (error) {
console.error(chalk.red('โ MongoDB connection failed:'), error);
process.exit(1);
}
}
async function initialize() {
console.log(chalk.cyan.bold('\n๐ SPECTERAL BOT INITIALIZING...\n'));
await connectDatabase();
await loadCommands();
await loadEvents();
await client.login(process.env.DISCORD_TOKEN);
}
initialize().catch(console.error);
export default client;