adding in new commands

This commit is contained in:
Raven Scott 2024-06-14 01:10:23 -04:00
parent b56d603b1e
commit 458954c1e1
2 changed files with 34 additions and 7 deletions

16
app.js
View File

@ -61,6 +61,12 @@ function updatePeerCount() {
} }
} }
async function joinRoom(topicStr) {
const topicBuffer = b4a.from(topicStr, 'hex');
addRoomToList(topicStr);
await joinSwarm(topicBuffer);
}
async function initialize() { async function initialize() {
try { try {
servePort = getRandomPort(); servePort = getRandomPort();
@ -493,7 +499,9 @@ function leaveRoom(topic) {
document.querySelector('#setup').classList.remove('hidden'); document.querySelector('#setup').classList.remove('hidden');
} }
} }
function sendMessage(e) {
async function sendMessage(e) {
e.preventDefault(); e.preventDefault();
const message = document.querySelector('#message').value; const message = document.querySelector('#message').value;
document.querySelector('#message').value = ''; document.querySelector('#message').value = '';
@ -503,11 +511,13 @@ function sendMessage(e) {
if (message.startsWith('~')) { if (message.startsWith('~')) {
// Handle command // Handle command
handleCommand(message, { await handleCommand(message, {
eventEmitter, eventEmitter,
currentTopic, currentTopic,
clearMessages, clearMessages,
addMessage: (from, message, avatar, topic) => onMessageAdded(from, message, avatar, topic, timestamp) addMessage: (from, message, avatar, topic) => onMessageAdded(from, message, avatar, topic, timestamp),
joinRoom,
leaveRoom
}); });
return; return;
} }

View File

@ -11,16 +11,33 @@ if (fs.existsSync(agentAvatarPath)) {
agentAvatar = `data:image/png;base64,${b4a.toString(avatarBuffer, 'base64')}`; agentAvatar = `data:image/png;base64,${b4a.toString(avatarBuffer, 'base64')}`;
} }
export default function handleCommand(command, context) { export default async function handleCommand(command, context) {
const { eventEmitter, currentTopic, clearMessages, addMessage } = context; const { eventEmitter, currentTopic, clearMessages, addMessage, joinRoom, leaveRoom } = context;
switch (command.trim().toLowerCase()) { const args = command.trim().split(' ');
const cmd = args[0].toLowerCase();
const restArgs = args.slice(1).join(' ');
switch (cmd) {
case '~clear': case '~clear':
clearMessages(); clearMessages();
break; break;
case '~ping': case '~ping':
addMessage('LinkUp', 'pong', agentAvatar, currentTopic()); addMessage('LinkUp', 'pong', agentAvatar, currentTopic());
break; break;
case '~help':
addMessage('LinkUp', 'Available commands: ~clear, ~ping, ~help, ~join [topic], ~leave', agentAvatar, currentTopic());
break;
case '~join':
if (restArgs) {
await joinRoom(restArgs);
} else {
addMessage('LinkUp', 'Usage: ~join [topic]', agentAvatar, currentTopic());
}
break;
case '~leave':
leaveRoom(currentTopic());
break;
default: default:
console.log('Unknown command:', command); console.log('Unknown command:', command);
} }