DiscordJS-Example/index.js

59 lines
1.7 KiB
JavaScript
Raw Normal View History

2024-02-11 01:04:12 +00:00
import { Client, Collection, GatewayIntentBits, Partials } from 'discord.js';
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,
GatewayIntentBits.GuildMessages,
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');
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) => {
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);
});