Added onIcon and onFile events. onMessage is now being called only on messageType == message. Removed debug code from swarm.on('update').

This commit is contained in:
MrMasrozYTLIVE 2024-06-10 21:23:59 +03:00
parent 56d8a8091e
commit 7275b69948
2 changed files with 6 additions and 4 deletions

View File

@ -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}`);

View File

@ -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(", ")}]`);
});
});
}