From 01fee56692ecef50e421f7243ca166870e0a7e68 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Mon, 10 Jun 2024 19:49:46 -0400 Subject: [PATCH] Fixing bot messages --- app.js | 1 - chatBot/includes/Client.js | 35 ++++++++++++++++++++++++----------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/app.js b/app.js index ff0e68c..cb66232 100644 --- a/app.js +++ b/app.js @@ -433,7 +433,6 @@ function onMessageAdded(from, message, avatar) { const $img = document.createElement('img'); $img.src = updatePortInUrl(avatar) || 'https://via.placeholder.com/40'; // Default to a placeholder image if avatar URL is not provided - console.log(updatePortInUrl(avatar)) $img.classList.add('avatar'); $div.appendChild($img); diff --git a/chatBot/includes/Client.js b/chatBot/includes/Client.js index 426b094..97f13fc 100644 --- a/chatBot/includes/Client.js +++ b/chatBot/includes/Client.js @@ -5,7 +5,7 @@ import b4a from "b4a"; class Client extends EventEmitter { constructor(botName) { super(); - if(!botName) return console.error("BotName is not defined!"); + if (!botName) return console.error("BotName is not defined!"); this.botName = botName; this.swarm = new Hyperswarm(); this.setupSwarm(); @@ -14,9 +14,10 @@ class Client extends EventEmitter { setupSwarm() { this.swarm.on('connection', (peer) => { peer.on('data', message => { - if(message.type === "message") this.emit('onMessage', peer, JSON.parse(message.toString())); - if(message.type === "icon") this.emit('onIcon', peer, JSON.parse(message.toString())); - if(message.type === "file") this.emit('onFile', peer, JSON.parse(message.toString())); + const messageObj = JSON.parse(message.toString()); + if (messageObj.type === "message") this.emit('onMessage', peer, messageObj); + if (messageObj.type === "icon") this.emit('onIcon', peer, messageObj); + if (messageObj.type === "file") this.emit('onFile', peer, messageObj); }); peer.on('error', e => { @@ -33,14 +34,14 @@ class Client extends EventEmitter { const peer = [peerId]; const peerTopics = [peerInfo.topics] - .filter(topics => topics) - .map(topics => topics.map(topic => b4a.toString(topic, 'hex'))); + .filter(topics => topics) + .map(topics => topics.map(topic => b4a.toString(topic, 'hex'))); }); }); } joinChatRoom(chatRoomID) { - this.discovery = this.swarm.join(Buffer.from(chatRoomID, 'hex'), {client: true, server: true}); + this.discovery = this.swarm.join(Buffer.from(chatRoomID, 'hex'), { client: true, server: true }); this.discovery.flushed().then(() => { console.log(`Bot ${this.botName} joined the chat room.`); this.emit('onBotJoinRoom'); @@ -50,16 +51,28 @@ class Client extends EventEmitter { sendMessage(roomPeers, message) { console.log('Bot name:', this.botName); const timestamp = Date.now(); // Generate timestamp - const peers = [...this.swarm.connections].filter(peer => roomPeers.includes(peer.remotePublicKey.toString('hex'))); // We remove all the peers that arent included in a room - const data = JSON.stringify({name: this.botName, message, timestamp}); // Include timestamp + const messageObj = { + type: 'message', // Add type field + name: this.botName, + message, + timestamp + }; + const data = JSON.stringify(messageObj); + const peers = [...this.swarm.connections].filter(peer => roomPeers.includes(peer.remotePublicKey.toString('hex'))); // We remove all the peers that aren't included in a room for (const peer of peers) peer.write(data); } sendMessageToAll(message) { console.log('Bot name:', this.botName); const timestamp = Date.now(); // Generate timestamp - const peers = [...this.swarm.connections] - const data = JSON.stringify({name: this.botName, message, timestamp}); // Include timestamp + const messageObj = { + type: 'message', // Add type field + name: this.botName, + message, + timestamp + }; + const data = JSON.stringify(messageObj); + const peers = [...this.swarm.connections]; for (const peer of peers) peer.write(data); }