2024-02-11 01:04:12 +00:00
|
|
|
import { Client, Collection, GatewayIntentBits, Partials } from 'discord.js';
|
2024-02-06 14:25:58 +00:00
|
|
|
import { getFiles, importAndCheck } from './shared.js';
|
2024-01-26 13:28:02 +00:00
|
|
|
import { join, dirname } from 'path';
|
|
|
|
import { fileURLToPath } from 'url';
|
|
|
|
import { config } from 'dotenv';
|
2024-02-11 01:04:12 +00:00
|
|
|
import Module from 'module';
|
2024-01-26 13:28:02 +00:00
|
|
|
|
|
|
|
config();
|
|
|
|
|
2024-02-11 01:04:12 +00:00
|
|
|
/**
|
|
|
|
* Main entry point, the bot logs on to discord.
|
|
|
|
* @param {Array<Module>} commands
|
|
|
|
* @param {Array<Module>} events
|
|
|
|
*/
|
2024-01-26 13:28:02 +00:00
|
|
|
const runClient = (commands, events) => {
|
|
|
|
// Create a new client instance
|
2024-01-28 17:33:03 +00:00
|
|
|
const client = new Client({
|
|
|
|
intents: [
|
|
|
|
GatewayIntentBits.Guilds,
|
2024-03-02 22:49:13 +00:00
|
|
|
GatewayIntentBits.GuildMembers,
|
2024-01-28 17:33:03 +00:00
|
|
|
GatewayIntentBits.GuildMessages,
|
2024-01-28 17:33:22 +00:00
|
|
|
GatewayIntentBits.GuildVoiceStates,
|
2024-01-28 17:33:03 +00:00
|
|
|
GatewayIntentBits.GuildMessageReactions
|
|
|
|
],
|
|
|
|
partials: [Partials.Message, Partials.Reaction]
|
|
|
|
});
|
2024-02-11 01:04:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The commands registered for this client.
|
|
|
|
* @type {Collection}
|
|
|
|
*/
|
2024-01-26 13:28:02 +00:00
|
|
|
client.commands = new Collection();
|
|
|
|
commands.forEach((c) => client.commands.set(c.data.name, c));
|
2024-02-11 01:04:12 +00:00
|
|
|
|
|
|
|
// Register client events
|
2024-01-26 13:28:02 +00:00
|
|
|
events.forEach((e) =>
|
|
|
|
e.once
|
|
|
|
? client.once(e.name, (...args) => e.execute(...args))
|
|
|
|
: client.on(e.name, (...args) => e.execute(...args))
|
|
|
|
);
|
|
|
|
|
|
|
|
// Log in to Discord with your client's token
|
|
|
|
client.login(process.env.TOKEN);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Register commands from sub-directories
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
|
|
const cmdPath = join(__dirname, 'commands');
|
|
|
|
const evtPath = join(__dirname, 'events');
|
2024-02-06 14:25:58 +00:00
|
|
|
getFiles(cmdPath)
|
|
|
|
// For each command file
|
|
|
|
.then(async (files) =>
|
2024-02-06 15:18:06 +00:00
|
|
|
(await Promise.all(files.map(importAndCheck))).filter((module) => module !== 0)
|
|
|
|
)
|
|
|
|
.then(async (commands) => {
|
2024-02-06 14:25:58 +00:00
|
|
|
const files = await getFiles(evtPath);
|
2024-02-06 15:18:06 +00:00
|
|
|
const events = await Promise.all(files.map(async (filePath) => await import(filePath)));
|
2024-01-26 13:28:02 +00:00
|
|
|
runClient(commands, events);
|
|
|
|
});
|