From e57db27a1341b7b39417024711f295b403e7558c Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Mon, 10 Jun 2024 22:20:36 -0400 Subject: [PATCH] Fixing bot --- chatBot/includes/Client.js | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/chatBot/includes/Client.js b/chatBot/includes/Client.js index 7c939c0..8fe0b5e 100644 --- a/chatBot/includes/Client.js +++ b/chatBot/includes/Client.js @@ -8,7 +8,8 @@ class Client extends EventEmitter { if (!botName) return console.error("BotName is not defined!"); this.botName = botName; this.swarm = new Hyperswarm(); - this.currentTopic = null; // Store the current topic + this.joinedRooms = new Set(); // Track the rooms the bot has joined + this.currentTopic = null; // Track the current topic this.setupSwarm(); } @@ -16,12 +17,12 @@ class Client extends EventEmitter { this.swarm.on('connection', (peer) => { peer.on('data', message => { const messageObj = JSON.parse(message.toString()); - if (messageObj.type === "message") { - this.currentTopic = messageObj.topic; // Capture the topic from incoming messages - this.emit('onMessage', peer, messageObj); + if (this.joinedRooms.has(messageObj.topic)) { // Process message only if it is from a joined room + this.currentTopic = messageObj.topic; // Set the current topic from the incoming message + 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); } - if (messageObj.type === "icon") this.emit('onIcon', peer, messageObj); - if (messageObj.type === "file") this.emit('onFile', peer, messageObj); }); peer.on('error', e => { @@ -32,17 +33,11 @@ class Client extends EventEmitter { this.swarm.on('update', () => { console.log(`Connections count: ${this.swarm.connections.size} / Peers count: ${this.swarm.peers.size}`); - - this.swarm.peers.forEach((peerInfo, peerId) => { - const peer = [peerId]; - const peerTopics = [peerInfo.topics] - .filter(topics => topics) - .map(topics => topics.map(topic => b4a.toString(topic, 'hex'))); - }); }); } joinChatRoom(chatRoomID) { + this.joinedRooms.add(chatRoomID); // Add the room to the list of joined rooms this.currentTopic = chatRoomID; // Store the current topic this.discovery = this.swarm.join(Buffer.from(chatRoomID, 'hex'), { client: true, server: true }); this.discovery.flushed().then(() => {