44 lines
1.3 KiB
JavaScript
44 lines
1.3 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const { Client, GatewayIntentBits } = require('discord.js');
|
|
const { discordToken } = require('@config'); // Using alias for config
|
|
const environment = process.env.NODE_ENV || 'development';
|
|
|
|
const client = new Client({
|
|
intents: [
|
|
GatewayIntentBits.Guilds,
|
|
GatewayIntentBits.GuildMessages,
|
|
GatewayIntentBits.MessageContent
|
|
]
|
|
});
|
|
const serverDataFilePath = path.join(
|
|
__dirname,
|
|
`discord/store/${environment}-serverData.json`
|
|
);
|
|
|
|
let serverData = {};
|
|
if (fs.existsSync(serverDataFilePath)) {
|
|
serverData = JSON.parse(fs.readFileSync(serverDataFilePath, 'utf8'));
|
|
} else {
|
|
fs.writeFileSync(serverDataFilePath, JSON.stringify({}, null, 2), 'utf8');
|
|
console.warn(
|
|
`Warning: File not found at ${serverDataFilePath}. Using empty server data.`
|
|
);
|
|
}
|
|
|
|
// Load event handlers
|
|
const eventFiles = fs
|
|
.readdirSync('./src/discord/events')
|
|
.filter(file => file.endsWith('.js') && !file.endsWith('.spec.js'));
|
|
for (const file of eventFiles) {
|
|
const event = require(`./discord/events/${file}`);
|
|
|
|
if (event.once) {
|
|
client.once(event.name, (...args) => event.execute(...args, serverData));
|
|
} else {
|
|
client.on(event.name, (...args) => event.execute(...args, serverData));
|
|
}
|
|
}
|
|
|
|
client.login(discordToken);
|