From 2ab5a181bd58d80ab80d62e700f7e4746fb4c6ab Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 25 May 2024 23:52:18 -0400 Subject: [PATCH] #5 replace all `console.logs` with debugger --- addons/example/index.js | 6 ++- discordBot/commands/add.mjs | 4 +- discordBot/commands/join.mjs | 18 ++++--- discordBot/commands/leave.mjs | 8 +-- discordBot/commands/ping.mjs | 6 ++- discordBot/commands/triggerRss.mjs | 7 +-- discordBot/commands/update.mjs | 4 +- discordBot/discordBot.mjs | 29 +++++++---- discordBot/events/interactionCreate.mjs | 8 +-- discordBot/modules/registerCommands.mjs | 24 ++++----- discordBot/modules/rssWrappers.mjs | 2 +- discordBot/modules/wrappers.mjs | 8 +-- modules/addonManager.mjs | 6 ++- .../mongo-wrappers/mongoDiscordIDWrappers.mjs | 16 +++--- modules/mongo-wrappers/mongoFeedsWrappers.mjs | 16 +++--- modules/mongo-wrappers/mongoNodesWrappers.mjs | 16 +++--- .../mongo-wrappers/mongoSystemsWrappers.mjs | 18 ++++--- modules/socketServer.mjs | 4 +- modules/socketServerWrappers.mjs | 49 ++++++++++--------- rss-manager/feedHandler.mjs | 24 +++++---- rss-manager/rssController.mjs | 48 +++++++++++------- server.js | 4 +- 22 files changed, 192 insertions(+), 133 deletions(-) diff --git a/addons/example/index.js b/addons/example/index.js index 2ba6e20..384e9ad 100644 --- a/addons/example/index.js +++ b/addons/example/index.js @@ -1,8 +1,10 @@ // addons/addon1/index.js +import { DebugBuilder } from "../../modules/debugger.mjs"; +const log = new DebugBuilder("server", "server"); // Function called by the main application to initialize the addon export function initialize(nodeIo, config) { - console.log(`Initializing ${config.name}`); + log.INFO(`Initializing ${config.name}`); // Call other functions within the addon module registerSocketEvents(nodeIo, config); @@ -12,6 +14,6 @@ export function initialize(nodeIo, config) { // Function to register Socket.IO event handlers function registerSocketEvents(nodeIo, config) { nodeIo.on(config.options.eventName, (data) => { - console.log(`Received event "${config.options.eventName}" from client:`, data); + log.DEBUG(`Received event "${config.options.eventName}" from client:`, data); }); } diff --git a/discordBot/commands/add.mjs b/discordBot/commands/add.mjs index 0259c85..abe7107 100644 --- a/discordBot/commands/add.mjs +++ b/discordBot/commands/add.mjs @@ -2,7 +2,7 @@ import { SlashCommandBuilder } from 'discord.js'; import { DebugBuilder } from "../../modules/debugger.mjs"; import { addSource } from '../../rss-manager/feedHandler.mjs' -const log = new DebugBuilder("server", "add"); +const log = new DebugBuilder("server", "discordBot.command.add"); // Exporting data property that contains the command structure for discord including any params export const data = new SlashCommandBuilder() @@ -37,7 +37,7 @@ export async function autocomplete(nodeIo, interaction) { const focusedValue = interaction.options.getFocused(); const choices = []; const filtered = choices.filter(choice => choice.name.startsWith(focusedValue)); - console.log(focusedValue, choices, filtered); + log.DEBUG(focusedValue, choices, filtered); await interaction.respond(filtered); } */ diff --git a/discordBot/commands/join.mjs b/discordBot/commands/join.mjs index 0942a78..a7784eb 100644 --- a/discordBot/commands/join.mjs +++ b/discordBot/commands/join.mjs @@ -1,3 +1,5 @@ +import { DebugBuilder } from "../../modules/debugger.mjs"; +const log = new DebugBuilder("server", "discordBot.command.join"); import { SlashCommandBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } from 'discord.js'; import { requestNodeJoinSystem, checkIfNodeIsConnectedToVC, checkIfNodeHasOpenDiscordClient, getNodeCurrentListeningSystem } from '../../modules/socketServerWrappers.mjs'; import { getSystemsByNuid, getAllSystems, getSystemByName } from '../../modules/mongo-wrappers/mongoSystemsWrappers.mjs'; @@ -27,7 +29,7 @@ export async function autocomplete(nodeIo, interaction) { const choices = await getAllSystems(); const filtered = choices.filter(choice => choice.name.startsWith(focusedValue)); - console.log(focusedValue, choices, filtered); + log.DEBUG(focusedValue, choices, filtered); await interaction.respond( filtered.map(choice => ({ name: choice.name, value: choice.name })), @@ -44,7 +46,7 @@ export async function execute(nodeIo, interaction) { if (!interaction.member.voice.channel) { return await interaction.editReply({ content: `<@${interaction.member.id}>, you need to enter a voice channel before you use this command`, ephemeral: true }) } // Grab the channel if the user is connected to VC const channelToJoin = interaction.member.voice.channel; - console.log(`The user '${interaction.member.id}' is in the voice channel '${channelToJoin}'`); + log.INFO(`The user '${interaction.member.id}' is in the voice channel '${channelToJoin}'`); // Get the selected system option from the command interaction const selectedSystem = interaction.options.getString('system'); @@ -58,11 +60,11 @@ export async function execute(nodeIo, interaction) { const openSocket = await nodeIo.sockets.sockets.get(selectedNodeSocketId); // Get the open ID for this connection\ const discordTokens = await getAvailableTokensInGuild(nodeIo, interaction.guild.id); - console.log("Available discord tokens: ", discordTokens); + log.DEBUG("Available discord tokens: ", discordTokens); if (discordTokens.length >= 1) { // TODO - Implement a method to have preferred tokens (bot users) for specific systems - console.log("Joining selected open socket:", selectedNodeSocketId, system.name, channelToJoin.id, openSocket.node.name, discordTokens[0].token); + log.INFO("Joining selected open socket:", selectedNodeSocketId, system.name, channelToJoin.id, openSocket.node.name, discordTokens[0].token); // Ask the node to join the selected channel and system await requestNodeJoinSystem(openSocket, system.name, channelToJoin.id, discordTokens[0].token); @@ -74,7 +76,7 @@ export async function execute(nodeIo, interaction) { // Get all open socket nodes const openSockets = [...await nodeIo.allSockets()]; // TODO - Filter the returned nodes to only nodes that have the radio capability - console.log("All open sockets: ", openSockets); + log.DEBUG("All open sockets: ", openSockets); var availableNodes = []; // Check each open socket to see if the node has the requested system @@ -85,14 +87,14 @@ export async function execute(nodeIo, interaction) { if (hasOpenClient) { let currentSystem = await getNodeCurrentListeningSystem(openSocket); if (currentSystem != system.name) { - console.log("Node is listening to a different system than requested", openSocket.node.name); + log.INFO("Node is listening to a different system than requested", openSocket.node.name); return; } } // Check if the bot has an open voice connection in the requested server already const connected = await checkIfNodeIsConnectedToVC(nodeIo, interaction.guild.id, openSocket.node.nuid); - console.log("Connected:", connected); + log.INFO("Connected:", connected); if (!connected) { // Check if this node has the requested system, if so add it to the availble array if (system.nodes.includes(openSocket.node.nuid)) { @@ -102,7 +104,7 @@ export async function execute(nodeIo, interaction) { })); - console.log("Availble nodes:", availableNodes.map(socket => socket.node.name)); + log.DEBUG("Availble nodes:", availableNodes.map(socket => socket.node.name)); // If there are no available nodes, let the user know there are none available if (availableNodes.length == 0) { diff --git a/discordBot/commands/leave.mjs b/discordBot/commands/leave.mjs index 2539175..a993e13 100644 --- a/discordBot/commands/leave.mjs +++ b/discordBot/commands/leave.mjs @@ -1,3 +1,5 @@ +import { DebugBuilder } from "../../modules/debugger.mjs"; +const log = new DebugBuilder("server", "discordBot.command.leave"); import { SlashCommandBuilder } from 'discord.js'; import { requestBotLeaveServer, getSocketIdByNuid } from '../../modules/socketServerWrappers.mjs'; import { checkOnlineBotsInGuild } from '../modules/wrappers.mjs' @@ -25,11 +27,11 @@ export async function autocomplete(nodeIo, interaction) { const focusedValue = interaction.options.getFocused(); const choices = (await checkOnlineBotsInGuild(nodeIo, interaction.guild.id)); - console.log(choices); + log.DEBUG(choices); const filtered = choices.filter(choice => choice.name.startsWith(focusedValue)).map(choice => choice = {name: choice.name, value: choice.nuid}); - console.log(focusedValue, choices, filtered); + log.DEBUG(focusedValue, choices, filtered); await interaction.respond(filtered); } @@ -44,7 +46,7 @@ export async function execute(nodeIo, interaction) { // Get the requested bot const selectedNode = interaction.options.getString('bot'); const socket = await getSocketIdByNuid(nodeIo, selectedNode); - console.log("All open sockets:", socket, selectedNode); + log.DEBUG("All open sockets:", socket, selectedNode); await requestBotLeaveServer(socket, interaction.guild.id); //await interaction.reply(`**Online Sockets: '${sockets}'**`); await interaction.editReply(`Ok <@${interaction.member.id}>, the bot is leaving shortly`); diff --git a/discordBot/commands/ping.mjs b/discordBot/commands/ping.mjs index 6b4cc2e..4ab5e80 100644 --- a/discordBot/commands/ping.mjs +++ b/discordBot/commands/ping.mjs @@ -1,3 +1,5 @@ +import { DebugBuilder } from "../../modules/debugger.mjs"; +const log = new DebugBuilder("server", "discordBot.command.ping"); import { SlashCommandBuilder } from 'discord.js'; // Exporting data property that contains the command structure for discord including any params @@ -19,7 +21,7 @@ export async function autocomplete(nodeIo, interaction) { const focusedValue = interaction.options.getFocused(); const choices = []; const filtered = choices.filter(choice => choice.name.startsWith(focusedValue)); - console.log(focusedValue, choices, filtered); + log.INFO(focusedValue, choices, filtered); await interaction.respond(filtered); } */ @@ -32,7 +34,7 @@ export async function autocomplete(nodeIo, interaction) { export const execute = async (nodeIo, interaction) => { try { const sockets = await nodeIo.allSockets(); - console.log("All open sockets: ",sockets); + log.DEBUG("All open sockets: ",sockets); //await interaction.reply(`**Online Sockets: '${sockets}'**`); await interaction.reply('**Pong.**'); //await interaction.channel.send('**Pong.**'); diff --git a/discordBot/commands/triggerRss.mjs b/discordBot/commands/triggerRss.mjs index f55b0be..1827575 100644 --- a/discordBot/commands/triggerRss.mjs +++ b/discordBot/commands/triggerRss.mjs @@ -1,3 +1,5 @@ +import { DebugBuilder } from "../../modules/debugger.mjs"; +const log = new DebugBuilder("server", "discordBot.command.triggerRss"); import { SlashCommandBuilder } from 'discord.js'; import { updateFeeds } from '../../rss-manager/feedHandler.mjs' @@ -20,7 +22,7 @@ export async function autocomplete(nodeIo, interaction) { const focusedValue = interaction.options.getFocused(); const choices = []; const filtered = choices.filter(choice => choice.name.startsWith(focusedValue)); - console.log(focusedValue, choices, filtered); + log.INFO(focusedValue, choices, filtered); await interaction.respond(filtered); } */ @@ -32,8 +34,7 @@ export async function autocomplete(nodeIo, interaction) { */ export const execute = async (nodeIo, interaction) => { try { - //const sockets = await nodeIo.allSockets(); - //console.log("All open sockets: ", sockets); + //const sockets = await nodeIo.allSockets(); //await interaction.reply(`**Online Sockets: '${sockets}'**`); await interaction.reply('Triggering RSS update'); await updateFeeds(interaction.client); diff --git a/discordBot/commands/update.mjs b/discordBot/commands/update.mjs index b9f4150..d5245d5 100644 --- a/discordBot/commands/update.mjs +++ b/discordBot/commands/update.mjs @@ -1,3 +1,5 @@ +import { DebugBuilder } from "../../modules/debugger.mjs"; +const log = new DebugBuilder("server", "discordBot.command.update"); import { SlashCommandBuilder } from 'discord.js'; import { requestNodeUpdate } from '../../modules/socketServerWrappers.mjs'; @@ -18,7 +20,7 @@ export const deferInitialReply = false; // If we the initial reply in discord sh export const execute = async (nodeIo, interaction) => { try { const openSockets = [...await nodeIo.allSockets()]; // TODO - Filter the returned nodes to only nodes that have the radio capability - console.log("All open sockets: ", openSockets); + log.DEBUG("All open sockets: ", openSockets); // Check each open socket to see if the node has the requested system await Promise.all(openSockets.map(openSocket => { diff --git a/discordBot/discordBot.mjs b/discordBot/discordBot.mjs index d09feeb..aea3d4e 100644 --- a/discordBot/discordBot.mjs +++ b/discordBot/discordBot.mjs @@ -1,14 +1,19 @@ +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 dotenv from 'dotenv'; +dotenv.config() + +const log = new DebugBuilder("server", "discordBot"); + 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 @@ -25,18 +30,18 @@ export const addEnabledCommands = async (serverClient, _commandsPath = "./comman for (const file of commandFiles) { const filePath = await join(commandsPath, file); - console.log(`Adding enabled command: ${filePath}`); + 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; - console.log("Importing command: ", command.data.name, command); + 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 { - console.log("Importing command: ", command.data.name, command); + 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); @@ -61,9 +66,9 @@ export function addEnabledEventListeners(serverClient, _eventsPath = "./events") for (const file of eventFiles) { const filePath = join(eventsPath, file); - console.log(`Adding enabled event listener: ${filePath}`); + log.INFO(`Adding enabled event listener: ${filePath}`); import(`file://${filePath}`).then(event => { - console.log("Adding event: ", event); + log.DEBUG("Adding event: ", event); if (event.once) { serverClient.once(event.name, (...args) => event.execute(serverClient.nodeIo, ...args)); } else { @@ -78,15 +83,21 @@ export const serverClient = new Client({ intents: [GatewayIntentBits.Guilds, Gat // Run when the bot is ready serverClient.on('ready', async () => { - console.log(`Logged in as ${serverClient.user.tag}!`); + log.INFO(`Logged in as ${serverClient.user.tag}!`); // Add and register commands await addEnabledCommands(serverClient); // Config the discord bot with events await addEnabledEventListeners(serverClient); + + // Start the RSS Controller + serverClient.RSSController = await new RSSController(serverClient); + serverClient.RSSController.start(); + + log.INFO("RSS Controller:", serverClient.RSSController); }); // Startup the discord bot -console.log(`Logging into discord with ID: ${process.env.DISCORD_TOKEN}`); +log.INFO(`Logging into discord with ID: ${process.env.DISCORD_TOKEN}`); serverClient.login(process.env.DISCORD_TOKEN); diff --git a/discordBot/events/interactionCreate.mjs b/discordBot/events/interactionCreate.mjs index 0aa7482..9084d88 100644 --- a/discordBot/events/interactionCreate.mjs +++ b/discordBot/events/interactionCreate.mjs @@ -1,14 +1,16 @@ +import { DebugBuilder } from "../../modules/debugger.mjs"; +const log = new DebugBuilder("server", "discordBot.events.interactionCreate"); import { Events } from 'discord.js'; export const name = Events.InteractionCreate; export async function execute(nodeIo, interaction) { const command = interaction.client.commands.get(interaction.commandName); - console.log("Interaction created for command: ", command); + log.INFO("Interaction created for command: ", command); // Execute autocomplete if the user is checking autocomplete if (interaction.isAutocomplete()) { - console.log("Running autocomplete for command: ", command.data.name); + log.INFO("Running autocomplete for command: ", command.data.name); return await command.autocomplete(nodeIo, interaction); } @@ -20,7 +22,7 @@ export async function execute(nodeIo, interaction) { return; } - console.log(`${interaction.member.user} is running '${interaction.commandName}'`); + log.INFO(`${interaction.member.user} is running '${interaction.commandName}'`); // Defer the initial reply if the command has the parameter set if (command.deferInitialReply) { diff --git a/discordBot/modules/registerCommands.mjs b/discordBot/modules/registerCommands.mjs index cfadb0e..f8d027a 100644 --- a/discordBot/modules/registerCommands.mjs +++ b/discordBot/modules/registerCommands.mjs @@ -1,3 +1,5 @@ +import { DebugBuilder } from "../../modules/debugger.mjs"; +const log = new DebugBuilder("server", "discordBot.modules.registerCommands"); import { REST, Routes } from 'discord.js'; import dotenv from 'dotenv'; @@ -15,21 +17,21 @@ export const registerActiveCommands = async (serverClient) => { // and deploy your commands! guildIDs.forEach(guild => { - console.log("Deploying commands for: ", guild.id); - console.log("Commands", commands); + log.INFO("Deploying commands for: ", guild.id); + log.DEBUG("Commands", commands); (async () => { try { - console.log(`Started refreshing application (/) commands for guild ID: ${guild.id}.`); + log.DEBUG(`Started refreshing application (/) commands for guild ID: ${guild.id}.`); // The put method is used to fully refresh all commands in the guild with the current set const data = await rest.put( Routes.applicationGuildCommands(clientId, guild.id), { body: commands }, ); - console.log(`Successfully reloaded ${data.length} application (/) commands for guild ID: ${guild.id}.`); + log.DEBUG(`Successfully reloaded ${data.length} application (/) commands for guild ID: ${guild.id}.`); } catch (error) { // And of course, make sure you catch and log any errors! - console.log("ERROR Deploying commands: ", error, "Body from error: ", commands); + log.ERROR("ERROR Deploying commands: ", error, "Body from error: ", commands); } })() }) @@ -47,20 +49,20 @@ export const unregisterAllCommands = async (serverClient) => { const rest = new REST({ version: '10' }).setToken(discordToken); guildIDs.forEach(guild => { - console.log("Removing commands for: ", clientId, guild.id); + log.INFO("Removing commands for: ", clientId, guild.id); (async () => { try { - console.log(`Started removal of ${commands.length} application (/) commands for guild ID: ${guild.id}.`); + log.DEBUG(`Started removal of ${commands.length} application (/) commands for guild ID: ${guild.id}.`); // The put method is used to fully refresh all commands in the guild with the current set const data = await rest.put( Routes.applicationGuildCommands(clientId, guild.id), { body: commands }, ); - console.log(`Successfully removed ${data.length} application (/) commands for guild ID: ${guild.id}.`); + log.DEBUG(`Successfully removed ${data.length} application (/) commands for guild ID: ${guild.id}.`); } catch (error) { // And of course, make sure you catch and log any errors! - console.log("ERROR removing commands: ", error, "Body from error: ", commands); + log.ERROR("ERROR removing commands: ", error, "Body from error: ", commands); } })() }) @@ -74,10 +76,10 @@ export const unregisterAllCommands = async (serverClient) => { */ export const refreshActiveCommandsWrapper = async (serverClient) => { // Remove all commands - console.log("Removing/Unregistering all commands from all connected servers/guilds"); + log.INFO("Removing/Unregistering all commands from all connected servers/guilds"); await unregisterAllCommands(serverClient); // Deploy the active commands - console.log("Adding commands to all connected servers/guilds"); + log.INFO("Adding commands to all connected servers/guilds"); await registerActiveCommands(serverClient); return; } \ No newline at end of file diff --git a/discordBot/modules/rssWrappers.mjs b/discordBot/modules/rssWrappers.mjs index 9b769e0..91c1397 100644 --- a/discordBot/modules/rssWrappers.mjs +++ b/discordBot/modules/rssWrappers.mjs @@ -7,7 +7,7 @@ import { config } from 'dotenv'; // Load environment variables config(); -const log = new DebugBuilder("server", "libUtils"); +const log = new DebugBuilder("server", "discordBot.modules.rssWrappers"); const imageRegex = /(http(s?):)([/|.|\w|\s|-])*((\.(?:jpg|gif|png|webm))|(\/gallery\/(?:[/|.|\w|\s|-])*))/g; const youtubeVideoRegex = /((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube(-nocookie)?\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)/g; diff --git a/discordBot/modules/wrappers.mjs b/discordBot/modules/wrappers.mjs index 4661ec2..652b6b3 100644 --- a/discordBot/modules/wrappers.mjs +++ b/discordBot/modules/wrappers.mjs @@ -1,3 +1,5 @@ +import { DebugBuilder } from "../../modules/debugger.mjs"; +const log = new DebugBuilder("server", "discordBot.modules.wrappers"); import { checkIfNodeIsConnectedToVC, getNodeDiscordID, getNodeDiscordUsername } from '../../modules/socketServerWrappers.mjs'; import { getAllDiscordIDs } from '../../modules/mongo-wrappers/mongoDiscordIDWrappers.mjs' @@ -8,7 +10,7 @@ export const checkOnlineBotsInGuild = async (nodeIo, guildId) => { await Promise.all(openSockets.map(async openSocket => { openSocket = await nodeIo.sockets.sockets.get(openSocket); const connected = await checkIfNodeIsConnectedToVC(nodeIo, guildId, openSocket.node.nuid); - console.log("Connected:", connected); + log.INFO("Connected:", connected); if (connected) { const username = await getNodeDiscordUsername(openSocket, guildId); const discordID = await getNodeDiscordID(openSocket); @@ -33,8 +35,8 @@ export const checkOnlineBotsInGuild = async (nodeIo, guildId) => { ]); // Use the results of both promises here - console.log("Available Discord IDs:", discordIDs); - console.log("Online bots in the guild:", onlineBots); + log.INFO("Available Discord IDs:", discordIDs); + log.INFO("Online bots in the guild:", onlineBots); // Filter any discordIDs that are not active const availableDiscordIDs = discordIDs.filter(discordID => discordID.active == true).filter(discordID => !onlineBots.some(bot => Number(bot.discord_id) == discordID.discord_id)); diff --git a/modules/addonManager.mjs b/modules/addonManager.mjs index ca63c51..fbc3f40 100644 --- a/modules/addonManager.mjs +++ b/modules/addonManager.mjs @@ -1,3 +1,5 @@ +import { DebugBuilder } from "../modules/debugger.mjs"; +const log = new DebugBuilder("server", "addonManager"); import { fileURLToPath } from 'url'; import fs from 'fs'; import path from 'path'; @@ -20,9 +22,9 @@ export const loadAddons = async (nodeIo) => { if (addonConfig.enabled) { const addonIndexPath = path.join(addonsDir, addonDir.name, 'index.js'); import(`file://${addonIndexPath}`).then(addonModule => { - console.log("Loading addon: ", addonModule); + log.DEBUG("Loading addon: ", addonModule); addonModule.initialize(nodeIo, addonConfig); - console.log(`Addon ${addonConfig.name} loaded.`); + log.DEBUG(`Addon ${addonConfig.name} loaded.`); }); } } diff --git a/modules/mongo-wrappers/mongoDiscordIDWrappers.mjs b/modules/mongo-wrappers/mongoDiscordIDWrappers.mjs index 1249a62..feb477c 100644 --- a/modules/mongo-wrappers/mongoDiscordIDWrappers.mjs +++ b/modules/mongo-wrappers/mongoDiscordIDWrappers.mjs @@ -1,3 +1,5 @@ +import { DebugBuilder } from "../../modules/debugger.mjs"; +const log = new DebugBuilder("server", "mongoDiscordIDWrappers"); import { insertDocument, getDocuments, connectToDatabase } from "./mongoHandler.mjs"; const collectionName = 'discord-ids'; @@ -8,7 +10,7 @@ export const createDiscordID = async (discordID) => { const insertedId = await insertDocument(collectionName, discordID); return insertedId; } catch (error) { - console.error('Error creating Discord ID:', error); + log.ERROR('Error creating Discord ID:', error); throw error; } }; @@ -19,7 +21,7 @@ export const getAllDiscordIDs = async () => { const discordIDs = await getDocuments(collectionName); return discordIDs; } catch (error) { - console.error('Error getting all Discord IDs:', error); + log.ERROR('Error getting all Discord IDs:', error); throw error; } }; @@ -37,7 +39,7 @@ export const getDiscordID = async (identifier) => { }); return discordID; } catch (error) { - console.error('Error getting Discord ID:', error); + log.ERROR('Error getting Discord ID:', error); throw error; } finally { // Close the connection @@ -56,10 +58,10 @@ export const updateDiscordID = async (identifier, updatedFields) => { { discord_id: identifier } ] }, { $set: updatedFields }); - console.log('Discord ID updated:', result.modifiedCount); + log.INFO('Discord ID updated:', result.modifiedCount); return result.modifiedCount; } catch (error) { - console.error('Error updating Discord ID:', error); + log.ERROR('Error updating Discord ID:', error); throw error; } finally { // Close the connection @@ -78,10 +80,10 @@ export const deleteDiscordID = async (identifier) => { { discord_id: identifier } ] }); - console.log('Discord ID deleted:', result.deletedCount); + log.INFO('Discord ID deleted:', result.deletedCount); return result.deletedCount; } catch (error) { - console.error('Error deleting Discord ID:', error); + log.ERROR('Error deleting Discord ID:', error); throw error; } finally { // Close the connection diff --git a/modules/mongo-wrappers/mongoFeedsWrappers.mjs b/modules/mongo-wrappers/mongoFeedsWrappers.mjs index 4238ed1..540967d 100644 --- a/modules/mongo-wrappers/mongoFeedsWrappers.mjs +++ b/modules/mongo-wrappers/mongoFeedsWrappers.mjs @@ -1,3 +1,5 @@ +import { DebugBuilder } from "../../modules/debugger.mjs"; +const log = new DebugBuilder("server", "mongoFeedsWrappers"); import { insertDocument, getDocuments, @@ -15,7 +17,7 @@ import { const insertedId = await insertDocument(feedCollectionName, feed); return insertedId; } catch (error) { - console.error('Error creating feed:', error); + log.ERROR('Error creating feed:', error); throw error; } }; @@ -26,7 +28,7 @@ import { const feeds = await getDocuments(feedCollectionName); return feeds; } catch (error) { - console.error('Error getting all feeds:', error); + log.ERROR('Error getting all feeds:', error); throw error; } }; @@ -37,7 +39,7 @@ import { const feed = await getDocumentByField(feedCollectionName, 'link', link); return feed; } catch (error) { - console.error('Error getting feed by link:', error); + log.ERROR('Error getting feed by link:', error); throw error; } }; @@ -48,7 +50,7 @@ import { const modifiedCount = await updateDocumentByField(feedCollectionName, 'link', link, updatedFields); return modifiedCount; } catch (error) { - console.error('Error updating feed by link:', error); + log.ERROR('Error updating feed by link:', error); throw error; } }; @@ -59,7 +61,7 @@ import { const deletedCount = await deleteDocumentByField(feedCollectionName, 'link', link); return deletedCount; } catch (error) { - console.error('Error deleting feed by link:', error); + log.ERROR('Error deleting feed by link:', error); throw error; } }; @@ -70,7 +72,7 @@ import { const insertedId = await insertDocument(postCollectionName, post); return insertedId; } catch (error) { - console.error('Error creating post:', error); + log.ERROR('Error creating post:', error); throw error; } }; @@ -81,7 +83,7 @@ import { const post = await getDocumentByField(postCollectionName, 'postId', postId); return post; } catch (error) { - console.error('Error getting post by postId:', error); + log.ERROR('Error getting post by postId:', error); throw error; } }; diff --git a/modules/mongo-wrappers/mongoNodesWrappers.mjs b/modules/mongo-wrappers/mongoNodesWrappers.mjs index f12fbbf..13bbc66 100644 --- a/modules/mongo-wrappers/mongoNodesWrappers.mjs +++ b/modules/mongo-wrappers/mongoNodesWrappers.mjs @@ -1,3 +1,5 @@ +import { DebugBuilder } from "../../modules/debugger.mjs"; +const log = new DebugBuilder("server", "mongoNodesWrappers"); import { insertDocument, getDocuments, connectToDatabase } from "./mongoHandler.mjs"; const collectionName = 'nodes'; @@ -8,7 +10,7 @@ export const createNode = async (node) => { const insertedId = await insertDocument(collectionName, node); return insertedId; } catch (error) { - console.error('Error creating node:', error); + log.ERROR('Error creating node:', error); throw error; } }; @@ -19,7 +21,7 @@ export const getAllNodes = async () => { const nodes = await getDocuments(collectionName); return nodes; } catch (error) { - console.error('Error getting all nodes:', error); + log.ERROR('Error getting all nodes:', error); throw error; } }; @@ -32,7 +34,7 @@ export const getNodeByNuid = async (nuid) => { const node = await collection.findOne({ nuid }); return node; } catch (error) { - console.error('Error getting node by NUID:', error); + log.ERROR('Error getting node by NUID:', error); throw error; } finally { // Close the connection @@ -46,10 +48,10 @@ export const updateNodeByNuid = async (nuid, updatedFields) => { try { const collection = db.db().collection(collectionName); const result = await collection.updateOne({ nuid }, { $set: updatedFields }); - console.log('Node updated:', result.modifiedCount); + log.INFO('Node updated:', result.modifiedCount); return result.modifiedCount; } catch (error) { - console.error('Error updating node by NUID:', error); + log.ERROR('Error updating node by NUID:', error); throw error; } finally { // Close the connection @@ -63,10 +65,10 @@ export const deleteNodeByNuid = async (nuid) => { try { const collection = db.db().collection(collectionName); const result = await collection.deleteOne({ nuid }); - console.log('Node deleted:', result.deletedCount); + log.INFO('Node deleted:', result.deletedCount); return result.deletedCount; } catch (error) { - console.error('Error deleting node by NUID:', error); + log.ERROR('Error deleting node by NUID:', error); throw error; } finally { // Close the connection diff --git a/modules/mongo-wrappers/mongoSystemsWrappers.mjs b/modules/mongo-wrappers/mongoSystemsWrappers.mjs index f353073..212d516 100644 --- a/modules/mongo-wrappers/mongoSystemsWrappers.mjs +++ b/modules/mongo-wrappers/mongoSystemsWrappers.mjs @@ -1,3 +1,5 @@ +import { DebugBuilder } from "../../modules/debugger.mjs"; +const log = new DebugBuilder("server", "mongoSystemsWrappers"); import { insertDocument, getDocuments, connectToDatabase } from "./mongoHandler.mjs"; const collectionName = 'radio-systems'; @@ -21,7 +23,7 @@ export const createSystem = async (name, system, nuid) => { const insertedId = await insertDocument(collectionName, system); return insertedId; } catch (error) { - console.error('Error creating system:', error); + log.ERROR('Error creating system:', error); throw error; } }; @@ -32,7 +34,7 @@ export const getAllSystems = async () => { const systems = await getDocuments(collectionName); return systems; } catch (error) { - console.error('Error getting all systems:', error); + log.ERROR('Error getting all systems:', error); throw error; } }; @@ -45,7 +47,7 @@ export const getSystemByName = async (name) => { const system = await collection.findOne({ name }); return system; } catch (error) { - console.error('Error getting system by name:', error); + log.ERROR('Error getting system by name:', error); throw error; } finally { // Close the connection @@ -65,7 +67,7 @@ export const getSystemsByNuid = async (nuid) => { return systems; } catch (error) { - console.error('Error finding entries:', error); + log.ERROR('Error finding entries:', error); throw error; } finally { // Close the connection @@ -82,10 +84,10 @@ export const updateSystemByName = async (name, updatedSystem) => { try { const collection = db.db().collection(collectionName); const result = await collection.updateOne({ name }, { $set: updatedSystem }); - console.log('System updated:', result.modifiedCount); + log.INFO('System updated:', result.modifiedCount); return result.modifiedCount; } catch (error) { - console.error('Error updating system by name:', error); + log.ERROR('Error updating system by name:', error); throw error; } finally { // Close the connection @@ -99,10 +101,10 @@ export const deleteSystemByName = async (name) => { try { const collection = db.db().collection(collectionName); const result = await collection.deleteOne({ name }); - console.log('System deleted:', result.deletedCount); + log.INFO('System deleted:', result.deletedCount); return result.deletedCount; } catch (error) { - console.error('Error deleting system by name:', error); + log.ERROR('Error deleting system by name:', error); throw error; } finally { // Close the connection diff --git a/modules/socketServer.mjs b/modules/socketServer.mjs index 848ad9f..26c83ee 100644 --- a/modules/socketServer.mjs +++ b/modules/socketServer.mjs @@ -1,3 +1,5 @@ +import { DebugBuilder } from "../modules/debugger.mjs"; +const log = new DebugBuilder("server", "socketServer"); import express from 'express'; import { createServer } from 'node:http'; import { Server } from 'socket.io'; @@ -15,7 +17,7 @@ app.get('/', (req, res) => { }); nodeIo.on('connection', (socket) => { - console.log('a user connected', socket.id); + log.INFO('a user connected', socket.id); socket.on('node-login', async (data) => { await nodeLoginWrapper(data, socket); diff --git a/modules/socketServerWrappers.mjs b/modules/socketServerWrappers.mjs index f7c0608..ccc4e16 100644 --- a/modules/socketServerWrappers.mjs +++ b/modules/socketServerWrappers.mjs @@ -1,3 +1,5 @@ +import { DebugBuilder } from "../modules/debugger.mjs"; +const log = new DebugBuilder("server", "socketServerWrappers"); import { createNode, getNodeByNuid, updateNodeByNuid } from "./mongo-wrappers/mongoNodesWrappers.mjs" import { createSystem, getSystemByName, updateSystemByName, getSystemsByNuid, deleteSystemByName } from "./mongo-wrappers/mongoSystemsWrappers.mjs" @@ -22,17 +24,16 @@ const sendNodeCommand = async (socket, command, data) => { * @returns {any} */ export const nodeLoginWrapper = async (data, socket) => { - console.log(`Login requested from node: ${data.nuid}`, data); + log.INFO(`Login requested from node: ${data.nuid}`, data); // Check to see if node exists - var node = await getNodeByNuid(data.nuid); - console.log("After grabbing", node); + var node = await getNodeByNuid(data.nuid); if (!node) { const insertedId = await createNode(data); - console.log("Added new node to the database:", insertedId); + log.DEBUG("Added new node to the database:", insertedId); } else { // Check for updates const updatedNode = await updateNodeByNuid(data.nuid, data) - console.log("Updated node:", updatedNode); + log.DEBUG("Updated node:", updatedNode); } node = await getNodeByNuid(data.nuid); @@ -59,7 +60,7 @@ export const nodeDisconnectWrapper = async (socketId) => { * @returns {any} */ export const nodeUpdateWrapper = async (nodeData) => { - console.log("Data update sent by node: ", nodeData); + log.DEBUG("Data update sent by node: ", nodeData); const updateResults = await updateNodeByNuid(nodeData.nuid, nodeData); return; } @@ -70,10 +71,10 @@ export const nodeUpdateWrapper = async (nodeData) => { * @param {object} nearbySystems The nearby systems object passed from the node to be updated */ export const nearbySystemsUpdateWraper = async (nuid, nearbySystems) => { - console.log("System updates sent by node: ", nuid, nearbySystems); + log.DEBUG("System updates sent by node: ", nuid, nearbySystems); // Check to see if the node removed any systems const existingSystems = await getSystemsByNuid(nuid); - console.log("Existing systems:", existingSystems); + log.DEBUG("Existing systems:", existingSystems); if (existingSystems !== nearbySystems) { for (const existingSystem of existingSystems) { if (existingSystem.name in nearbySystems) { @@ -81,17 +82,17 @@ export const nearbySystemsUpdateWraper = async (nuid, nearbySystems) => { continue; } - console.log("System exists that was not given by node", existingSystem); + log.DEBUG("System exists that was not given by node", existingSystem); // Check if this node was the only node on this system if (existingSystem.nodes.filter(node => node !== nuid).length === 0) { // Remove the system if so - console.log("Given node was the only node on this system, removing the system..."); + log.INFO("Given node was the only node on this system, removing the system..."); await deleteSystemByName(existingSystem.name); } else { // Remove the node from the array if there are other nodes with this system - console.log("Other nodes found on this system, removing the given NUID"); + log.INFO("Other nodes found on this system, removing the given NUID"); existingSystem.nodes = existingSystem.nodes.filter(node => node !== nuid); - console.log(existingSystem); + log.DEBUG(existingSystem); await updateSystemByName(existingSystem.name, existingSystem); } } @@ -111,7 +112,7 @@ export const nearbySystemsUpdateWraper = async (nuid, nearbySystems) => { existingSystem.nodes.push(nuid); // Update the system with the added node const updateResults = await updateSystemByName(nearbySystem, existingSystem); - if (updateResults) console.log("System updated", nearbySystem); + if (updateResults) log.INFO("System updated", nearbySystem); } } else { // The systems are not the same @@ -125,13 +126,13 @@ export const nearbySystemsUpdateWraper = async (nuid, nearbySystems) => { // Update the system with the added node const updateResults = await updateSystemByName(nearbySystem, nearbySystems[nearbySystem]); - if (updateResults) console.log("System updated", nearbySystem); + if (updateResults) log.INFO("System updated", nearbySystem); } } else { // Create a new system const newSystem = await createSystem(nearbySystem, nearbySystems[nearbySystem], nuid); - console.log("New system created", nearbySystem, newSystem); + log.INFO("New system created", nearbySystem, newSystem); } } return; @@ -146,7 +147,7 @@ export const nearbySystemsUpdateWraper = async (nuid, nearbySystems) => { export const getSocketIdByNuid = async (nodeIo, nuid) => { const openSockets = await nodeIo.allSockets(); for (const openSocketId of openSockets) { - console.log(openSockets) + log.DEBUG(openSockets) const openSocket = await nodeIo.sockets.sockets.get(openSocketId); if (openSocket.node.nuid == nuid) return openSocket; @@ -171,10 +172,10 @@ export const getAllSocketsConnectedToVC = async (nodeIo, guildId) => { await new Promise((res) => { openSocket.emit('node-check-connected-status', guildId, (status) => { if (status) { - console.log("Socket is connected to VC:", openSocket.node.name, status); + log.INFO("Socket is connected to VC:", openSocket.node.name, status); socketsConnectedToVC.push(openSocket); } else { - console.log("Socket is NOT connected to VC:", openSocket.node.name); + log.INFO("Socket is NOT connected to VC:", openSocket.node.name); } res(); }) @@ -196,10 +197,10 @@ export const checkIfNodeHasOpenDiscordClient = async (openSocket) => { await new Promise((res) => { openSocket.emit('node-check-discord-open-client', (status) => { if (status) { - console.log("Socket has an open discord client:", openSocket.node.name, status); + log.INFO("Socket has an open discord client:", openSocket.node.name, status); hasOpenDiscordClient = true; } else { - console.log("Socket does NOT have an open discord client:", openSocket.node.name); + log.INFO("Socket does NOT have an open discord client:", openSocket.node.name); } res(); }) @@ -217,10 +218,10 @@ export const getNodeCurrentListeningSystem = async (openSocket) => { await new Promise((res) => { openSocket.emit('node-check-current-system', (system) => { if (system) { - console.log("Socket is listening to system:", openSocket.node.name, system); + log.INFO("Socket is listening to system:", openSocket.node.name, system); currentSystem = system; } else { - console.log("Socket is not currently listening to a system:", openSocket.node.name); + log.INFO("Socket is not currently listening to a system:", openSocket.node.name); } res(); }) @@ -307,9 +308,9 @@ export const requestBotLeaveServer = async (socket, guildId) => { export const requestNodeUpdate = async (socket) => { await sendNodeCommand(socket, 'node-update', (status) => { if (status) { - console.log("Node is out of date, updating now", socket.node.name); + log.INFO("Node is out of date, updating now", socket.node.name); } else { - console.log("Node is up to date", socket.node.name); + log.INFO("Node is up to date", socket.node.name); } }); } \ No newline at end of file diff --git a/rss-manager/feedHandler.mjs b/rss-manager/feedHandler.mjs index aae743b..b0f81b8 100644 --- a/rss-manager/feedHandler.mjs +++ b/rss-manager/feedHandler.mjs @@ -5,6 +5,9 @@ import { DebugBuilder } from "../modules/debugger.mjs"; import UserAgent from "user-agents"; import Parser from 'rss-parser'; +import dotenv from 'dotenv'; +dotenv.config() + // Initialize the User-Agent string process.env.USER_AGENT_STRING = new UserAgent({ platform: 'Win32' }).toString(); @@ -16,8 +19,9 @@ const parser = new Parser({ }); const log = new DebugBuilder("server", "feedHandler"); -const runningPostsToRemove = {}; -const sourceFailureLimit = 5; +const sourceFailureLimit = process.env.RSS_SOURCE_FAILURE_LIMIT ?? 5; +const runningSourcesToRemove = {}; // This holds the sources that are pending removal (they've failed to load, return data, etc.) + export const returnHash = (...stringsIncluded) => { return crypto.createHash('sha1').update(stringsIncluded.join("-<>-")).digest("base64"); @@ -100,22 +104,22 @@ export const addSource = async (title, link, category, guildId, channelId, callb export const removeSource = async (sourceURL) => { log.INFO("Removing source:", sourceURL); - if (!runningPostsToRemove[sourceURL]) { - runningPostsToRemove[sourceURL] = { count: 1, timestamp: Date.now(), ignoredAttempts: 0 }; + if (!runningSourcesToRemove[sourceURL]) { + runningSourcesToRemove[sourceURL] = { count: 1, timestamp: Date.now(), ignoredAttempts: 0 }; return; } - const elapsedTime = Date.now() - runningPostsToRemove[sourceURL].timestamp; - const waitTime = runningPostsToRemove[sourceURL].count * 30000; + const elapsedTime = Date.now() - runningSourcesToRemove[sourceURL].timestamp; + const waitTime = runningSourcesToRemove[sourceURL].count * 30000; if (elapsedTime <= waitTime) { - runningPostsToRemove[sourceURL].ignoredAttempts += 1; + runningSourcesToRemove[sourceURL].ignoredAttempts += 1; return; } - if (runningPostsToRemove[sourceURL].count < sourceFailureLimit) { - runningPostsToRemove[sourceURL].count += 1; - runningPostsToRemove[sourceURL].timestamp = Date.now(); + if (runningSourcesToRemove[sourceURL].count < sourceFailureLimit) { + runningSourcesToRemove[sourceURL].count += 1; + runningSourcesToRemove[sourceURL].timestamp = Date.now(); return; } diff --git a/rss-manager/rssController.mjs b/rss-manager/rssController.mjs index e99d22c..fb64698 100644 --- a/rss-manager/rssController.mjs +++ b/rss-manager/rssController.mjs @@ -1,37 +1,49 @@ -//Will handle updating feeds in all channels +// Will handle updating feeds in all channels import { DebugBuilder } from "../modules/debugger.mjs"; import { updateFeeds } from "./feedHandler.mjs"; import dotenv from 'dotenv'; -dotenv.config() +dotenv.config(); const log = new DebugBuilder("server", "rssController"); -const refreshInterval = process.env.RSS_REFRESH_INTERVAL ?? 300000; +const refreshInterval = parseInt(process.env.RSS_REFRESH_INTERVAL) || 300000; export class RSSController { constructor(client) { - this.client = client; + this.client = client; + this.intervalId = null; } async start() { - // Wait for the refresh period before starting RSS feeds, so the rest of the bot can start - await new Promise(resolve => setTimeout(resolve, refreshInterval)); - - log.INFO("Starting RSS Controller"); - // Get initial feeds before starting the infinite loop - await updateFeeds(this.client); - - while(true) { - // Wait for the refresh interval, then wait for the posts to return, then wait a quarter of the refresh interval to make sure everything is cleared up - await new Promise(resolve => setTimeout(resolve, refreshInterval)); + try { + log.INFO("Starting RSS Controller"); + // Get initial feeds before starting the interval loop await this.collectLatestPosts(); - await new Promise(resolve => setTimeout(resolve, refreshInterval / 4)); - } + + // Start the interval loop for updating feeds + this.intervalId = setInterval(async () => { + await this.collectLatestPosts(); + }, refreshInterval); + + } catch (error) { + log.ERROR(`Failed to start RSS Controller: ${error.message}`); + } + } + + async stop() { + if (this.intervalId) { + clearInterval(this.intervalId); + log.INFO("RSS Controller stopped"); + } } async collectLatestPosts() { - log.INFO("Updating sources"); - await updateFeeds(this.client); + try { + log.INFO("Updating sources"); + await updateFeeds(this.client); + } catch (error) { + log.ERROR(`Error updating feeds: ${error.message}`); + } } } diff --git a/server.js b/server.js index 04769c3..8f35323 100644 --- a/server.js +++ b/server.js @@ -1,3 +1,5 @@ +import { DebugBuilder } from "./modules/debugger.mjs"; +const log = new DebugBuilder("server", "server"); import { nodeIo, app, server } from './modules/socketServer.mjs'; import { loadAddons } from './modules/addonManager.mjs'; import { serverClient, addEnabledEventListeners } from './discordBot/discordBot.mjs'; @@ -7,7 +9,7 @@ dotenv.config() // Startup the node server server.listen(process.env.SERVER_PORT || 3000, () => { - console.log(`server running at http://localhost:${process.env.SERVER_PORT}`); + log.INFO(`server running at http://localhost:${process.env.SERVER_PORT}`); }); // Add objects to the others