2022-06-17 01:25:05 +02:00
|
|
|
import fs from 'node:fs';
|
|
|
|
import path from 'node:path';
|
2024-07-04 05:53:39 +02:00
|
|
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
2022-08-28 17:02:22 +02:00
|
|
|
import { Client, Collection, GatewayIntentBits, Partials } from 'discord.js';
|
2022-06-17 02:14:14 +02:00
|
|
|
import dotenv from 'dotenv';
|
|
|
|
dotenv.config();
|
2022-08-28 17:02:22 +02:00
|
|
|
const { token, NODE_ENV } = process.env;
|
2022-06-16 09:18:39 +02:00
|
|
|
|
2022-06-17 01:25:05 +02:00
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
|
2022-08-28 17:02:22 +02:00
|
|
|
const client = new Client({
|
|
|
|
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.GuildMembers],
|
|
|
|
partials: [Partials.Message, Partials.Reaction, Partials.Channel],
|
|
|
|
shards: 'auto',
|
|
|
|
});
|
2022-06-16 09:18:39 +02:00
|
|
|
|
2022-06-17 07:31:58 +02:00
|
|
|
// Load commands
|
2022-06-16 09:18:39 +02:00
|
|
|
client.commands = new Collection();
|
2022-09-28 16:05:07 +02:00
|
|
|
|
2023-04-04 00:26:52 +02:00
|
|
|
fs.readdir(`${__dirname}/commands`, (err, categoryPath) => {
|
|
|
|
if (err) {
|
|
|
|
return console.error(err);
|
|
|
|
}
|
|
|
|
categoryPath.forEach(category => {
|
|
|
|
loadCommandFromDir(category);
|
|
|
|
});
|
2022-09-28 16:05:07 +02:00
|
|
|
});
|
2022-06-16 09:18:39 +02:00
|
|
|
|
2022-06-17 07:31:58 +02:00
|
|
|
// Load events
|
2023-04-04 00:26:52 +02:00
|
|
|
loadEventFromDir('client', client);
|
2022-08-28 17:02:22 +02:00
|
|
|
if (NODE_ENV !== 'development') {
|
2023-04-04 00:26:52 +02:00
|
|
|
loadEventFromDir('process', process);
|
2022-08-28 17:02:22 +02:00
|
|
|
}
|
2022-06-16 09:18:39 +02:00
|
|
|
|
2022-06-17 07:31:58 +02:00
|
|
|
client.login(token);
|
2022-06-16 09:18:39 +02:00
|
|
|
|
2022-06-17 07:31:58 +02:00
|
|
|
async function loadCommandFromDir(dir) {
|
|
|
|
const commandsPath = path.join(`${__dirname}/commands`, dir);
|
|
|
|
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
|
2022-06-16 09:18:39 +02:00
|
|
|
|
2022-06-17 07:31:58 +02:00
|
|
|
for (const file of commandFiles) {
|
|
|
|
const filePath = path.join(commandsPath, file);
|
2024-07-04 05:53:39 +02:00
|
|
|
import(pathToFileURL(filePath))
|
2023-04-03 04:50:10 +02:00
|
|
|
.then(importedCommand => {
|
|
|
|
const command = importedCommand.default;
|
|
|
|
client.commands.set(command.data.name, command);
|
|
|
|
console.log(`Successfully loaded command \x1b[32m${command.category}/${command.data.name}\x1b[0m`);
|
|
|
|
})
|
|
|
|
.catch(error => console.error(`Failed to load command for path: ${filePath}`, error));
|
2022-06-17 02:08:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-17 07:31:58 +02:00
|
|
|
async function loadEventFromDir(dir, listener) {
|
|
|
|
const eventsPath = path.join(`${__dirname}/events`, dir);
|
|
|
|
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));
|
|
|
|
|
|
|
|
for (const file of eventFiles) {
|
|
|
|
const filePath = path.join(eventsPath, file);
|
2024-07-04 05:53:39 +02:00
|
|
|
import(pathToFileURL(filePath))
|
2023-04-03 04:50:10 +02:00
|
|
|
.then(importedEvent => {
|
|
|
|
const event = importedEvent.default;
|
|
|
|
if (event.once) {
|
|
|
|
listener.once(event.name, (...args) => event.execute(...args, client));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
listener.on(event.name, (...args) => event.execute(...args, client));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => console.error(`Failed to load event for path: ${filePath}`, error));
|
2022-06-17 07:31:58 +02:00
|
|
|
}
|
2023-04-04 00:26:52 +02:00
|
|
|
}
|