From ddcf67226f56cd9e41f581bde83b75ec4165a142 Mon Sep 17 00:00:00 2001 From: Fernando Campione Date: Fri, 9 Jan 2026 10:28:50 +0000 Subject: [PATCH] modularise chat commands and add error handling for wrong commands --- devdocs/CHAT-COMMANDS.md | 25 +++ public/app.js | 26 ++- public/index.html | 279 +++++++++++------------- public/js/chat-commands/clear.js | 7 + public/js/chat-commands/frenzy.js | 43 ++++ public/js/chat-commands/help.js | 76 +++++++ public/js/chat-commands/replacements.js | 10 + public/js/commands.js | 185 +++++----------- 8 files changed, 359 insertions(+), 292 deletions(-) create mode 100644 devdocs/CHAT-COMMANDS.md create mode 100644 public/js/chat-commands/clear.js create mode 100644 public/js/chat-commands/frenzy.js create mode 100644 public/js/chat-commands/help.js create mode 100644 public/js/chat-commands/replacements.js diff --git a/devdocs/CHAT-COMMANDS.md b/devdocs/CHAT-COMMANDS.md new file mode 100644 index 0000000..2878585 --- /dev/null +++ b/devdocs/CHAT-COMMANDS.md @@ -0,0 +1,25 @@ +# Chat Commands + +Chat commands are modular and located in `public/js/chat-commands/`. + +## Adding a Command + +1. Create `chat-commands/mycommand.js`: +```javascript +export const myCommand = { + description: "What it does", + execute: () => { + const output = document.getElementById("terminal-output"); + // implementation + }, +}; +``` + +2. Import and register in `commands.js`: +```javascript +import { myCommand } from "./chat-commands/mycommand.js"; + +const actions = { + // ... existing commands + "/mycommand": myCommand, +}; \ No newline at end of file diff --git a/public/app.js b/public/app.js index 5c53da5..cf7bbcf 100644 --- a/public/app.js +++ b/public/app.js @@ -466,7 +466,7 @@ const appendMessage = (msg) => { senderName = msg.sender.slice(-8); } - // Update name map + // Update name map (before changing senderName to "You") nameToId.set(senderName, msg.sender); if (msg.sender === myId) senderName = "You"; @@ -491,7 +491,7 @@ const appendMessage = (msg) => { const contentSpan = document.createElement("span"); contentSpan.className = "msg-content"; - // Use formatMessage for rich text rendering + const rawContent = ` > ${msg.content}`; if (window.ChatCommands) { contentSpan.innerHTML = window.ChatCommands.formatMessage(rawContent); @@ -523,13 +523,24 @@ terminalInput.addEventListener("keypress", async (e) => { if (window.ChatCommands) { const result = window.ChatCommands.processInput(content); if (result.type === "action") { - window.ChatCommands.actions[result.command].execute(); + const action = window.ChatCommands.actions[result.command]; + if (action && typeof action.execute === "function") { + try { + action.execute(); + } catch (err) { + console.error("Command execution error:", err); + systemStatusBar.innerText = `[SYSTEM] Command failed: ${result.command}`; + } + } else { + systemStatusBar.innerText = `[SYSTEM] Unknown command: ${result.command}`; + } return; } else if (result.type === "text") { content = result.content; } } + let scope = "GLOBAL"; let target = null; @@ -594,10 +605,11 @@ terminalInput.addEventListener("keypress", async (e) => { target = nameToId.get(potentialName); content = msg; scope = "GLOBAL"; + } else if (!msg) { + systemStatusBar.innerText = `[SYSTEM] Usage: /${potentialName} `; + return; } else { - // If it looks like a command but we don't recognize the user or command - // Prevent sending it as raw text to chat - systemStatusBar.innerText = `[SYSTEM] Unknown command or user: ${potentialName}`; + systemStatusBar.innerText = `[SYSTEM] Unknown user: ${potentialName}`; return; } } @@ -805,4 +817,6 @@ function cycleTheme() { } } +window.cycleTheme = cycleTheme; + document.getElementById("theme-switcher").addEventListener("click", cycleTheme); diff --git a/public/index.html b/public/index.html index f679559..1bc8b04 100644 --- a/public/index.html +++ b/public/index.html @@ -1,90 +1,76 @@ - - Hypermind - - - - - - - - - - - - - -
-
- New version available! - View Release - -
-
- -
-
- {{COUNT}} -
-
Active Nodes
- -
- ID: {{ID}}
- Direct Connections: {{DIRECT}}
- Total Unique: {{TOTAL_UNIQUE}}
- diagnostics - - | - map -
-
+ + Hypermind + + + + + + + + + + + + -