From af4b23e879fee38024fc4eca65ce0f1215d6b782 Mon Sep 17 00:00:00 2001 From: maia tillie arson crimew Date: Sun, 13 Feb 2022 19:35:52 +0100 Subject: [PATCH] handle chat commands better in preparation for other commands such as for resyncing --- frontend/lib/chat.mjs | 52 +++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/frontend/lib/chat.mjs b/frontend/lib/chat.mjs index 2b48b7f..1dc4275 100644 --- a/frontend/lib/chat.mjs +++ b/frontend/lib/chat.mjs @@ -12,24 +12,42 @@ const setupChatboxEvents = (socket) => { if (content.trim().length) { input.value = ""; - if ( - content.toLowerCase() == "/ping" || - content.toLowerCase().startsWith("/ping ") - ) { - socket.send( - JSON.stringify({ - op: "Ping", - data: content.slice(5).trim(), - }) - ); - } else { - socket.send( - JSON.stringify({ - op: "ChatMessage", - data: content, - }) - ); + // handle commands + if (content.startsWith("/")) { + const command = content.toLowerCase().match(/^\/\S+/)[0]; + const args = content.slice(command.length).trim(); + + let handled = false; + switch (command) { + case "/ping": + socket.send( + JSON.stringify({ + op: "Ping", + data: args, + }) + ); + handled = true; + break; + case "/help": + // TODO: print help in chat + handled = true; + break; + default: + break; + } + + if (handled) { + return; + } } + + // handle regular chat messages + socket.send( + JSON.stringify({ + op: "ChatMessage", + data: content, + }) + ); } }); };