Files
DRBv3/server/discordBot/discordBot.mjs
2024-02-10 15:10:35 -05:00

93 lines
3.7 KiB
JavaScript

import { Client, GatewayIntentBits, Collection } from 'discord.js';
import { registerActiveCommands, unregisterAllCommands } from './modules/registerCommands.mjs'
import { join, dirname } from 'path';
import { readdirSync } from 'fs';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
import dotenv from 'dotenv';
dotenv.config()
/**
* Add the enabled commands to the bot to be used by users in discord
* (commands that end in '.mjs' will be enabled, to disable just remove the extension or replace with '.mjs.disabled')
* @param {any} serverClient
* @param {any} _commandsPath="./commands"
* @returns {any}
*/
export const addEnabledCommands = async (serverClient, _commandsPath = "./commands") => {
// Setup commands for the Discord bot
serverClient.commands = new Collection();
const commandsPath = join(__dirname, _commandsPath);
const commandFiles = readdirSync(commandsPath).filter(file => file.endsWith('.mjs'));
for (const file of commandFiles) {
const filePath = await join(commandsPath, file);
console.log(`Adding enabled command: ${filePath}`);
await import(`file://${filePath}`).then(command => {
if (command.data instanceof Promise) {
command.data.then(async (builder) => {
command.data = builder;
console.log("Importing command: ", command.data.name, command);
// Set a new item in the Collection
// With the key as the command name and the value as the exported module
serverClient.commands.set(command.data.name, command);
});
} else {
console.log("Importing command: ", command.data.name, command);
// Set a new item in the Collection
// With the key as the command name and the value as the exported module
serverClient.commands.set(command.data.name, command);
}
})
}
// Register the commands currently in use by the bot
await registerActiveCommands(serverClient);
}
/**
* Add the enabled event listeners to the bot
* (events that end in '.mjs' will be enabled, to disable just remove the extension or replace with '.mjs.disabled')
* @param {any} serverClient
* @param {any} _eventsPath="./events"
* @returns {any}
*/
export function addEnabledEventListeners(serverClient, _eventsPath = "./events") {
const eventsPath = join(__dirname, _eventsPath);
const eventFiles = readdirSync(eventsPath).filter(file => file.endsWith('.mjs'));
for (const file of eventFiles) {
const filePath = join(eventsPath, file);
console.log(`Adding enabled event listener: ${filePath}`);
import(`file://${filePath}`).then(event => {
console.log("Adding event: ", event);
if (event.once) {
serverClient.once(event.name, (...args) => event.execute(serverClient.nodeIo, ...args));
} else {
serverClient.on(event.name, (...args) => event.execute(serverClient.nodeIo, ...args));
}
})
}
}
// The discord client
export const serverClient = new Client({ intents: [GatewayIntentBits.Guilds] });
// Run when the bot is ready
serverClient.on('ready', async () => {
console.log(`Logged in as ${serverClient.user.tag}!`);
// Add and register commands
await addEnabledCommands(serverClient);
// Config the discord bot with events
await addEnabledEventListeners(serverClient);
});
// Startup the discord bot
console.log(`Logging into discord with ID: ${process.env.DISCORD_TOKEN}`);
serverClient.login(process.env.DISCORD_TOKEN);