LinkUp-P2P-Chat/commands.js

45 lines
1.3 KiB
JavaScript
Raw Normal View History

// 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')}`;
}
2024-06-14 01:10:23 -04:00
export default async function handleCommand(command, context) {
2024-06-14 01:18:13 -04:00
const { eventEmitter, currentTopic, clearMessagesCMD, addMessage, joinRoom, leaveRoom } = context;
2024-06-14 01:10:23 -04:00
const args = command.trim().split(' ');
const cmd = args[0].toLowerCase();
const restArgs = args.slice(1).join(' ');
2024-06-14 01:10:23 -04:00
switch (cmd) {
case '~clear':
clearMessagesCMD();
break;
case '~ping':
addMessage('LinkUp', 'pong', agentAvatar, currentTopic());
break;
2024-06-14 01:10:23 -04:00
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);
}
2024-06-14 01:10:23 -04:00
}