From 7275b6994863e317821870db0cda0604be5c73c3 Mon Sep 17 00:00:00 2001 From: MrMasrozYTLIVE <61359286+MrMasrozYTLIVE@users.noreply.github.com> Date: Mon, 10 Jun 2024 21:23:59 +0300 Subject: [PATCH] Added onIcon and onFile events. onMessage is now being called only on messageType == message. Removed debug code from swarm.on('update'). --- chatBot/bot.js | 1 - chatBot/includes/client.js | 9 ++++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/chatBot/bot.js b/chatBot/bot.js index b2844c3..c1359b9 100644 --- a/chatBot/bot.js +++ b/chatBot/bot.js @@ -39,7 +39,6 @@ loadCommands().then(commands => { // We use Event Emitter here to handle new messages bot.on('onMessage', (peer, message) => { - if (message.type == "icon") return; console.log(message); console.log(`Message received from ${message.name}@${message.topic} at ${new Date(message.timestamp).toLocaleTimeString()}: ${message.message}`); diff --git a/chatBot/includes/client.js b/chatBot/includes/client.js index ae51599..426b094 100644 --- a/chatBot/includes/client.js +++ b/chatBot/includes/client.js @@ -13,7 +13,12 @@ class Client extends EventEmitter { setupSwarm() { this.swarm.on('connection', (peer) => { - peer.on('data', message => this.emit('onMessage', peer, JSON.parse(message.toString()))); + 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())); + }); + peer.on('error', e => { this.emit('onError', e); console.error(`Connection error: ${e}`) @@ -30,8 +35,6 @@ class Client extends EventEmitter { const peerTopics = [peerInfo.topics] .filter(topics => topics) .map(topics => topics.map(topic => b4a.toString(topic, 'hex'))); - - console.log(`Peer ${peer} is in topic(s) [${peerTopics.join(", ")}]`); }); }); }