Compare commits
19 Commits
b88337c398
...
4fbbc73527
Author | SHA1 | Date | |
---|---|---|---|
4fbbc73527 | |||
|
edf61f0462 | ||
|
92ba56b9fc | ||
|
7275b69948 | ||
|
56d8a8091e | ||
|
4ec0be2774 | ||
|
27b3528304 | ||
|
2ca07e46c3 | ||
|
e396bcbb0b | ||
|
3b7f0fbd1d | ||
|
79bdd04810 | ||
|
1dca21fe75 | ||
|
edd3f632cf | ||
|
340362872f | ||
|
230cdd1321 | ||
|
acb5609d03 | ||
|
66d684e473 | ||
|
03f053723d | ||
|
033330fd04 |
3
app.js
3
app.js
@ -307,7 +307,7 @@ function sendMessage(e) {
|
||||
message,
|
||||
avatar: config.userAvatar,
|
||||
topic: b4a.toString(currentRoom.topic, 'hex'),
|
||||
peers: peersPublicKeys,
|
||||
peers: peersPublicKeys, // Deprecated. To be deleted in future updates
|
||||
timestamp: Date.now(),
|
||||
readableTimestamp: new Date().toLocaleString(), // Added human-readable timestamp
|
||||
});
|
||||
@ -327,6 +327,7 @@ function sendFileMessage(name, fileUrl, fileType, avatar) {
|
||||
fileUrl,
|
||||
fileType,
|
||||
avatar,
|
||||
topic: b4a.toString(currentRoom.topic, 'hex'),
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import Client from './includes/client.js'; // Adjust the import path as necessary
|
||||
import Client from './includes/Client.js'; // Adjust the import path as necessary
|
||||
import 'dotenv/config';
|
||||
|
||||
// Create a new instance of the chatBot class with a valid botName
|
||||
@ -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}`);
|
||||
|
@ -3,8 +3,6 @@ class ChatRoom {
|
||||
this.topic = topic;
|
||||
this.peers = peers;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
export default ChatRoom;
|
@ -1,5 +1,6 @@
|
||||
import Hyperswarm from 'hyperswarm';
|
||||
import EventEmitter from 'node:events'
|
||||
import b4a from "b4a";
|
||||
|
||||
class Client extends EventEmitter {
|
||||
constructor(botName) {
|
||||
@ -12,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}`)
|
||||
@ -20,7 +26,16 @@ class Client extends EventEmitter {
|
||||
});
|
||||
|
||||
this.swarm.on('update', () => {
|
||||
console.log(`Peers count: ${this.swarm.connections.size}`);
|
||||
console.log(`Connections count: ${this.swarm.connections.size} / Peers count: ${this.swarm.peers.size}`);
|
||||
|
||||
this.swarm.peers.forEach((peerInfo, peerId) => {
|
||||
// Please do not try to understand what is going on here. I have no idea anyway. But it surprisingly works
|
||||
|
||||
const peer = [peerId];
|
||||
const peerTopics = [peerInfo.topics]
|
||||
.filter(topics => topics)
|
||||
.map(topics => topics.map(topic => b4a.toString(topic, 'hex')));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
12
chatBot/includes/FileMessage.js
Normal file
12
chatBot/includes/FileMessage.js
Normal file
@ -0,0 +1,12 @@
|
||||
class FileMessage {
|
||||
public FileMessage(chatRoom, userPeer, fileName, fileUrl, fileType, timestamp) {
|
||||
this.chatRoom = chatRoom;
|
||||
this.userPeer = userPeer;
|
||||
this.fileName = fileName;
|
||||
this.fileUrl = fileUrl;
|
||||
this.fileType = fileType;
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
}
|
||||
|
||||
export default FileMessage;
|
10
chatBot/includes/TextMessage.js
Normal file
10
chatBot/includes/TextMessage.js
Normal file
@ -0,0 +1,10 @@
|
||||
class TextMessage {
|
||||
public TextMessage(chatRoom, userPeer, message, timestamp) {
|
||||
this.chatRoom = chatRoom;
|
||||
this.userPeer = userPeer;
|
||||
this.message = message;
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
}
|
||||
|
||||
export default TextMessage;
|
10
chatBot/includes/UserPeer.js
Normal file
10
chatBot/includes/UserPeer.js
Normal file
@ -0,0 +1,10 @@
|
||||
class UserPeer {
|
||||
public UserPeer(peer, topics, username, avatar) {
|
||||
this.peer = peer;
|
||||
this.topics = topics;
|
||||
this.username = username;
|
||||
this.avatar = avatar;
|
||||
}
|
||||
}
|
||||
|
||||
export default UserPeer;
|
Loading…
Reference in New Issue
Block a user