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() {
try {
servePort = getRandomPort();
@ -493,7 +499,9 @@ function leaveRoom(topic) {
document.querySelector('#setup').classList.remove('hidden');
}
}
function sendMessage(e) {
async function sendMessage(e) {
e.preventDefault();
const message = document.querySelector('#message').value;
document.querySelector('#message').value = '';
@ -503,11 +511,13 @@ function sendMessage(e) {
if (message.startsWith('~')) {
// Handle command
handleCommand(message, {
await handleCommand(message, {
eventEmitter,
currentTopic,
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;
}

View File

@ -11,16 +11,33 @@ if (fs.existsSync(agentAvatarPath)) {
agentAvatar = `data:image/png;base64,${b4a.toString(avatarBuffer, 'base64')}`;
}
export default function handleCommand(command, context) {
const { eventEmitter, currentTopic, clearMessages, addMessage } = context;
export default async function handleCommand(command, 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':
clearMessages();
break;
case '~ping':
addMessage('LinkUp', 'pong', agentAvatar, currentTopic());
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:
console.log('Unknown command:', command);
}