Linting
All checks were successful
release-tag / release-image (push) Successful in 1m52s
Lint JavaScript/Node.js / lint-js (push) Successful in 11s
DRB Tests / drb_mocha_tests (push) Successful in 29s

This commit is contained in:
Logan Cusano
2024-08-11 15:57:46 -04:00
parent 5cd47378d6
commit 117cbea67f
37 changed files with 2273 additions and 1738 deletions

View File

@@ -1,20 +1,22 @@
import { DebugBuilder } from "../modules/debugger.mjs";
import { Client, GatewayIntentBits, Collection } from 'discord.js';
import { registerActiveCommands, unregisterAllCommands } from './modules/registerCommands.mjs'
import { RSSController } from '../rss-manager/rssController.mjs'
import { join, dirname } from 'path';
import { readdirSync } from 'fs';
import { fileURLToPath } from 'url';
import { Client, GatewayIntentBits, Collection } from "discord.js";
import {
registerActiveCommands,
unregisterAllCommands,
} from "./modules/registerCommands.mjs";
import { RSSController } from "../rss-manager/rssController.mjs";
import { join, dirname } from "path";
import { readdirSync } from "fs";
import { fileURLToPath } from "url";
import dotenv from 'dotenv';
dotenv.config()
import dotenv from "dotenv";
dotenv.config();
const log = new DebugBuilder("server", "discordBot");
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
/**
* 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')
@@ -22,36 +24,41 @@ const __dirname = dirname(__filename);
* @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'));
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);
log.INFO(`Adding enabled command: ${filePath}`);
await import(`file://${filePath}`).then(command => {
if (command.data instanceof Promise) {
command.data.then(async (builder) => {
command.data = builder;
log.DEBUG("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 {
log.DEBUG("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);
}
})
}
for (const file of commandFiles) {
const filePath = await join(commandsPath, file);
log.INFO(`Adding enabled command: ${filePath}`);
await import(`file://${filePath}`).then((command) => {
if (command.data instanceof Promise) {
command.data.then(async (builder) => {
command.data = builder;
log.DEBUG("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 {
log.DEBUG("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);
}
// Register the commands currently in use by the bot
await registerActiveCommands(serverClient);
};
/**
* Add the enabled event listeners to the bot
@@ -60,42 +67,60 @@ export const addEnabledCommands = async (serverClient, _commandsPath = "./comman
* @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'));
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);
log.INFO(`Adding enabled event listener: ${filePath}`);
import(`file://${filePath}`).then(event => {
log.DEBUG("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));
}
})
}
for (const file of eventFiles) {
const filePath = join(eventsPath, file);
log.INFO(`Adding enabled event listener: ${filePath}`);
import(`file://${filePath}`).then((event) => {
log.DEBUG("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, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildPresences] });
export const serverClient = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences,
],
});
// Run when the bot is ready
serverClient.on('ready', async () => {
log.INFO(`Logged in as ${serverClient.user.tag}!`);
serverClient.on("ready", async () => {
log.INFO(`Logged in as ${serverClient.user.tag}!`);
// Add and register commands
await addEnabledCommands(serverClient);
// Add and register commands
await addEnabledCommands(serverClient);
// Config the discord bot with events
await addEnabledEventListeners(serverClient);
// Config the discord bot with events
await addEnabledEventListeners(serverClient);
// Start the RSS Controller
serverClient.RSSController = await new RSSController(serverClient);
serverClient.RSSController.start();
// Start the RSS Controller
serverClient.RSSController = await new RSSController(serverClient);
serverClient.RSSController.start();
log.INFO("RSS Controller:", serverClient.RSSController);
log.INFO("RSS Controller:", serverClient.RSSController);
});
// Startup the discord bot