handle chat commands better

in preparation for other commands such as for resyncing
votekiss
maia arson crimew 2022-02-13 19:35:52 +01:00
parent 72c212a100
commit af4b23e879
1 changed files with 35 additions and 17 deletions

View File

@ -12,17 +12,36 @@ const setupChatboxEvents = (socket) => {
if (content.trim().length) { if (content.trim().length) {
input.value = ""; input.value = "";
if ( // handle commands
content.toLowerCase() == "/ping" || if (content.startsWith("/")) {
content.toLowerCase().startsWith("/ping ") const command = content.toLowerCase().match(/^\/\S+/)[0];
) { const args = content.slice(command.length).trim();
let handled = false;
switch (command) {
case "/ping":
socket.send( socket.send(
JSON.stringify({ JSON.stringify({
op: "Ping", op: "Ping",
data: content.slice(5).trim(), data: args,
}) })
); );
} else { handled = true;
break;
case "/help":
// TODO: print help in chat
handled = true;
break;
default:
break;
}
if (handled) {
return;
}
}
// handle regular chat messages
socket.send( socket.send(
JSON.stringify({ JSON.stringify({
op: "ChatMessage", op: "ChatMessage",
@ -30,7 +49,6 @@ const setupChatboxEvents = (socket) => {
}) })
); );
} }
}
}); });
}; };