forked from snxraven/LinkUp-P2P-Chat
57 lines
1.8 KiB
JavaScript
57 lines
1.8 KiB
JavaScript
// commands.js
|
|
import b4a from 'b4a';
|
|
import fs from 'fs';
|
|
|
|
const agentAvatarPath = './assets/agent.png';
|
|
let agentAvatar = '';
|
|
|
|
// Load the agent avatar once when the module is imported
|
|
if (fs.existsSync(agentAvatarPath)) {
|
|
const avatarBuffer = fs.readFileSync(agentAvatarPath);
|
|
agentAvatar = `data:image/png;base64,${b4a.toString(avatarBuffer, 'base64')}`;
|
|
}
|
|
|
|
export default async function handleCommand(command, context) {
|
|
const { eventEmitter, currentTopic, clearMessages, addMessage, joinRoom, leaveRoom, createRoom, listFiles } = context;
|
|
|
|
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:\n- ~clear\n- ~ping\n- ~help\n- ~join [topic]\n- ~leave\n- ~create [alias]\n- ~list-files', agentAvatar, currentTopic());
|
|
break;
|
|
case '~join':
|
|
if (restArgs) {
|
|
await joinRoom(restArgs);
|
|
} else {
|
|
addMessage('LinkUp', 'Usage: ~join [topic]', agentAvatar, currentTopic());
|
|
}
|
|
break;
|
|
case '~leave':
|
|
leaveRoom(currentTopic());
|
|
break;
|
|
case '~create':
|
|
if (restArgs) {
|
|
await createRoom(restArgs);
|
|
} else {
|
|
addMessage('LinkUp', 'Usage: ~create [alias]', agentAvatar, currentTopic());
|
|
}
|
|
break;
|
|
case '~list-files':
|
|
const files = await listFiles();
|
|
const fileList = files.length > 0 ? files.map(file => `- ${file}`).join('\n') : 'No files available';
|
|
addMessage('LinkUp', `Available files:\n${fileList}`, agentAvatar, currentTopic());
|
|
break;
|
|
default:
|
|
console.log('Unknown command:', command);
|
|
}
|
|
}
|