Fixed bot crash on file attachment, added 2 more new events and topic ID to file message #6

Merged
snxraven merged 18 commits from MiTask/LinkUp-P2P-Chat:main into main 2024-06-10 16:09:25 -04:00
2 changed files with 6 additions and 4 deletions
Showing only changes of commit 7275b69948 - Show all commits

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